07 / 25 · Traffic and capacity
Caching
A cache keeps a reusable copy so future reads can avoid slower work. Copies can become stale or disappear. Decide what may be cached, for how long and who is allowed to read it.
What you will practise
- Trace cache-aside hits/misses
- reason about staleness
- protect the cold-cache path
Before you start: Source of truth, key and expiry.
About 4 minutes · offline-generated English female narration · captions included. Diagrams are conceptual; timings are illustrative.
Download MP4CaptionsListen without the video
Download audioChapters and full transcript
Let us understand caching. You keep a timetable on your desk instead of walking to the office every time. It is fast, but a changed timetable makes your copy wrong. The expiry time tells you when to check again; it does not make the copy correct until then. A cache keeps a reusable copy so future reads can avoid slower work. Copies can become stale or disappear. Decide what may be cached, for how long and who is allowed to read it.
Follow the normal path. The reader asks for a public event description. The key identifies the resource and representation; private results need appropriate identity scoping. A reusable cached value returns quickly. Its age matters: a fast response can still be too stale for the business operation. SeatSpot event descriptions change rarely and are read often. They are good cache candidates. Available-seat ownership and payment outcomes need stricter correctness rules than a public description.
Now change one condition. If the value is absent or expired, fetch from the source of truth. A miss is normal and must have bounded latency and safe error handling. A common misunderstanding is this: A cache copy is always current until TTL expires. It can become stale immediately after a source update; expiry is not a correctness guarantee. Think about this. A popular key expires for every visitor at once. What load reaches the database? Pause the video and make your prediction. Many concurrent misses can refill the same value. Coalesce work, stagger expiry or serve controlled stale data where allowed.
Let us reason with a concrete example. An illustrative endpoint receives 1,000 reads/second with an 80% cache hit rate. About 200 reads/second reach the source in the simplified model. After a cache outage, the source may suddenly receive all 1,000. Test the cold-cache path, bound concurrent refill work and specify acceptable staleness for descriptions. Do not use the estimate to promise production capacity. These figures are teaching examples, not production guarantees.
Turn the idea into a design decision. Many readers may miss the same hot key together. Coalesce loads or bound refill work so the cache does not amplify pressure on the database. Populate the cache with an explicit lifetime. Plan what happens after an update and whether serving an older value is acceptable during refresh. When you try the practice task, look for this result. At 1,000 reads/second, source demand can jump from about 200 to 1,000 when the cache disappears.
Check your understanding. Remove the cache in a safe test. Observe source traffic, latency and errors. A warm hit-rate chart alone does not prove the architecture can recover. Here is a challenge. An event title changes but users still see the old title. Explain two remedies. Pause before the answer. Invalidate or version the affected cache entry on a successful update, or accept the documented TTL staleness. Consider races between refill and update; test the chosen consistency behavior. For an interview, choose key scope, invalidation and stampede control. Say what you would measure, not only which tool you would buy.
Start with an everyday picture
You keep a timetable on your desk instead of walking to the office every time. It is fast, but a changed timetable makes your copy wrong. The expiry time tells you when to check again; it does not make the copy correct until then.
Why SeatSpot needs this
SeatSpot event descriptions change rarely and are read often. They are good cache candidates. Available-seat ownership and payment outcomes need stricter correctness rules than a public description.
Trace one step at a time
Move through the diagram at your pace. Teal paths are active; unavailable components are labelled.
Request a description
The reader asks for a public event description. The key identifies the resource and representation; private results need appropriate identity scoping.
Read all six mechanism steps
Request a description
The reader asks for a public event description. The key identifies the resource and representation; private results need appropriate identity scoping.
Serve a hit
A reusable cached value returns quickly. Its age matters: a fast response can still be too stale for the business operation.
Handle a miss
If the value is absent or expired, fetch from the source of truth. A miss is normal and must have bounded latency and safe error handling.
Control refill work
Many readers may miss the same hot key together. Coalesce loads or bound refill work so the cache does not amplify pressure on the database.
Store a temporary copy
Populate the cache with an explicit lifetime. Plan what happens after an update and whether serving an older value is acceptable during refresh.
Test a cold cache
Remove the cache in a safe test. Observe source traffic, latency and errors. A warm hit-rate chart alone does not prove the architecture can recover.
Pause and make a prediction
A popular key expires for every visitor at once. What load reaches the database?
Correct a common misunderstanding
Claim to question: A cache copy is always current until TTL expires.
Better explanation: It can become stale immediately after a source update; expiry is not a correctness guarantee.
Trace a cache-aside sequence
- Write a cache key for a public event description.
- Follow miss → source read → cache write, then a hit.
- Remove the cache and estimate load at 80% previous hit rate.
Check what you can explain
This is your self-check, not an automated grade. Try again without the diagram tomorrow.
0 of 3 goals checked. Choose one remaining idea to practise next.
Teach it back
Without looking, explain what this component changes, what it does not solve and what happens if it fails. Use the animation to check your explanation, then switch to Engineering.
Engineering model and trade-offs
In cache-aside, read the cache, fetch from the source on a miss, then populate the cache. The source remains authoritative. Define keys, TTL, invalidation and serialization; include tenant/user scope when needed.
A popular expired key can cause a cache stampede. Coalesce concurrent loads, add TTL jitter or use controlled stale-while-revalidate where staleness is allowed. Protect the source if the cache is unavailable.
Monitor hit rate, eviction and stale-result impact. A high hit rate can hide a very expensive miss path. Never cache private responses under a shared key that omits the authorization context.
Work through an example
An illustrative endpoint receives 1,000 reads/second with an 80% cache hit rate.
Reason with the numbers
About 200 reads/second reach the source in the simplified model. After a cache outage, the source may suddenly receive all 1,000.
Make a decision
Test the cold-cache path, bound concurrent refill work and specify acceptable staleness for descriptions. Do not use the estimate to promise production capacity.
Implementation sketch
async function description(id: string) {
const key = `event:v1:${id}`;
const hit = await cache.get(key);
if (hit !== null) return hit;
const value = await db.events.find(id);
await cache.set(key, value, {ttlSeconds: 60});
return value;
}Illustrative pseudocode: cache/db adapters are not defined. Real code must handle errors, null values, concurrent misses, validation and invalidation.
Where this is used
- Repeated expensive reads with an explicit staleness budget.
- Public descriptions, computed summaries and suitable static assets.
Common mistakes
- Treating a cache as the only durable copy.
- Caching private data with a public shared key.
- Ignoring simultaneous misses after expiry.
Practise a reasoned answer
Answer aloud or write notes before opening the reference. These prompts teach reasoning and trade-offs; they are not claimed questions from a particular company.
How would you choose a TTL?
Choose a practice depth
- Foundation
- Explain a hit, miss and source of truth.
- Practical
- Choose key scope, invalidation and stampede control.
- Advanced
- Defend consistency boundaries, cold-cache capacity and private-data isolation.
Self-check rubric
- State the required user outcome and the relevant invariant.
- Explain the normal path and one failure path.
- Name a cost or consistency/latency trade-off.
- Say what you would measure or test before trusting it.
For deeper practice, connect this topic to the previous lesson and identify a shared bottleneck. A longer answer is not automatically a better answer.
Try it before viewing the solution
An event title changes but users still see the old title. Explain two remedies.
Check one key idea
Your explanation and interview notes
Saved only in this browser when storage is available. Export a backup from the course page before clearing browser data or moving devices.
Loading your local notes…
Official references and next reading
The explanation, fictional examples and animations are original teaching material. Vendor behavior depends on the specific product/version; use its documentation before deploying.