Design Discord's Message Storage, stage 8 of 9: break it
Write the coalescer
Implement the read path in the data service so that identical concurrent reads share one database query. Permissions have already been checked by the API servers. Think about what happens when the shared query fails or hangs.
System so far· 5 parts
Select a component to see what it is responsible for and which state it owns.
- 1Members → API servers: Send, load history, jump
- 2API servers → Gateway: New message event
- 3Gateway → Members: Push to online members
- 4API servers → Message data service: Query by channel (hash-routed)
- 5Message data service → Message cluster: Read and write one partition
- Request / response
- Asynchronous
- Server push
What you need to know
0 of 2 checks done
A coalescer is a map from request key to in-flight promise:
- If the key is in the map, await the existing promise.
- Otherwise, start the query, store its promise, and remove the entry when it settles.
The key must include every parameter that changes the result: channel, cursor and limit. Two requests that differ in any of them aren't the same question.
Check
The shared query fails. What must happen to its map entry?Think first
The shared query hangs for 60 seconds. What happens to the hundreds of thousands of waiters, and what prevents it?