Quick revision
The short answer
In cache-aside, the application reads Redis, loads from the database on a miss and writes the result into the cache, which is what Spring's @Cacheable does. Read-through moves that loading into the cache layer through a loader, with similar consistency. Write-through writes the cache and the database together, so reads are fresh but writes are slower and a failure between the two writes can leave them inconsistent. In cache-aside a reader loading old data can race with a writer's eviction and leave a stale entry until it expires, which is why every pattern should set a TTL to bound staleness.
What to remember
In this answer, we compared three caching patterns with Redis. Cache-aside, which @Cacheable and @CacheEvict implement, loads on a miss and evicts on a write, and can be left stale when a slow reader re-caches old data after an eviction. Read-through moves loading into the cache with similar consistency, and write-through keeps reads fresh at the cost of slower writes and inconsistency when one of its two writes fails. A TTL on every entry bounds how long any of these failures can show stale data.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
In cache-aside, the application reads Redis, loads from the database on a miss and writes the result into the cache, which is what Spring's @Cacheable does. Read-through moves that loading into the cache layer through a loader, with similar consistency. Write-through writes the cache and the database together, so reads are fresh but writes are slower and a failure between the two writes can leave them inconsistent. In cache-aside a reader loading old data can race with a writer's eviction and leave a stale entry until it expires, which is why every pattern should set a TTL to bound staleness.
Why this question is asked
Adding Redis in front of a database is one of the quickest ways to make a Spring Boot service faster, and one of the quickest ways to start showing users wrong data. The three classic caching patterns differ in who loads the cache, who writes it, and therefore in how stale it can get. Interviewers ask about them because choosing well depends on understanding the failure cases, not just the happy path. In this answer, we will recap what a cache is for, walk through cache-aside with @Cacheable, then read-through and write-through, look at the race that makes cache-aside stale, and finish with TTLs as the safety net for all three.
1A Quick Recap of Caching and Staleness
A cache keeps a copy of data in fast storage, here Redis, so that most reads avoid the slower database. The database stays the source of truth. The moment we have a copy, we have a consistency question: after the database changes, how long can the cache still return the old value? That old value is called stale.
Each caching pattern is a set of rules for when the copy is created and when it is updated or removed. Those rules decide how stale data can get and which failures leave it stale. A TTL, time to live, is an expiry time on each entry; after it, the entry disappears and the next read loads a fresh copy.
2Cache-Aside With @Cacheable
In cache-aside, also called lazy loading, the application is in charge. On a read it looks in the cache; on a hit it returns the cached value; on a miss it reads the database, puts the result into the cache, and returns it. On a write it updates the database and evicts the cache entry, so the next read reloads it. Spring's caching abstraction implements exactly this with annotations.
Cache-aside is simple and resilient: if Redis is down, the method can still read the database. Its consistency weakness is that the cache and the database are written by separate steps, which leaves room for races, as we will see.
3Read-Through and Write-Through
In read-through, the cache itself knows how to load missing entries. The application only ever asks the cache; on a miss, the cache calls a loader that reads the database. Caffeine's LoadingCache works this way, and some Redis client libraries offer it. From a consistency point of view it behaves like cache-aside: entries are loaded on a miss and can go stale when the database changes, unless writes evict them.
In write-through, every write goes to the cache and the database together, typically the cache layer writes the database synchronously and then updates its own copy. Reads are then fresh, because the cache is updated on every write rather than emptied. The costs are slower writes, since each one does two writes, and a new failure mode: if the database write succeeds and the cache write fails, or the reverse, the two disagree, and nothing repairs it until the entry expires or is written again.
| Pattern | Who loads and writes | Main consistency problem |
|---|---|---|
| Cache-aside | app reads DB on miss, evicts on write | race between a slow reader and an eviction |
| Read-through | cache loader reads DB on miss | same as cache-aside: stale after DB changes |
| Write-through | every write updates DB and cache | a failure between the two writes |
| All three | stale until TTL if an update is missed |
4The Race That Leaves Cache-Aside Stale
Here is the classic interleaving. A reader misses the cache and reads price 10 from the database. Before it writes the cache, a writer updates the price to 12 and evicts the entry. Then the reader, which was slow, puts its old value 10 into the cache. The eviction has already happened, so nothing removes the stale 10.
7 messages in order. Play them, or step through them one at a time.
The eviction ran before the reader's put, so the old value wins.
5Bounding Staleness With TTLs
Because every pattern has some way to miss an update, every cache entry should have a TTL. It turns a stale entry that would live forever into one that lives at most a known time. In Spring Boot with Redis we set it in configuration, globally or per cache, and pick the value from how stale the business can tolerate: perhaps 10 minutes for a product description, 30 seconds for a price.
The TTL is the safety net, not the main mechanism. Evictions keep the cache fresh in the normal case; the TTL guarantees that the race above, a failed write-through, or a forgotten eviction in some code path can only cause staleness for a bounded time.
6Follow-up questions
Is it better to evict or to update the cache on a write?
Usually evict. Writing the new value into the cache on every update can store a value from a transaction that later rolls back, and two concurrent writers can put their values in the opposite order of their commits, leaving the older one in the cache. Evicting avoids both, because the next read loads whatever is committed. Updating makes sense with write-through, where the cache layer controls the order of writes.
How do you reduce the cache-aside race?
Keep TTLs short for data where staleness matters, evict after the database commit rather than before it, and for very sensitive keys use a delayed second eviction a short time after the write, which removes a value re-cached by a slow reader. Versioned values, where the cache only accepts a value newer than the one it holds, close the race completely but cost more complexity.
What about write-behind?
Write-behind, or write-back, updates the cache first and writes the database asynchronously later. It gives very fast writes but risks losing data if the cache fails before the database write, so it is rare for business data in Spring services.
7Conclusion
In this answer, we compared three caching patterns with Redis. Cache-aside, which @Cacheable and @CacheEvict implement, loads on a miss and evicts on a write, and can be left stale when a slow reader re-caches old data after an eviction. Read-through moves loading into the cache with similar consistency, and write-through keeps reads fresh at the cost of slower writes and inconsistency when one of its two writes fails. A TTL on every entry bounds how long any of these failures can show stale data.
Open with
Cache-aside means the application manages the cache. On a read it checks Redis, on a miss it loads from the database and puts the result in the cache, and on a write it updates the database and evicts the entry.
Then, if they want more
That is exactly what Cacheable and CacheEvict do in Spring. Read-through is similar, except the cache itself has a loader that fetches missing entries, so the application only talks to the cache; consistency-wise it behaves like cache-aside.
Write-through means every write updates the database and the cache together, so reads are fresh, but writes are slower and if one of the two writes fails they disagree until the entry is rewritten or expires.
The best-known problem with cache-aside is a race: a reader misses, reads the old value from the database, then a writer updates and evicts, and only then the slow reader puts the old value into the cache, so it stays stale. I would reduce that by evicting after commit and keeping TTLs short for sensitive data like prices.
And in every pattern I would always set a TTL, globally and per cache in the Redis cache manager, because the TTL is what bounds staleness when any update is missed, whether it is the race, a failed write or a code path that forgot to evict.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Cloud documentation, Resilience4j documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain cache-aside, read-through and write-through caching, and which consistency...": https://scrivio-interview-library.netlify.app/spring-boot/distributed-data/1899f3e519/