Scaling / 5 / 6
Cache invalidation
An account API keeps each account's plan in a cache in front of the database. A TTL alone keeps a customer who just paid on the old plan for up to ten minutes. Deleting the key after the write fixes the usual case but leaves a race in which an old value is written back after the delete. Delete twice, leases and invalidation from the database change log close the gaps one by one, and on the read side a hot key is served stale while one request refreshes it, and rows that do not exist are cached too.
- 14 min
- Advanced
- 12 steps
- Stale cache fills
- 0 /day
- Longest stale window
- 0 s
- DB reads
- 900 /s
- Cache hit ratio
- 94 %
- p99 latency
- 6 ms
- request
- reply
- redelivery
- duplicate
- lost
- call: the reply returns on the same line
Numbers are a model scenario, not measurements
The real request
Web app → Account APIRequest
GET /v1/accounts/4821/plan HTTP/1.13
Host: api.example.com
Authorization: hidden
Response
HTTP/1.1 200 OK
X-Cache1: HIT
Age2: 212
{
"account_id": 4821,
"plan": "free",
"seats": 43
}
X-Cache— This answer came from the cache, not from the database.Age— The cached copy is 212 s old. It can be up to 600 s old.plan— The value that decides which features the customer gets.seats— Cached together with the plan, so it goes stale together with it.
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