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&nbsp;managed to store trillions of messages. While there are numerous Youtube videos and articles about this article, I think one section of this article, titled &ldquo;<strong>Data Services Serving Data,&rdquo;</strong>&nbsp;didn&rsquo;t receive enough attention. In this article, we discuss Discord&rsquo;s approach to data services and explore how we can leverage Golang&rsquo;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&rsquo;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>Discord introduced an intermediary service that sits between Python API and the database cluster &mdash; 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">Website</a></p>