Skip to main content

Cache Configuration

What to Cache?

PolyScale is designed to use automation to determine what caching rules should be applied to your database -- what to cache, and for how long. When you create a new cache, the default behavior is automatic caching enabled. With this setting in place, PolyScale will adaptively write query results to the cache and invalidate the cache based on mutations. By dynamically adapting to the ebb and flow of your traffic, PolyScale ensures optimal caching rules are applied at all times.

If, however, you want to provide more fine-grained control over caching rules, PolyScale allows you to override our automated caching settings.

Caching Defaults

When configuring your cache, you can select either Auto-cached or Uncached behavior. By default, a cache is created with Auto-cached behavior, which means that automation will be applied for every query against your database. With this setting in place, you can then manually override specified queries. Alternatively, if you set your Cache Defaults to Uncached, no caching will be done, except for those queries you manually specify.

Default Overrides

Cache Defaults can be overriden using rules. There are three types of rules: automation, template, and table. The section above describes how automation works; the section below describes template and table rules. By using a combination of the defaults and the overrides, you should be able to configure any pattern of caching logic required.

Query Template and Table Caching

PolyScale's template and table rules provide a way to do more traditional cache configuration. They offer a mechanism to easily set Time To Live (TTL) values on classes of queries with a single action.

RuleDescription
TABLEApply a given TTL to all SQL queries that utilize the specified table.
QUERY TEMPLATEApply a given TTL to a set of similar SQL queries.

Table TTL

Apply a given TTL to all SQL queries that utilize the specified table.

Settling a Table TTL will apply the specified TTL value to all SQL queries that reference that table. For example, consider the following queries:

SELECT AVG(price) FROM products;

SELECT name FROM products WHERE category="shoes" LIMIT 50;

Setting a TABLE TTL on the PRODUCTS table for say 60 seconds, would mean that both queries would be cached for 60 seconds.

Multi-table Queries (JOIN's)

For queries that utilize more than one table, TTL values must be set on all referenced tables before the query is deemed cacheable. For example, if a query joined across PRODUCTS and SALARIES tables, if a TTL value was set on one table and not the other, the query would not be cacheable.

tip

If different TTL values are configured for different tables used by a query, the lowest TTL is derived.

Query Template TTL

Apply a given TTL to a set of similar SQL queries.

A Query Template can be used to set a TTL value across a set of similar queries. Consider the following examples:

SELECT name, age FROM users WHERE id = 2;

SELECT name, age FROM users WHERE id = 6;

SELECT name, age FROM users WHERE id = 14;

Whilst these queries are syntactically different, many use cases require the ability to easily set a TTL value for all individual queries, even though they are unique. A query Template does exactly this. From a TTL lookup perspective, PolyScale normalizes the queries and removes the query parameters. In the above examples this yields the following query Template:

SELECT name, age FROM users WHERE id = ?;

A query Template can simply be thought of as a SQL query with the parameters removed which is assigned a TTL. Once a TTL is set on this template, all queries that match (once parameters are removed) inherit the TTL value.

Combining Override Rules

The three rules -- auto, template, and table -- can be used in combination to offer unlimited flexibility with regard to what queries are cached. The rules follow a specific hierarchy:

  1. AUTO - First, PolyScale will check for the existence of an AUTO rule for a template. If found, PolyScale's automated caching algorithms will be used for that template.

  2. TEMPLATE - Next, PolyScale will check for the existence of a TEMPLATE rule. If found, then that template TTL is used.

  3. TABLE - If a TEMPLATE rule cannot be found, PolyScale will then look to see if there is an applicable TABLE rule. If found, the applicable table TTL is used.

  4. CACHE DEFAULT - In the absence of any of the three overrides, the caching behavior will be determined by the default cache setting (Auto-cached or Uncached).