# Refresh Ahead: Reload Popular Cache Items Before They Expire

> Refresh ahead is a caching strategy that reloads a busy item from the database shortly before it expires, so readers rarely wait.

**In short:** Refresh ahead is a caching strategy that reloads a busy item from the database shortly before it expires, so readers rarely wait.

From "System design", a free course by Venkat R. Murty: https://designervenkat.online/learn/system-design/refresh-ahead

## What You Will Learn

Caches make systems fast, but a cached copy does not last forever. When a popular copy expires, the next reader often waits for a slow database read. Refresh ahead is one way to avoid that wait. In this lesson you will learn how it works, follow one example step by step, see what it gains and what it risks, and learn when a team should pick it over a simpler method. You do not need to write any code to follow along. The short sample later only shows the idea in a few lines.

- **Cache:** A fast, temporary store, often kept in memory, that holds a copy of data so the system does not have to fetch it again every time.
- **Database:** The lasting store where a system keeps its records. Reading from it is slower than reading from a cache.
- **TTL (time to live):** How long a cached copy may stay in the cache before it expires and must not be used.
- **Cache miss:** A request that finds no usable copy in the cache, so it must go to the database.
- **Hot item:** A piece of data that many people ask for in a short time, such as the top story on a news site.
- **Latency:** The time one request takes from the moment it starts until the answer arrives.

## Why Do Caches Pause When Items Expire?

The most common pattern is called cache-aside. The application checks the cache first. If the copy is there, it uses it. If not, it reads the database, returns the answer, and stores a copy for next time. Reads that hit the cache are quick. The trouble starts when a copy expires. The next reader gets a cache miss and waits for the database. Under cache-aside, some requests can end up waiting on that database read, so the slowest responses are often the ones that found no copy.

For a rarely read item, one slow read is no big deal. For a hot item, the problem grows. If thousands of people open the same page every minute, many of them may arrive right after the copy expires. Refresh ahead is designed for exactly that moment. It tries to replace the copy before it expires, so the application does not feel the impact of a slow read during the reload.

## How Does Refresh Ahead Work?

Refresh ahead adds one rule on top of the normal TTL. The team sets a refresh window, which is the last part of the copy's life. If someone reads the item inside that window, the cache starts a reload from the database in the background. The reader still gets the copy that is already stored. When the reload finishes, the new copy takes its place and the TTL starts again.

1. **Set the TTL:** Choose how long a copy may live. For example, a list might live for 60 seconds.
2. **Set the window:** Choose the final slice of that life where a read starts a reload. Oracle's example uses a refresh-ahead factor of 0.75.
3. **Serve normal reads:** While the copy is young, reads simply return the stored copy. Nothing extra happens.
4. **Read inside the window:** The cache returns the stored copy right away and starts a background reload from the database. The reader does not wait for that reload.
5. **Swap in the new copy:** When the reload ends, the new copy replaces the old one, and its clock starts over.

Oracle's Coherence caching guide describes this same idea. A refresh-ahead setting of 0.75 on an entry with a one-minute expiry means that an entry read in its last fifteen seconds is reloaded in the background.

Refresh ahead is not the same as a read that waits. The point is that the reader never pays for the reload. A background reload is a job that runs beside the request, not inside it.

### How Do You Pick the Window Size?

Oracle's example uses a refresh-ahead factor of 0.75, so the window starts at 75 percent of the TTL. The formula is simple.

$$
t_{\text{refresh}} = 0.8 \times \text{TTL}
$$

In words, the refresh can start at 0.8 times the TTL. For a 60-second TTL, that is 48 seconds into the copy's life.

A wider window reloads more items early, which costs more database reads. A narrower window saves reads, but a reader who arrives after the copy expires still has to wait. The right size depends on how often the data changes and how many people read it.

> **Tip:** If many items are loaded at the same moment, they will also expire at the same moment.

## Worked Example: Top Stories on a News Homepage

A news site shows a Top Stories list on its homepage. Thousands of readers open the page every minute, so the list is a hot item. The team sets the TTL to 60 seconds and the refresh window to the last 10 seconds of that life. Here is what happens, step by step.

