Part 6 of 8 · 2 min read
Scaling
Shards and a cache.
The base design handles a hotel chain easily. The interviewer may ask what happens for a travel site such as Booking.com, where the load could be 1,000 times higher.
Stateless services scale by adding servers. The database holds the state, so it is the bottleneck.
Sharding. Most queries filter by hotel, so shard by hotel id. With 30,000 queries per second spread over 16 shards, each shard handles 30,000 / 16 = 1,875 per second, which a single database server can take.
Old data. Only current and future dates matter for booking. Expire old inventory automatically (a time-to-live, TTL) and keep history in another database.
Inventory cache. To reduce load, put a cache (such as Redis) in front. Reads, which are far more common than writes, are answered by the cache. The key is the hotel, room type and date, and the value is the number of rooms available. Updates go to the database first, then reach the cache a moment later, either by the application code or by change data capture (CDC), a tool that reads changes from a database and applies them to another system. The database stays the source of truth.
The cache can be a little out of date. Is that a problem? The book argues it is not. If the cache says a room is free when the database says it is sold, the user tries to book, the database check fails, and the user sees an error "someone just booked the last room". That is annoying, but correct. And if the cache shows sold-out when a room has just been freed by a cancellation, the user loses a chance, which the business can accept.
A worked example (made up). At 30,000 queries per second with 95 per cent of them reads, the cache answers 28,500 and the database sees only about 1,500 reads, plus the writes.
Source of truth
The source of truth is the one store whose value wins when copies disagree.
Example: the inventory database is the source of truth, and the cache is a faster, slightly older copy.
Check yourself: 1. Why shard by hotel id? 2. What does CDC do? 3. Why is a stale cache acceptable here?