The labs on this site need JavaScript to run. The same content is written out below.
Learn › Architecture and operations › High traffic
Caching and TTL
If you give @Cacheable a TTL, how much does it reduce database queries, and when do requests pile up again?
Why it exists — If product information is read from the database again each time it is viewed, the database repeats the same work even though the result is the same every time, and when requests increase, the database is the first thing that cannot hold up. A cache keeps a result that has been read once in a memory store for a short time and returns it from there from the next request on, so that fewer requests go to the database. But if a value is kept forever, the cache never learns that the original changed, so we decide after how long to discard a value (the TTL), and that brought a new problem: at the moment the TTL ends, requests rush to the database all at once.
Where it's used today — It is used for data that is read often and changes occasionally, such as a product detail, a popular post list, and common codes. In Spring, you write @Cacheable on a lookup method, and if several servers must see the same cache, you attach Redis as the store and set the TTL with entryTtl of RedisCacheManager. For a key that many requests rush to, you let only one request query the database by using @Cacheable(sync = true) together with a locking RedisCacheWriter. Values that must always be the latest, such as inventory or balance right before payment, are read from the database without going through the cache.
One popular product is requested continuously for 30 seconds. The lookup method getProduct has Spring's @Cacheable on it, and the cache store is Redis (RedisCacheManager, provided by Spring Data Redis). @Cacheable looks at the cache first, and only when there is no value does it run the method body (the database query) and store the result in the cache. A stored value disappears from Redis after the time (TTL) set with entryTtl of RedisCacheConfiguration. This lab looks at the operating problems that come after that: requests rushing to the database at the moment the TTL ends (a cache stampede), and the cache giving an old value during the TTL even though the original has changed.
Checked against real execution · The code on the screen was run for 30 seconds each on Spring Boot 4.1.1, Spring Data Redis 4.1.1, Redis 8.10.2, and MySQL 8.4.11, and every request and the result table were compared for all 270 combinations (1, 5, and 20 requests per second). How many times the body runs with sync = true and the exception with unless were checked by running them on Spring 7.0.9
Concepts in this lab
- TTL (time to live)
- Where it's used today — In Redis, you set it with EXPIRE or the EX option of SET. Besides caches, it is used for values that must disappear by themselves after a set time, such as phone verification codes, sessions, and distributed locks.
- The length of time for which data is valid. If you set it when you put a value in a cache, the value is deleted automatically after that time passes. It is a basic safeguard that stops a value that no longer matches the source from staying in the cache forever.
- RedisCacheManager and entryTtl
- Why it exists — Spring's cache annotations are written so that they do not know the store, so a separate CacheManager that decides the actual store is needed. A memory cache on one server (ConcurrentMapCacheManager) keeps separate values on each server and has no TTL, so to let several servers see the same values and to make a value disappear after some time, a store such as Redis and an expiration setting were needed.
- Where it's used today — In a Spring application that keeps a cache shared by several servers in Redis, you register it as the cacheManager bean. In Spring Boot, a RedisCacheManager is created automatically and you can set the TTL of all caches with the property spring.cache.redis.time-to-live. To set it differently for each cache, you create the bean yourself as above, or call withCacheConfiguration in a RedisCacheManagerBuilderCustomizer bean.
- RedisCacheManager is an implementation of CacheManager provided by Spring Data Redis (spring-data-redis), and it lets Spring cache annotations such as @Cacheable use Redis as the store. You create it with RedisCacheManager.builder(connectionFactory), and the settings for each cache are set with RedisCacheConfiguration. If you give entryTtl, as in RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(10)), a value stored in that cache disappears from Redis after that time. If you do not give entryTtl, the value never expires, which is the default of defaultCacheConfig(). To give different settings for each cache name, you use withCacheConfiguration("product", settings) of the builder. By default, the key stored in Redis is the cache name followed by :: and then the key (product::1).
- Cache stampede
- A situation in which, at the moment a heavily read cache entry expires, many requests that arrive at the same time all fail to find the value in the cache and query the database at once. The sudden load on the database can lead to an outage. It is mitigated by locking so that only one request queries the source, or by spreading out the expiration times.
- @Cacheable(sync = true)
- Why it exists — @Cacheable does not lock anything by default, so when the cache was empty, calls that looked for the same key at the same time all ran the body and computed the same value several times. An attribute was added for this case, which asks the cache implementation to let only one call compute.
- Where it's used today — Write it on a method that many requests rush to and whose computation or database query is expensive. Whether it really blocks depends on the cache implementation, so if you use Redis as the store, you must also configure lockingRedisCacheWriter, and with a server memory cache (ConcurrentMapCacheManager, Caffeine), each server blocks separately.
- When the sync attribute of @Cacheable is set to true, Spring does not look at the cache first and run the body only when there is none. Instead, it calls get(key, valueLoader) of the cache implementation and hands the running of the body over as the valueLoader. Letting only one of the calls that look for the same key at the same time run the body, and making the others wait until the value is stored, is done by that cache implementation. With ConcurrentMapCacheManager, provided by Spring, the body ran only 1 time even when 20 threads called with the same key at the same time on an empty cache (20 times without sync). On the other hand, with an implementation that does not lock get(key, valueLoader), it runs 20 times even with sync = true. In Spring Data Redis, up to 3.3 RedisCache blocked only inside one server (JVM) with Java's ReentrantLock, and from 3.4 it leaves this job to RedisCacheWriter, whose default writer does not lock. sync = true cannot be used together with unless (IllegalStateException).
- Locking RedisCacheWriter
- Why it exists — The locking writer was added so that operations on a cache that need several Redis commands (putIfAbsent, erasing) do not overlap, and from Spring Data Redis 3.4, the "only one call computes" of @Cacheable(sync = true) is also done with this lock.
- Where it's used today — Use it together with @Cacheable(sync = true) when several servers share a Redis cache and you must prevent a cache stampede. If the cost of the lock covering the whole cache is high, block only the key that many requests rush to, separately, with a per-key distributed lock such as RLock of Redisson.
- RedisCacheWriter is the component that RedisCacheManager uses to read and write values in Redis. The default writer used by RedisCacheManager.builder(connectionFactory) (nonLockingRedisCacheWriter) does not lock. If you pass a writer made with RedisCacheWriter.lockingRedisCacheWriter(connectionFactory) to the builder, then while a value is being computed and put in, it sets a lock key in Redis with SET NX (a key made of the cache name followed by ~lock, for example product~lock), and the other calls wait, checking every 50 milliseconds by default until that key disappears. Because the lock key is in Redis, it blocks all the servers together even when there are several. But the lock covers the whole cache, not each key, so while the lock is held, requests that read other keys of the same cache also wait.
- Stale data in a cache
- Stale data in a cache is a value that stays in the cache after the value in the source has changed, so that it differs from the source. The side that uses the cache does not look at the source when there is a hit, so it receives the old value without knowing that it changed. If you also erase the value written in the cache when you change the source, the next lookup is a miss and reads the changed value again (only that one lookup is slow). Another way is to give a written-down value a validity time. The HTTP caching specification (RFC 9111, section 4.2) calls a stored response stale when it is past its determined freshness lifetime, and this word is the term of the specification for a response whose time has passed.
- @Cacheable
- This annotation stores the result of a method in a cache so that, when the method is called again with the same arguments, the value in the cache is returned without running the body. A proxy receives the call and looks up the cache first, and only when the value is not there does it run the body and then store the result. By default, the cache key is the arguments of the method, and cacheNames sets which cache the result goes into. If you write a condition in unless, a result for which the condition is true is not stored (for example, when the result is null). Because this is the work of a proxy, it is not applied when the method is called with this from within the same class.
- Redis
- A key-value store that keeps data in memory. It provides not only strings but also data structures such as lists, hashes, sets, and sorted sets. It is very fast because it works in memory, and it is used for caches, session stores, distributed locks, leaderboards, rate limiting, and more. It runs commands one after another on a single thread.
- Distributed lock
- Where it's used today — It is used to run a scheduler job that runs on several servers only once, or to put requests that arrive all at once, such as coupon issuing, into a line. In Java, libraries such as Redisson's RLock and ShedLock, for schedulers, are commonly used.
- A lock that lets only one process at a time perform a specific task among processes that run on several servers. Java's synchronized is valid only inside one server, so with several servers it is implemented by placing a "lock marker" in a store that all of them access (such as Redis). It is given an expiration time so that it is released even if the side that took it runs into a problem.
- Cache-aside pattern
- Where it's used today — It is used with Redis for data that is read far more often than it is written, such as a product detail or a member profile. Adding Spring's @Cacheable and @CacheEvict makes it work in this order.
- The most common approach, in which the application manages the cache directly. To read data, the application checks the cache first. If the data is not there, it reads from the database, stores the value in the cache, and returns it. To change data, it updates the database and deletes the matching cache entry.
- RedisTemplate
- Why it exists — When you use a Redis client (Lettuce, Jedis) directly, you have to write every time the work of getting and returning a connection and of turning Java objects into bytes. RedisTemplate takes over this work, so you only call the methods that correspond to the commands.
- Where it's used today — It is used for caches that are hard to express with @Cacheable (changing the TTL depending on a condition, or handling several keys at once), and where Redis data structures are used directly, such as counting views, limiting the number of requests, and leaderboards.
- RedisTemplate is a class provided by Spring Data Redis (RedisTemplate<K, V>) that lets Java code send Redis commands. You use the string-value commands through the ValueOperations returned by opsForValue(): get(key) reads a value (null if there is none), and set(key, value, Duration.ofSeconds(10)) stores it with a TTL. You use it after deciding the serialization method that turns keys and values into bytes, and Spring Boot creates a RedisTemplate bean whose keys and values are Object and a StringRedisTemplate bean only for strings. Unlike @Cacheable, you write in the code when to read and when to store.
Try these
- Default settings — TTL 10s. Each time the value disappears, several requests that arrive before it is filled again go to the database at the same time.
- Without a cache — Every request runs the method body and queries the database.
- Many requests and a slow database — All 40 requests that arrive from the moment the value disappears until it is filled again query the database (a cache stampede).
- Writing only sync = true — The pile-up stays. sync = true leaves the locking to the cache implementation, and the default RedisCacheWriter does not lock.
- With a locking writer — Thanks to the lock key in Redis, only one request queries the database, and the others wait for its result.
- When the source changes — Until the value in the cache disappears and is read again, requests receive the price from before the change.
- Shorten the TTL to 5s — Fewer requests receive an old value, but there are more database queries.
- Not writing entryTtl — The database is queried only when the cache is first filled, but every request after the change receives the old price.
- How it works underneath — What @Cacheable did in the proxy is written directly in the method body. The result is the same as with @Cacheable, and the same pile-up happens.
Running with the default settings
- 0.0s: The cache has no value (the first request). The request runs the method body to query the database. It takes 1s for the result to come back.
- 1.0s: The value read from the database was stored in the product cache in Redis (TTL 10s). Before it was stored, 5 requests, starting at 0.0s, all failed to find the value in the cache, so each ran the method body and queried the database. The other requests store the value again under the same key as their queries finish, so the TTL is counted from the last store (1.8s).
- 11.8s: The TTL has passed, so the value in the cache was removed from Redis. From now on, requests that arrive will not find the value in the cache.
- 11.8s: The cache has no value (right after expiration). The request runs the method body to query the database. It takes 1s for the result to come back.
- 12.8s: The value read from the database was stored in the product cache in Redis (TTL 10s). Before it was stored, 5 requests, starting at 11.8s, all failed to find the value in the cache, so each ran the method body and queried the database. The other requests store the value again under the same key as their queries finish, so the TTL is counted from the last store (13.6s).
- 23.6s: The TTL has passed, so the value in the cache was removed from Redis. From now on, requests that arrive will not find the value in the cache.
- 23.6s: The cache has no value (right after expiration). The request runs the method body to query the database. It takes 1s for the result to come back.
- 24.6s: The value read from the database was stored in the product cache in Redis (TTL 10s). Before it was stored, 5 requests, starting at 23.6s, all failed to find the value in the cache, so each ran the method body and queried the database. The other requests store the value again under the same key as their queries finish, so the TTL is counted from the last store (25.4s).
Result of this run
- From the moment the cache was empty until it was filled again (during 1s), all the requests that arrived ran the method body and went to the database. The more requests there are and the slower the database query is, the bigger the pile-up. This is a cache stampede, and @Cacheable alone cannot prevent it. In Spring, you write @Cacheable(sync = true), and if the store is Redis, you make RedisCacheManager with lockingRedisCacheWriter so that only one request queries the database. When many keys expire at the same time, give each key a slightly different TTL.
- The longer the entryTtl, the fewer the database queries, but even when the source changes, the cache keeps returning the old value for up to the TTL. How old a value you can accept is the criterion for choosing the TTL.
Code
// The import lines are omitted.
// Provided by Spring Data Redis (spring-data-redis): RedisConnectionFactory, RedisCacheManager, RedisCacheConfiguration
// Example classes we made (declarations omitted): Product, ProductRepository
// Turn the caching feature on and register as a bean a RedisCacheManager that uses Redis as the cache store
@Configuration
@EnableCaching
class CacheConfig {
// connectionFactory: a bean that Spring Boot creates from the Redis connection settings (spring.data.redis.host and so on)
@Bean
RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
// A value in the product cache disappears from Redis 10 seconds after it is stored
RedisCacheConfiguration product = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(10));
// The default writer used by builder(connectionFactory) does not lock
return RedisCacheManager.builder(connectionFactory)
.withCacheConfiguration("product", product)
.build();
}
}
// An example product service we made
@Service
class ProductService {
private final ProductRepository productRepository;
ProductService(ProductRepository productRepository) {
this.productRepository = productRepository;
}
// Look for a value with key id in the product cache, and only when there is none, run the body and store the result
@Cacheable(cacheNames = "product", key = "#id")
public Product getProduct(long id) {
// Database query (1s)
return productRepository.findById(id).orElseThrow();
}
}
Assumptions of this model
Only one key (one popular product) is handled, and requests are assumed to arrive at regular intervals. The eviction policy, which removes keys when the cache is full, is not covered here. The code on the screen (CacheConfig and ProductService) was compiled as it is and really run for 30 seconds per combination on Spring Boot 4.1.1, Spring Data Redis 4.1.1 (Lettuce 7.5.2), Redis 8.10.2, and MySQL 8.4.11, and for every single request, whether it was served from the cache, ran the body and queried the database, or waited for the query of another request, and the price it received, matched the model (verify/spring/cache_run_probe.out.txt). Only 1, 5, and 20 requests per second were run. Product, ProductRepository, and the RedisTemplate bean, whose declarations are omitted on the screen, were written separately for the run, and the database query time was produced with SLEEP in MySQL. The query was made to finish 25 milliseconds earlier than the value on the screen (0.975s for 1s). This is because, if the query takes exactly 1s, the moment the value is stored and the moment the next request arrives coincide and their order changes from run to run; the model computes that moment as the store finishing first. For the same reason, the UPDATE that changes the price was sent 15 milliseconds before 5s. Because the verification environment was busy with other jobs, runs in which a late request or a late store changed that order were discarded and only those combinations were run again (16 of 234). With the locking writer, every waiting request saw the lock key product~lock in Redis, and the waiting requests received the value within 44 milliseconds after the request that queried returned (by default, the writer checks again every 50 milliseconds whether the lock key has disappeared). While the lock was held, a call that looked up a different product whose value was in the cache also waited until the query finished. In the configurations without entryTtl, the remaining time to live of the key was -1 (never expires). When 20 threads called with the same key at the same time on an empty cache, @Cacheable ran the body 20 times, sync = true with ConcurrentMapCacheManager ran it 1 time, and even with sync = true, a cache implementation that does not lock get(key, valueLoader) ran it 20 times (spring-context 7.0.9, verify/spring/cache_probe.out.txt). That the default writer stopped locking in Spring Data Redis 3.4 follows the official documentation, and the version that was run is 4.1.1.
Previous lab: Load balancers and scale out · Next lab: Redis single-threaded execution