1. **Load the list:** At the start, the list is read from the database and stored in the cache with a fresh 60-second TTL.
2. **Serve quietly:** For the next 50 seconds, every reader gets the stored list. The window has not started, so no reload runs.
3. **Read inside the window:** At second 52, a reader opens the page. The copy is 52 seconds old, which is inside the last 10 seconds. The reader gets the stored list at once, and the cache starts a background reload.
4. **Swap the copy:** When the reload finishes, the newer list replaces the old one. Readers keep getting a stored copy the whole time.
5. **Result:** No reader sees an empty cache, so no reader waits on the database for the list.

The code below is a small simulation of the same rule. It uses fixed times instead of a real clock, so the output is always the same. A real site would run the reload on a separate worker. This demo runs the reload inside the read function to keep the code short.

```python
class Entry:
    def __init__(self, value, stored_at):
        self.value = value
        self.stored_at = stored_at

TTL = 60
WINDOW = 10

def read(cache, key, now, db):
    entry = cache.get(key)
    if entry is None or now - entry.stored_at >= TTL:
        value = db(now)
        cache[key] = Entry(value, now)
        return value, 'miss'
    age = now - entry.stored_at
    if age >= TTL - WINDOW:
        cache[key] = Entry(db(now), now)
        return entry.value, 'hit+refresh'
    return entry.value, 'hit'

db_calls = []

def db(now):
    db_calls.append(now)
    return 'top-stories@%d' % now

cache = {}
print(read(cache, 'top', 0, db))
print(read(cache, 'top', 30, db))
print(read(cache, 'top', 55, db))
print(read(cache, 'top', 100, db))
print(len(db_calls))

```

Output:

```text
('top-stories@0', 'miss')
('top-stories@0', 'hit')
('top-stories@0', 'hit+refresh')
('top-stories@55', 'hit')
2
```

Read the output line by line. The first read is a miss, so the database is used. The read at 30 seconds is a normal hit. The read at 55 seconds falls in the window, so it returns the old copy and reloads a new one. The read at 100 seconds gets the newer copy. The database was called only twice, once for the first load and once for the reload.

## What Does Refresh Ahead Gain and Risk?

- **Shorter waits for popular items:** Readers of a hot item rarely pay for a reload inside their own request, because the reload runs in the background before the copy expires.
- **Steady freshness:** A popular item is replaced on a regular schedule, so the copy most readers see is recent.
- **Wasted reads when guesses are wrong:** The cache must guess which items will be read again. Each wrong guess sends a database read that nobody needed.

Oracle's guide makes a clear point about the trade. Refresh ahead lowers latency compared with a plain read-through cache, but only when the cache can predict accurately which items will be needed. With perfect predictions it adds no overhead. The more often the cache guesses wrong, the more unneeded requests reach the database.

Refresh ahead therefore pays off only when the access pattern is steady. If a hot item keeps being read, the early reload is useful. If it is read once and then forgotten, the early reload is pure cost.

### What Happens When a Guess Goes Wrong?

Imagine a cache that reloads every item in a large catalog just before expiry. Only a few of those items are read again before the next reload. Most of the reloads are database reads that give nothing back. A poor guess can leave the database busier than a plain cache would.

A second risk is a burst of reloads. If a group of hot items was loaded at the same moment, they may all need reloading together. That sends a flood of database reads at once, which is the very load the cache was meant to protect against.

Refresh ahead also adds code. The team must run background jobs, handle reload failures, and avoid reloading the same item twice. Deciding which items deserve a refresh is not simple, and a bad rule can waste more work than it saves.

## Refresh Ahead Compared With Cache-Aside

Refresh ahead is usually not a full replacement for cache-aside. It is often an extra layer added to a small set of keys, while the rest of the system keeps the simple path. The list below gives a short comparison. The full details of each write and read strategy are covered in other lessons.

- **Cache-aside:** The first request after expiry waits for the database. The code is simpler, and the cache only fills when someone asks for an item.
- **Refresh ahead:** A popular item is reloaded before it expires, so most readers never see a miss. The code is more complex, and the database may get reads that nobody uses.

The first read of data that is not yet cached is usually the slow one. Refresh ahead tries to remove that slow read for items that the cache already knows are popular. For items that are rarely read, plain cache-aside is usually the better choice because it avoids extra reloads.

