esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

Caching: Keeping Copies of Data Close So Repeat Requests Are Fast

Learn what a cache is, how an app checks it before the database, and how to keep cached copies from going stale.

In short

Caching means keeping a copy of often-requested data in a place that is faster to reach, so repeat requests are answered quickly.

11 min readFree

What You Will Learn

Caching means keeping a copy of data that people ask for often, in a place that is faster to reach than the original. The copy lets a system answer a repeat request without doing slow work again, such as running the same database query. In this lesson you will see why caching makes systems faster, where caches sit in a typical system, how an app reads through a cache, and what caching costs in freshness and extra complexity.

Key words

Cache
A small, fast store that holds copies of data so later requests can be answered quickly.
Latency
The time one request takes to get its answer. Lower latency means a faster page.
Backend
The servers and databases behind an app. Users do not see them directly.
Cache hit
A request whose data is already in the cache.
Cache miss
A request whose data is not in the cache, so the system must fetch it from the original source.
Hit rate
The share of all requests that were cache hits.
Stale data
A cached copy that no longer matches the original, because the original changed.

Why Does Caching Help?

Slow work is often the same work repeated. Imagine a thousand visitors asking for the same product page. Without a cache, each visit may run the same database query. With a cache, the first visit does the query and the rest read a saved answer. Users wait less, and the backend, which is the set of servers and databases doing the work, has less to do.

  • Faster answers

    A cache usually lives in RAM, the computer's fast working memory. A hit can return straight from memory, which is much quicker than running a query.

  • Less load on the backend

    A hit skips the database completely, so it adds no work there. The database then has more room for requests that truly need it.

  • Best for repeat reads

    Caching pays off when many people read the same data. Recently used items tend to be used again soon, so a cache keeps the likely ones close.

Two words describe what happens on each request. A cache hit means the data was already stored. A cache miss means it was not, so the system must go to the slower original source, such as the database, and fetch it.

hit rate=hitshits+misses\text{hit rate} = \frac{\text{hits}}{\text{hits} + \text{misses}}

In words: the hit rate is the number of hits divided by all requests, which is hits plus misses. If 900 of 1,000 requests are hits, the hit rate is 0.9, or 90 percent. A good cache aims for a high hit rate, so most requests stay fast.

Tip

A high hit rate is good, but it is not the whole story. A cache can have a great hit rate and still serve old data. Check both speed and freshness before you call a cache a success.

Where Do Caches Live?

A request can be answered at several layers. The closer a copy is to the user, the less travel is needed to reach it. Caches appear in five common places.

  • Browser or device

    The user's own browser can keep a copy of a page or image, so a repeat visit may need no network trip at all.

  • CDN

    A content delivery network is a group of servers spread across many places. It keeps copies of files near users. Other lessons cover CDNs in detail.

  • Web server

    A web server can keep a response it built recently, so it does not rebuild the same page for every visitor.

  • Application layer

    The app's own code can check an in-memory store, such as Redis or Memcached, before it touches the database.

  • Database

    Databases also keep hot data in memory.

The layer this lesson focuses on is the application layer, because that is where most teams add a cache by hand. You decide what goes in, what key it uses, and how long it lives.

Read the layers from the outside in. A request stops at the first layer that holds a usable copy. Each miss pushes the request one layer deeper, and the database is the deepest and slowest place to answer it.

What Is Cache-aside, the Basic Read Pattern?

Cache-aside, also called lazy loading, is the most common way an app uses a cache. The app does the checking and filling itself. The cache does not load data on its own. The steps below run on every read.

  1. 1

    Check the cache

    The app looks up a key in the cache. A key is a label, such as product:42, that points to one stored value.

  2. 2

    Return a hit

    If the value is there, the app returns it right away and never touches the database.

  3. 3

    Query on a miss

    If the value is missing, the app reads the database to get the answer.

  4. 4

    Store and return

    The app saves that answer in the cache with an expiry time, then returns it to the user.

Only data that someone actually asked for ends up in the cache, so it stays small. The cost is that the first reader of an item waits longer. A miss takes three trips: a request to the cache, a query to the database, and a write back to the cache.

Redis and Memcached in Short

Redis and Memcached are two popular in-memory stores used as caches. Both keep data in memory and find it by key. They differ in what they can hold.

Tip

Both tools are used as caches in many systems. Choose based on the kind of data you store and whether you can afford to lose it after a restart.

What Is the Cost of Keeping Copies Fresh?

