Cache

What's asked often, close at hand · Part of Networking and infrastructure

Key idea

Practice

7 questions · about 4 min

Explained step by step

Cache

A cache is like your desk drawer: you keep there what you use all the time, instead of going to the back storeroom every time. Reading from memory takes, say, 1 ms; reading from the database, 50 ms. It works because what was asked for recently will probably be asked for again. The price: the cache is small and holds only part of the data, and only for a while.

Cache miss

When the requested data is in the cache, it's a cache hit: it's served fast and that's it. When it isn't, it's a cache miss: you fetch it from slow storage and, while you're at it, save it in the cache for next time. It's like finding a book on your shelf (hit) or having to go to the library for it (miss) and then leaving it on the shelf. If 90 of 100 requests are hits, 90% are answered in 1 ms.

Hot cache

A computer's caches are arranged in levels: L1 is the smallest and fastest, then L2, L3 and so on. You look in L1 first; if it's not there, in L2; if not, in L3. Depending on where the data is found, the hit is called hot (L1, fastest), warm (L2 or L3) or cold (L3 or lower, slowest). It's like looking for your keys: in your pocket (hot), in your backpack (warm) or in the car (cold): you find them anyway, but it takes different times.

Write-through

Write-through writes to the cache and the database at the same time, like noting an appointment on your phone and in a paper planner at once. The upside: both always match and reads come fast from the cache. The downside: every write waits for both, so writing takes longer. If the cache takes 1 ms and the database 50 ms, every write takes at least 50 ms.

Write-around

Write-around writes straight to the database and skips the cache, like filing a new document straight into the archive without leaving it on your desk. Writing is faster because only 1 place is touched. But if someone reads it right after, it's not in the cache: it's a miss and you go to the database, which takes 50 ms instead of 1. It fits when what's written is rarely read again soon.

Write-behind

Write-back (also called write-behind) writes only to the cache and confirms right away; later, in the background, it copies the data to the database. It's like jotting something on a sticky note and copying it into your notebook later: very fast, but if you lose the note first, it's gone. It gives the lowest latency and high throughput for write-heavy apps. The risk: if the cache crashes before copying, that data is lost; it's reduced by having more than 1 replica acknowledge the write.

Cache invalidation

Invalidating means marking cached data as stale and removing or replacing it. If a product's price changes from 100 to 120 in the database but the cache still says 100, users see a false price. It's like a sale sign nobody took down when the sale ended. That's why it's called one of the two hard problems in computer science: you have to know exactly when a stored piece of data stopped being true.

Terms in this lesson

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.

Key idea

What's asked often, close at hand