## When to Use Refresh Ahead

- **Read-heavy hot data:** Trending lists and popular product pages are read far more often than they change. These items benefit most from a cache that already holds them.
- **Predictable access:** Items that people read in a regular pattern give the cache a good chance to guess right.
- **Skip rare items:** Items read once a day or less should stay on the plain path. Reloading them early only adds database work.

> **Tip:** Before turning refresh ahead on, measure two things for each item: how often it is read and how often it is reloaded. If many reloads are never read again, switch that item back to plain cache-aside.

The strategy fits data that many people read and that changes often enough to need a regular reload. It fits poorly when access is random, when items are rarely reused, or when the team cannot watch the cost of reloads.

## Check Your Understanding

Try these two short questions. Each one asks you to apply the idea to a new case.

**Quick check:** A shop caches each product page for 10 minutes and uses refresh ahead. Most products are viewed once a day, and a few are viewed thousands of times an hour. Which products should benefit, and what goes wrong if every product is refreshed?

A. Every product benefits, because a background reload is always cheaper than a cache miss.
B. The few products viewed thousands of times an hour benefit. Refreshing every product spends database reads on pages that are rarely asked for again.
C. The products viewed once a day benefit most, because they are the ones that expire.
D. No product benefits, because refresh ahead only works for pages that never change.

**Answer:** B. Refresh ahead pays off when the cache guesses right. A hot page is read again soon after its reload, so the early reload saves a waiting reader. A page read once a day is reloaded for nobody, and refreshing every page turns those reloads into wasted database reads.

**Quick check:** A cache uses refresh ahead for search suggestions. Each suggestion is searched in a short burst by one person, and most suggestions are not searched again within the hour. What should the team expect?

A. Shorter waits for every search, because any reload is useful.
B. More database reads with little saved waiting, because most early reloads are for items nobody reads again.
C. No change at all, because the cache ignores items that are not read again.
D. The database will never be called, because the cache keeps everything fresh.

**Answer:** B. The cache is guessing that these suggestions will return, and the guesses are mostly wrong. Each early reload costs a database read, and the reads save little waiting because the reloaded copies are rarely used.

## Key Takeaways

Refresh ahead reloads a recently read item from the database shortly before its TTL ends, so readers keep getting a stored copy and rarely wait. It needs a clear TTL and a refresh window, and it works best for a small set of hot, predictable items. Its cost is extra database reads when the cache guesses wrong, so measure the reloads before you rely on it.

## Sources

- [Oracle Coherence 3.1 User Guide: Read-Through, Write-Through, Refresh-Ahead and Write-Behind Caching](https://docs.oracle.com/cd/E14008_01/coh.310/coh31ug/read_through.htm)
- [Devtrovert: 6 Cache Strategies to Save Your Database's Performance](https://blog.devtrovert.com/p/6-cache-strategies-to-save-your-databases)
- [The New Stack: 6 Caching Strategies: Latency vs. Complexity Tradeoffs](https://thenewstack.io/6-caching-strategies-latency-vs-complexity-tradeoffs/)
- [Codemia: Caching Strategies, Cache-Aside, Write-Through, and Write-Behind](https://codemia.io/courses/system_design_fundamentals/caching_strategies)
- [TechMarcos: Redis Caching Strategies Every Backend Developer Needs](https://www.techmarcos.com/redis-caching-strategies-production/)
- [Medium (DevDomain): Caching Strategies Explained: Cache-Aside, Write-Through, and Write-Behind](https://medium.com/devdomain/caching-strategies-explained-cache-aside-write-through-and-write-behind-5c58bf253819)
- [EnjoyAlgorithms: Caching Strategy: Refresh Ahead Pattern](https://www.enjoyalgorithms.com/blog/refresh-ahead-caching-pattern/)
- [DEV Community: Refresh-Ahead Caching with Redis](https://dev.to/moose978/refresh-ahead-caching-with-redis-3j08)
- [Wittycoder: Cache-Aside, Write-Through, Write-Behind, Refresh-Ahead](https://wittycoder.in/courses/caching/cache-strategies)
- [bool.dev: Mastering Caching: Strategies, Patterns & Pitfalls](https://bool.dev/blog/detail/mastering-caching-strategies-patterns-pitfalls)
