Scaling / 4 / 6
Caching
A product page reads the same row from the database thousands of times a second. A cache in front of it makes reads fast, then brings its own problems: stale data, a stampede on a hot key and the question of where writes go.
- 13 min
- Intermediate
- 11 steps
- Cache hit ratio
- 0 %
- Reply p95
- 850 ms
- Database reads
- 2 400 /s
- Old price shown for
- 0 s
- request
- reply
- redelivery
- duplicate
- lost
Numbers are a model scenario, not measurements
The real request
Product API → DatabaseRequest
SELECT p.id, p.name, p.price, s.qty
FROM products p
JOIN stock s ON s.product_id = p.id1
WHERE p.id = 1042;
Response
1 row in 38 ms2
JOIN— Two tables joined on every request: price fromproducts, quantity fromstock.38 ms— Cheap on its own. Not at 2400 times a second on one database.
What happens if…?
Put in your own numbers: the result and the formula update at once.
Every read of the product page.
Share of reads the cache answers.
Reads per second the database can serve.
One GET from the cache.
One JOIN query on an idle database.
Result
- Database reads
- 144/s
- Database load
- 5.5%
- Average reply
- 3.4ms
- Reply p95 (rough)
- 41ms
Working
DB = λ × (1 − h) = 2 400 × (1 − 0.94) = 144/s
ρ = 144 / 2 600 = 5.5%
t(DB) = 38 / (1 − 0.055) = 40.2 ms
avg = h × 1.0 + (1 − h) × 41.2 = 3.4 ms
p95 ≈ miss (1 − h > 5%) = 41 msA simplified teaching model: it shows direction and scale, not a benchmark.
Build it yourselfCheat sheetCompare lessons
Course: 74 lessons
01 Foundations
02 Scaling
03 Data & storage
04 Communication
05 Reliability
06 Architecture
07 Security
08 Shipping
09 Whole systems
10 Business processes
11 Case studies