Learning from Discord’s Approach — Request Coalescing with Golang
<p>As you might have seen previously, Discord published a valuable article last year discussing how they successfully <a href="https://discord.com/blog/how-discord-stores-trillions-of-messages" rel="noopener ugc nofollow" target="_blank">managed to store trillions of messages</a>. While there are numerous Youtube videos and articles about this article, I think one section of this article, titled “<strong>Data Services Serving Data,”</strong> didn’t receive enough attention. In this article, we discuss Discord’s approach to data services and explore how we can leverage Golang’s concurrency features to reduce database load in certain scenarios.</p>
<h1>Data Services to Rescue Hot Partitions</h1>
<p>As you know, messaging and channels are the most used components of Discord. Let’s imagine a scenario where an admin of a channel with 500k members mentions @everyone. What would happen? Thousands of simultaneous requests direct to that database partition, all aiming to retrieve the same message. This pattern repeats until the partition can no longer respond to other requests.</p>
<p><img alt="" src="https://miro.medium.com/v2/resize:fit:700/1*ws3UfTelEWtyW7jHpAlVJA.png" style="height:444px; width:700px" /></p>
<p>Discord introduced an intermediary service that sits between Python API and the database cluster — which they call data service. This service contains roughly one gRPC endpoint per query without any business logic. The big feature that this service has for Discord is request coalescing.</p>
<h2>Request Coalescing</h2>
<p>As we discussed before, numerous similar requests direct to the database partition whenever there is a mention in a huge channel. By coalescing the requests, if multiple users are requesting the same row of the database, we can merge these requests in only one select query and run that instead.</p>
<p><a href="https://itnext.io/learning-from-discords-approach-request-coalescing-with-golang-41d824d83ed8"><strong>Visit Now</strong></a></p>