teacher
Sign in

Caching

Keep a copy of hot data in fast memory so most requests never reach the database.

30 min5 interactive

What a cache is

A cache is a small, fast store that keeps copies of data that is expensive to fetch or compute. Reading from memory takes well under a millisecond. A database query over the network often takes 5 to 50 ms.

  • A hit means the data was in the cache. A miss means it was not, so you go to the slower source.
  • The hit ratio is hits divided by total requests. It is the one number that tells you if a cache is worth having.
  • Typical tools: Redis and Memcached (in-memory key-value servers), plus caches inside your app process, your browser and your CDN.

Why it works: real traffic is lopsided. A small share of keys gets most of the reads, so a cache that holds only the hot keys catches most requests.

Estimate
Your API takes 10,000 reads/s. The cache hit ratio is 90%. How many reads per second reach the database?
reads/s
1 of 7