A cached copy is only as good as its match with the original. If the database changes but the cache does not, readers see stale data. So caching trades some freshness for speed. Three tools help control that risk.

  • TTL (time to live)

    A timer on each cached item. When the timer ends, the item expires, and the next read goes back to the database.

  • Explicit delete on update

    When the app changes a record, it also deletes the matching cache key. The next read then misses and loads the new value.

  • Eviction

    A cache has a memory limit. When it is full, something must leave to make room. The rule that picks what leaves is the eviction policy.

How Do TTL and Eviction Work Together?

Key words

TTL
Time to live. A fixed age after which a cached item expires.
Eviction
Removing items from a full cache so new items have room.
LRU
Least recently used. The rule that removes the item untouched for the longest time.
LFU
Least frequently used. The rule that removes the item asked for the fewest times.

Redis lets you choose an eviction policy, so it removes keys by your rule once memory passes the limit. LRU is the most common eviction policy.

Eviction and TTL do different jobs. Eviction keeps the cache within its size. TTL keeps any one copy from living too long. Many teams set both, so the cache stays small and its copies stay reasonably fresh.

Tip

Pick a TTL close to how old the data may safely be. A price may need seconds of freshness, while a profile photo may tolerate hours.

Worked Example: An Online Shop Product Page

Picture the product page of an online shop. Thousands of shoppers view the same popular item. The app keeps the product's details and price in Redis. Follow one request step by step.

  1. 1

    Ask Redis first

    The app asks Redis for the product's details and price, using a key such as product:42.

  2. 2

    Serve a hit from memory

    If Redis has the item, the app builds the page from it. The database is not touched, and the shopper gets a fast answer.

  3. 3

    Query the database on a miss

    If Redis does not have it, the app queries the database for the product's current details and price.

  4. 4

    Save with a 5-minute TTL

    The app stores the result in Redis with a 5-minute expiry. Later shoppers can now get a hit for the next five minutes.

  5. 5

    Return the page

    The app sends the page to the shopper. Most shoppers viewing the popular item now get the memory copy.

Redis uses a number of seconds, so a 5-minute TTL is 300 seconds. The line below shows the idea, but it is not run here.

Text
redis-cli SET product:42 '{"name":"Desk lamp","price":19.99}' EX 300

The next block copies the same idea in miniature. It uses a plain dictionary as the cache and a fake database function that counts its calls. For brevity it skips the expiry. Run it and notice that only the first request for each product reaches the database.

Python
cache = {}
db_calls = 0
hits = 0
requests = 0

def query_db(product_id):
    global db_calls
    db_calls += 1
    return {'id': product_id, 'price': 19.99}

def get_product(product_id):
    global hits, requests
    requests += 1
    if product_id in cache:
        hits += 1
        return cache[product_id], 'hit'
    value = query_db(product_id)
    cache[product_id] = value
    return value, 'miss'

for pid in ['p1', 'p1', 'p2', 'p1']:
    value, result = get_product(pid)
    print(pid, result, value['price'])

print('database queries:', db_calls)
print('hit rate:', hits / requests)

Output

p1 miss 19.99
p1 hit 19.99
p2 miss 19.99
p1 hit 19.99
database queries: 2
hit rate: 0.5

The output shows four requests and two database queries. Two requests were hits, so the hit rate is 0.5. In a real shop with many visitors, the same pattern gives a far higher hit rate.

Common Mistakes to Avoid

  • Caching data that changes every second

    A copy that goes wrong almost at once gives little benefit and confuses users. Cache data that changes slowly or that many people read.

  • Forgetting the expiry

    Without a TTL or a delete rule, a wrong copy can stay for a very long time. Give most keys an expiry.

  • Racing a write

    A read that starts before an update and finishes after the delete can put the old value back in the cache. A short TTL limits how long that old copy lasts.

  • Sizing from the whole data set

    Size the cache for the hot data, the items people read most, rather than for every record you own.

Check Yourself

Use what you learned to answer these short scenarios. Read each one, then choose the option that fits the idea, not just the words.

Quick check

A news site's homepage is slow because every visit runs the same heavy database query. The headlines change only every few minutes. What is the best plan?

Quick check

A shop's cache answered 900 requests from memory and sent 100 requests to the database. What is the hit rate?

Quick check

A shop changes the price of a lamp. The product page uses a cache with a 10-minute TTL, so some shoppers still see the old price. What should the team change so the next reader sees the new price?

Key Takeaways

  • Cache repeat reads

    Caching pays off when the same data is requested often. A hit saves time and backend work, and a miss falls back to the source.

  • Check the cache first

    In cache-aside, the app checks the cache, queries the database on a miss, and stores the answer for later readers.

  • Plan for freshness

    Use TTLs, delete keys when data changes, and choose an eviction rule such as LRU. Each copy you keep trades some freshness for speed.

Sources

How is this lesson?

Last updated on 17 November 2026

Learn with me

Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.