Brilliant technique for handling database queries literally saved Discord
It helped them store trillions of messages and fetch them without bringing their DB cluster to its knees.
The technique is called Request Coalescing.
And it’s too good to ignore.
But what’s so special about it?
If multiple users are requesting the same row at the same time, why not query the database only once?
This is exactly what Request Coalescing helps us achieve.
Here’s what happens under the hood:
- The first user that makes a request causes a worker task to spin up in the data service
- Subsequent requests for the same data will check for the existence of that task and subscribe to it
- Once the initial worker task queries the database and gets the result, it will return the row to all subscribers at the same time.
There are several pros to using Request Coalescing:
- Efficient utilization of database resources
- Ability to handle more concurrent requests without creating hot partitions
- Reduce latency
But there are some cons as well:
- Implementation can be complex with regards to getting a fair distributed reader-write lock. Basically, multiple readers need to access the data simultaneously while preventing conflicting writes
- Overall latency may go down, but certain requests will take more time
Of course, this technique is NOT needed normally. But at a certain scale, it can actually save your business.
Credits to Saurabh Dashora
Database
Highload
data architecture