Skip to content
tezvyn:
Menu

How does caching reduce database load?

Source: interviewEasyHow cards are made

Summary

caching as a read-offload layer.

Key points

cache-aside reads, RAM-speed lookups, TTL plus invalidation.

Watch out for

treating the cache as durable source of truth or ignoring stale-data and invalidation.

What's really being asked

The interviewer wants to know whether you understand why an in-memory store sits in front of a database and how it changes the load profile. It is a fundamentals check on latency, hit ratios, and consistency.

The full answer

A database read, especially a complex join, costs disk I/O and CPU. An in-memory store like Redis serves data from RAM in well under a millisecond. The dominant pattern is cache-aside: the application looks in the cache first, and on a hit returns immediately. On a miss it queries the database, writes the result into the cache with a time-to-live, and returns it. Repeated reads of hot keys then bypass the database, so query volume and connection pressure drop sharply. Good answers also name write strategies: write-through updates the cache and database together, while write-around or explicit invalidation deletes the cached key on a write so the next read repopulates fresh data.

The mistakes people make

Treating the cache as a primary store rather than a volatile speed layer. Forgetting invalidation, which leads to stale reads after updates. Assuming caching always helps; it only helps when reads repeat and data is reused. Caching highly volatile or per-request-unique data wastes memory and lowers the hit ratio.

What usually comes next

How do you pick a TTL? How do you handle a cache stampede when a popular key expires? What is the difference between Redis and Memcached? How do you size the cache and what eviction policy do you choose, such as LRU?

A concrete example

A product page is viewed thousands of times per minute but changes rarely. You cache the rendered product object under the key product:123 with a sixty-second TTL. The first request misses and hits Postgres; the next thousands of requests hit Redis and never touch the database. When an admin edits the product, the write handler deletes product:123 so the following read fetches and caches the updated record.

Interview question

In a cache-aside setup, what happens on a cache miss for a requested key?

  • a.The app reads from the database, stores the result in the cache, then returns itCorrect
  • b.The request fails until the key is manually warmed into the cache
  • c.The database write path is blocked until the cache entry is rebuilt
  • d.The cache automatically queries the database and the app never sees a miss
Why?

Cache-aside puts the application in control: on a miss it reads the DB, populates the cache, and returns the value. The cache itself does not fetch from the DB, which is the write-through or read-through misconception.

Just read this? Test yourself on what you have been reading.

Read the original → docs.aws.amazon.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on caching — each one lists the topics its interview covers.

See open roles