Scaling / 4 / 5
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.
Course: 42 lessons
01 Foundations
02 Scaling
03 Data & storage
04 Communication
05 Reliability
06 Architecture
07 Security
08 Shipping
09 Whole systems
10 Business processes