Database Caching: Keep Copies of Repeated Reads in Fast Memory
Learn where database caching lives, why it cuts load, how to keep cached copies correct, and how to judge what is worth caching.
In short
Database caching keeps copies of repeated reads in fast memory, which cuts load on the database and speeds up answers.
11 min readFree
What You Will Learn
Most apps read data far more often than they change it. A product page, a news homepage or a user profile may be loaded thousands of times, while its price or headline changes only now and then. Database caching means keeping the result of a frequent or expensive read in a faster place, so the next visitor does not make the database do the same work again.
In this lesson you will learn why caching helps, where database caching lives, how to keep cached copies correct, and how to judge what is worth caching. You will follow one worked example from an online shop, then try two short scenarios.
Key words
- Database
- The system that stores your data, usually on disk, and answers questions about it.
- Read and write
- A read asks the database for data without changing it. A write saves a change to the data.
- Disk
- Long-term storage. It holds data even when power is off, but it is slower to read than memory.
- RAM
- A computer's fast working memory. It is quicker to read than disk but loses its contents when power is off.
- Latency
- How long one request takes, from the moment it starts to the moment the answer arrives.
- Cache
- A small, fast store that holds copies of data so that repeat requests can be answered sooner.
- Cache hit
- The answer is already in the cache, so it is returned at once.
- Cache miss
- The answer is not in the cache, so the app must fetch it from the database.
- In-memory store
- A data program that keeps its data in RAM, such as Redis or Memcached.
- Buffer pool
- A memory area inside a database that holds recently used pages of its tables and indexes.
- TTL
- Time to live. A timer on a cache entry. When it runs out, the entry is removed.
- Stale
- Old data that no longer matches the original data it was copied from.
- Invalidation
- Removing or updating a cached copy when the original data changes.
Why Does Caching Help?
A database keeps its files on disk. When the same question arrives again and again, a database that goes to disk every time repeats the same slow work. A copy kept in memory answers the same question faster and leaves the database free for other jobs.
Reads outnumber writes
In many apps, people look at a page far more often than anyone edits what is on it. Repeated reads are the best target for a cache.
Memory answers faster
Reading memory is quicker than reading disk, so a copy held in memory returns sooner. The user sees the page load faster.
Less load on the database
Each cache hit is a query the database does not have to run. Load means the amount of work a server must do, so less load leaves room for saving changes.
Caching is a trade-off, not a free gift. A copy can go stale. After the original changes, the cache may keep showing the old value until the copy expires or is removed.
What Are Latency and Load?
Caching aims at two numbers. The first is latency, the time one request takes. The second is load, the work the database must do. A good cache lowers both. The price is that some visitors may see data that is a little old, so you must decide how old is acceptable.
Where Does Database Caching Live?
Database caching can sit in two places. One place is inside the database itself. The other is outside it, in a separate in-memory layer that the application checks before it asks the database.
What Caches Live Inside the Database?
The most common built-in cache is the buffer pool. It keeps copies of table and index pages in RAM, so the database can read them without going to disk. The database manages it on its own, and you tune its size. Databases also keep query plans, which are the step-by-step recipes for running a query, so a repeated query can skip some planning work.
Some databases once had a query cache, which stored the final result of an identical query. The MySQL query cache was deprecated and then removed in MySQL 8.0. One source says it often hurt more than it helped. A materialized view is a newer kind of stored result: it keeps a precomputed query answer that is refreshed on a schedule.
What Is an In-memory Layer in Front?
An external cache such as Redis or Memcached is a separate program that holds data in RAM. The application asks it first. If the answer is there, the database is never touched. If not, the application runs the query and stores the result for next time. The buffer pool cannot do this job, because it only speeds up how the database reads its own pages. An external cache can also hold finished objects, such as a whole product record.
Tip
Before you add an outside cache, ask two questions. Is the database really the bottleneck? Would a larger buffer pool already fix it? A well-sized buffer pool is often the cheapest first step.
Keeping the Cache Correct
A cache is only useful when its copies are correct enough for the job. Three tools help: expiry, invalidation and a sensible size limit. The next two sub-topics cover the first two.
Expiry With a TTL
A TTL, or time to live, is a timer attached to each cache entry. When the timer runs out, the entry is removed, and the next read fetches fresh data from the database. The stale window is never longer than the TTL. Redis lets you set this when you store a key, as the commands below show.
SET product:42 <product data> EX 300
TTL product:42The EX option sets the expiry in seconds, so 300 means 5 minutes. Redis removes expired keys on its own, though not at the exact instant they expire.
Choosing a TTL is a trade-off. A short TTL keeps data fresh but causes more misses. A long TTL gives more hits but more old answers. A research test on web applications found the same pattern: longer TTLs raised the hit ratio and also raised the rate of stale reads.
Invalidation When Data Changes
A TTL alone is simple, but some data must look fresh the moment it changes. For that, the application removes the cached copy when the original changes. The steps below use the common delete-on-write idea.
- 1
Change the original first
The application writes the new value to the database. The database stays the source of truth.
- 2
Remove the old copy
After the write succeeds, the application deletes the cached key. Deleting is usually safer than overwriting, because an old read still in flight could put a stale value back.
- 3
Let the next read refill
The next request misses, reads the fresh row from the database, and stores it again with a TTL.
- 4
Keep the TTL as a backstop
If a delete is ever lost, the TTL still limits how long the stale copy can survive.
Deciding What Is Worth Caching
Caching is not free. Each cached entry takes memory, adds a trip across the network and may go stale. So cache on purpose, for data that gives a real payoff. Sort each candidate into one of the groups below.
Hot data
Items that many people request again and again, such as a popular product, gain the most from a cache.
Read-heavy and rarely changing
Data that is read often but written seldom, such as a product description, is a strong candidate. It works best when a few minutes of age is acceptable.
Expensive to build
Results of slow joins or heavy calculations are worth keeping, because each miss costs a lot of database work.
Not worth caching
Data that changes as often as it is read, data unique to one user, and data read only once gain little. A cache adds risk without much benefit.
Tip
A stampede happens when a busy key expires and many requests miss at the same moment. All of them then hit the database together. A common fix is to let one request rebuild the value while the others wait.
Worked Example: An Online Shop Product Page
Picture a product page in an online shop. The name, price and description are read thousands of times a minute, but an editor changes them rarely. The shop stores them in Redis, following these steps.
- 1
First visitor arrives
The app looks for the key product:42 in Redis and finds nothing. This is a cache miss.
- 2
Run the query once
The app asks the database for the name, price and description of product 42, and gets the row back.
- 3
Store the copy with a TTL
The app saves the row in Redis under product:42 with a 5-minute TTL, which is 300 seconds.
- 4
Later visitors get hits
For the next 5 minutes, each visit finds the key and returns the data from memory. The database runs no query for those visits.
- 5
Handle a price change
When an editor changes the price, the app deletes product:42. Even if that delete were missed, the old price would vanish within 5 minutes.
The program below is a small stand-in for the cache flow. It uses a fake clock and a counter for database queries, so its output is always the same.
db_queries = 0
def run_query(product_id):
global db_queries
db_queries += 1
return {'name': 'Blue Mug', 'price': 12}
cache = {}
TTL = 300
def get_product(product_id, now):
key = f'product:{product_id}'
entry = cache.get(key)
if entry is not None and entry[1] > now:
return entry[0], 'hit'
value = run_query(product_id)
cache[key] = (value, now + TTL)
return value, 'miss'
print(get_product(7, 0)[1])
print(get_product(7, 60)[1])
print(get_product(7, 299)[1])
print(get_product(7, 300)[1])
print(db_queries)
Output
miss hit hit miss 2
The first call misses and runs the query. The calls at 60 and 299 seconds are hits, because the entry is still inside its 300-second window. At 300 seconds the entry has expired, so the next call misses again. Four page views cost the database only two queries.
Check Your Understanding
Try these two scenarios. Each one needs the ideas above to answer it.
Quick check
A news site's homepage runs the same heavy 'top 10 articles today' query on every visit, and the database is overloaded. Which change reduces the load most safely, and what new problem could appear?
Quick check
An online shop wants to cache three things for 10 minutes: the product description, the stock left for a popular item, and each customer's cart count. Which choice fits the ideas in this lesson best?
Common Mistakes to Avoid
No TTL
A cache entry with no expiry can stay wrong for a very long time if an invalidation step has a bug.
Forgetting to invalidate
Updating the database but not the cache is the classic bug. Readers then see old data that looks valid.
Caching everything
Caching data that is read once or changes every second adds a network hop and a risk, with no real gain.
Ignoring stampedes
A busy key that expires can send a rush of misses to the database at the same time.
Key Takeaways
Database caching keeps copies of frequent or expensive reads somewhere faster. The database's own buffer pool and plan caches work without your help. An in-memory layer such as Redis gives you more control, but you must manage its copies. Use a TTL as a safety net, remove copies when the original changes, and cache only data that is read often and changes rarely.
Sources
- Redis tutorial: Cache-Aside Pattern with Redis (query caching for microservices)
- Redis documentation: Key eviction
- Redis documentation: EXPIRE command
- Redis documentation: Eviction policy (Redis Software)
- Wikipedia: Database caching
- Layrs course: Database Caching - Query Cache, Buffer Pool and Redis
- Systems Architect: In-Memory Caching (Redis/ElastiCache) vs Database-Level Caching
- Medium: Caching Is a Concept, Not Use Redis
- Medium: Advanced SQL Server Series, Part 12: Usage of Caching
- Kokil: Database Query Caching Strategies, A Practical Guide
- Raju Ginni: MySQL query cache vs Redis vs Memcached vs buffer pool
- Caching: Beyond the Basics (adhdecode)
- DEV: Master-Class Caching, What Every Software Engineer Needs to Know
- DEV: Caching Strategies, Make Backends Fast Without Melting the Database
- Levelop: Cache Invalidation Strategies in System Design
- Low Level Design Mastery: Cache Invalidation
- Gophertrunk: Caching layers
- MajidPro: Caching Strategies and Cache Invalidation Guide
- Matthew Swong: Cache Invalidation Strategies, TTL, Events and Versioned Keys
- Scale Dojo: Cache Invalidation Strategies Explained
- arXiv: The Case of Transparent Cache Invalidation in Web Applications
- DEV: Redis Key Expiration, TTL, and Eviction Policies
- Acemq: Redis Eviction, All Policies and Which to Use
Last updated on 18 November 2026
Related lessons
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.