The Question Strikes Once more – Slack Engineering

The Question Strikes Once more – Slack Engineering
The Question Strikes Once more – Slack Engineering

On Thursday, 12 Oct. 2022, the EMEA a part of the Datastores workforce — the workforce answerable for Slack’s database clusters — was having an onsite day in Amsterdam, the Netherlands. We’re sitting collectively for the primary time after new engineers had joined the workforce, when all of the sudden a couple of of us have been paged: There was a rise within the variety of failed database queries. We stopped what we have been doing and staged-in to unravel the issue. After investigating the difficulty with different groups, we found that there was a long-running job (async job), and that it was purging a considerable amount of database data. This brought on an overload on the database cluster. The JobQueue workforce — answerable for asynchronous jobs — realized that we couldn’t cease the job, however we may disable it utterly (this operation known as shimming). This meant that the working jobs wouldn’t cease, however that no new jobs can be processed. The JobQueue workforce put in the shim, and the variety of failed database queries dropped off. Fortunately, this incident didn’t have an effect on our prospects.

The very subsequent day, the Datastores EMEA workforce received the identical web page. After wanting into it, the workforce found that the issue was just like the one skilled the day earlier than, however worse. Comparable actions have been taken to maintain the cluster in working situation, however there was an edge-case bug in Datastores automation which led to failure to deal with a flood of requests. Sadly, this incident did impression some prospects, they usually weren’t in a position to load Slack. We disabled particular options to assist scale back the load on the cluster, which helped give room to get well. After some time, the job completed, and the database cluster operated usually once more.

On this publish, we’ll describe what brought on the problems, how our datastores are arrange, how we fastened the problems, and the way we’re stopping them from occurring once more.

The set off

Certainly one of Slack’s prospects eliminated numerous customers from their workspace. Eradicating quite a few customers in a single operation will not be one thing prospects do usually; as an alternative, they have an inclination to take away them in small teams as they go away the corporate. Person elimination from Slack is completed by way of an asynchronous job referred to as neglect consumer. When the neglect consumer job began, it led to a spike within the database load. After some time, one of many shards couldn’t address the workload.

Within the determine above, you possibly can see a major improve within the variety of database queries. It is a screenshot of our monitoring dashboard through the incident; it was a necessary device through the incident, and it helped us make educated selections.


Let’s elaborate on some ideas earlier than we take a deep dive into what occurred.

Knowledge storage

The Datastores workforce makes use of Vitess to handle Slack’s MySQL clusters. Tables in Vitess are organized into keyspaces. A keyspace is a distributed database. It seems like a single MySQL occasion to consumer functions, whereas being distributed throughout a number of schemata of various MySQL cases.

Slack depends on an enormous dataset that doesn’t slot in a single MySQL occasion. Subsequently, we use Vitess to shard the dataset. There are different advantages from having a sharded database for every particular person MySQL occasion that’s a part of the dataset:

  • Quicker backup and restore
  • Smaller backup sizes
  • Blast radius mitigation: in case a shard is down, much less makes use of are impacted
  • Smaller host machines
  • Distributed database question load
  • Enhance in write capability

Each keyspace in Vitess consists of shards. Consider a shard like a slice of a keyspace. Every shard shops a key vary of keyspace IDs, they usually collectively symbolize what known as a partition. Vitess shards the information based mostly on the shards’ partition ranges.

For instance, a “customers” desk may be saved in a keyspace composed of two shards. One shard covers keys within the -80 (hexadecimal key ID) vary, and the opposite one, within the 80- vary. -80 and 80- symbolize integer numbers under and above (2^64)/2, respectively.  Assuming the sharding key’s homogeneously distributed, which means that Vitess will retailer half the data in a single shard, and half within the different one. Vitess additionally shops shard metadata internally in order that VTGates can decide the place to search out the information when a consumer requests it. Within the determine under, Vitess receives a SELECT assertion for one of many consumer’s knowledge. It seems into the metadata and determines that this consumer’s knowledge is obtainable within the shard with vary “80-“:

Question success and replication

Slack is greater than a messaging app, and lots of different options in Slack additionally depend upon Vitess. To extend database throughput, in addition to for failure tolerance, every shard has replicas. For every shard we create, there’s one major pill and a number of reproduction tablets. Main tablets are primarily answerable for queries modifying the information (DELETE, INSERT, UPDATE aka DML). Replicas are answerable for SELECT queries (the first may fulfill the SELECT queries, but it surely’s not beneficial as there’s restricted room for scaling). After knowledge is dedicated to the first, it’s distributed to the replicas in the identical shard by way of MySQL replication. The character of replication is that modifications are dedicated within the major earlier than they’re utilized within the replicas. Underneath low write load and a quick community, the information within the reproduction lags little or no behind the information within the major, However beneath excessive write load, replicas can lag considerably, resulting in potential reads of stale/outdated knowledge by consumer functions. What quantity of replication lag is appropriate relies on the appliance. At Slack, we take replicas out of service if their replication lag is larger than one hour —- that’s, if the information current on the replicas is lacking modifications from greater than an hour in the past.


We’ve a process to switch an current reproduction pill with a brand new one. To simplify the logic, we are able to think about it consisting of 4 steps. The first step is to provision the brand new host with all of the dependencies, instruments, safety insurance policies, MySQL, and Vitess. The second step is to attend for the pill to acquire a replica of the information by restoring the newest backup. The third step is to catch-up on replication. After that is carried out, the brand new reproduction can begin serving site visitors. Lastly, the fourth step is to deprovision the outdated reproduction. We’ll talk about the third step — catching up — a bit in additional element under.


As soon as the newest backup has been restored, the brand new reproduction has a replica of the dataset; nonetheless, it’s not updated, because it doesn’t but have the modifications which have taken place for the reason that backup was taken. Catching up means studying these modifications from MySQL’s binary log and making use of them to the copy of the information of the brand new reproduction. A reproduction is taken into account caught up as soon as its replication lag is under an acceptance threshold. Whereas we’re discussing catch-up right here from the viewpoint of provisioning a brand new reproduction, it’s value noting that replicas are consistently catching as much as any new modifications that their major might have taken.

What occurred through the incident

With the high-level context, let’s get again to the incident. In the event you bear in mind, a buyer deleted many customers from their workspace. This motion kicked off the neglect consumer job, which requires unsubscribing every affected consumer from the channels and threads they have been subscribed to. So to delete customers, it’s needed additionally to find and delete data representing every subscription of every consumer to every channel they belong to, and every subscription of every thread they participated in. Which means that an unlimited variety of database queries have been despatched to the a number of Vitess shards. That’s the variety of customers being deleted multiplied by the typical variety of subscribed objects per consumer. Sadly, there was one shard that contained 6% of the consumer’s subscription knowledge. When this shard began to get that many requests, we began to see MySQL replication lag within the shard. The rationale behind this lag is that replicas have been having bother maintaining with the first because of the great amount of knowledge being modified. To make issues worse, the excessive quantity of write load additionally led the Vitess tablets to expire of reminiscence on the shard major, which brought on the kernel to OOM-kill the MySQL course of. To mitigate this, a duplicate was robotically promoted to major, and a substitute began to happen. As described above, the substitute pill restored knowledge from the final backup, and tried to catch-up with the first. Due to the massive quantity of database writes executed on the first, they took a very long time to catch-up, due to this fact not having the ability to begin serving site visitors quick sufficient. This was interpreted by our automation because the newly provisioned reproduction not being wholesome, and due to this fact needing to be deprovisioned. Within the meantime, excessive write load continued, inflicting the brand new major to additionally run out of reminiscence, leading to its MySQL course of being killed by the kernel. One other reproduction was promoted to major, one other substitute was began, and the cycle repeated.

In different phrases, the shard was in an infinite-loop of the first failing, a duplicate being promoted to major, a substitute reproduction being provisioned, attempting (and failing) to catch-up, and eventually getting deprovisioned.

How we fastened it


The Datastores workforce broke the substitute loop by provisioning bigger occasion sorts (i.e. extra CPU and reminiscence) replicas manually. This mitigated the OOM-kill of the MySQL course of. Moreover, we resorted to guide provisioning as an alternative of automation-orchestrated substitute of failed hosts to mitigate for the difficulty during which our automation deprovisioned the replacements as a result of it thought of them unhealthy, attributable to the truth that they did not catch-up in an inexpensive period of time. This was arduous for the workforce as a result of now they should manually provision replicas, along with dealing with the excessive write site visitors.

Neglect Person Job

The “neglect consumer” job had problematic efficiency traits and brought on the database to work a lot tougher than it wanted to. When a “neglect consumer” job was being processed, it gathered all the channels that the consumer was a member of and issued a “go away channel” job for every of them. The aim of the “go away channel” job was to unsubscribe a consumer from all the threads that they have been subscribed to in that channel.  Underneath typical circumstances, this job is barely run for one channel at a time when a consumer manually leaves a channel. Throughout this incident nonetheless, there was an enormous inflow of those “go away channel” jobs corresponding to each channel that each consumer being deactivated was a member of.

Along with the sheer quantity of jobs being a lot larger than regular throughout this incident, there have been many inefficiencies within the work being carried out within the “go away channel” job that the workforce that owned it recognized and glued:

  1. First, every job run would question for all the Person’s subscriptions throughout all channels that they have been a member of though processing was solely being carried out for the one channel that they have been “leaving”.
  2. A associated drawback occurred through the subsequent UPDATE queries to mark these subscriptions as inactive. When issuing the database UPDATEs for the thread subscriptions within the to-be-left-channel, the UPDATE question, whereas scoped to the channel ID being processed, included all the consumer’s thread subscription IDs throughout all channels. For some customers, this was tens of 1000’s of subscription IDs which may be very costly for the database to course of.
  3. Lastly, after the UPDATEs accomplished, the “go away channel” job queried for all the consumer’s thread subscriptions once more to ship an replace to any related shoppers that might replace their unread thread message rely to not embrace threads from the channel they’d simply left.

Contemplating that these steps wanted to happen for each channel of each consumer being deleted, it turns into fairly apparent why the database had bother serving the load.

To mitigate the issue through the incident, the workforce optimized the “go away channel” job. As an alternative of querying for all subscriptions throughout all channels, the job was up to date to each question for under the subscriptions within the channel being processed and solely embrace these subscription IDs within the subsequent UPDATEs.

Moreover, the workforce recognized that the ultimate step to inform the shoppers about their new thread subscription state was pointless for deactivated customers who couldn’t be related in any case in order that work was skipped within the “neglect consumer” state of affairs completely.

Consumer workforce

As a closing resort to take care of the consumer expertise and let our customers proceed utilizing Slack, the consumer workforce quickly disabled the Thread View from the Slack consumer. This motion diminished the quantity of learn queries towards Vitess. This meant that fewer queries hit the replicas. Disabling the characteristic was solely a short lived mitigation. At Slack, our customers’ expertise is the highest precedence, so the characteristic was enabled once more as quickly because it was secure to take action.

How are we stopping it from occurring once more?


Are you able to recall the actual edge-case concern that the workforce encountered with replacements through the incident? The workforce swiftly acknowledged its significance and promptly resolved it, prioritizing it as a prime concern.

Apart from fixing this concern, the Datastores workforce has began to adapt the throttling mechanism and the circuit breaker pattern, which have confirmed to be efficient in safeguarding the database from question overload. By implementing these measures, the Datastores workforce is ready to proactively stop shoppers from overwhelming the database with extreme queries.

Within the occasion that the tablets throughout the database infrastructure turn out to be unhealthy or expertise efficiency points, we are able to take motion to restrict or cancel queries directed on the affected shard. This strategy helps to alleviate the pressure on the unhealthy replicas and ensures that the database stays steady and responsive. As soon as the tablets have been restored to a wholesome state, regular question operations can resume with out compromising the general system efficiency.

Throttling mechanisms play an important function in controlling the speed at which queries are processed, permitting the database to handle its sources successfully and prioritize important operations. As a result of this can be a essential a part of stopping overload, the Datastores workforce has  been contributing associated options and bug fixes to Vitess [1, 2, 3, 4, 5, 6, 7]. This is likely one of the optimistic outcomes of this incident.

Along with throttling, the workforce has adopted the circuit breaker sample, which acts as a fail-safe mechanism to guard the database from cascading failures. This sample includes monitoring the well being and responsiveness of the replicas and, within the occasion of an unhealthy state being detected, quickly halting the move of queries to that particular shard. By doing so, the workforce can isolate and comprise any points, permitting time for the replicas to get well or for alternate sources to be utilized.

The mix of throttling mechanisms and the circuit breaker sample offers the Datastores workforce with a sturdy protection towards potential overload and helps to take care of the steadiness and reliability of the database. These proactive measures be certain that the system can effectively deal with consumer requests whereas safeguarding the general efficiency and availability of the database infrastructure.

Neglect Person Job

After the mud settled from the incident, the workforce that owned the “neglect consumer” job took the optimizations additional by restructuring it to make life a lot simpler for the database. The “go away channel” job is suitable when a consumer is definitely leaving a single channel. Nevertheless, throughout “neglect consumer”, issuing a “go away channel” concurrently for all channels {that a} consumer is a member of causes pointless database rivalry.

As an alternative of issuing a “go away channel” job for every channel {that a} consumer was a member of, the workforce launched a brand new job to unsubscribe a consumer from all of their threads. “Neglect consumer” was up to date to enqueue only a single new “unsubscribe from all threads” job which resulted in a lot decrease rivalry throughout “neglect consumer” job runs.

Moreover, the Neglect Person job began to adapt the exponential back-off algorithm and the circuit breaker pattern. This implies jobs which can be getting failed will address the state of the dependencies (like database) and can cease retrying.


The incidents that occurred on October twelfth and thirteenth, 2022 highlighted among the challenges confronted by the Datastores EMEA workforce and the groups working asynchronous jobs at Slack. The incident was triggered by a major variety of customers being faraway from a workspace, resulting in a spike in write requests  and overwhelming the Vitess shards.

The incident resulted in replicas being unable to meet up with the first, and the first crashing, resulting in an infinite loop of replacements and additional pressure on the system. The Datastores workforce mitigated the difficulty by manually provisioning replicas with extra reminiscence to interrupt the substitute loop.

The workforce answerable for the Neglect Person job performed an important function in stopping the job answerable for the database write requests and optimizing the queries, lowering the load on the first database.

To forestall related incidents sooner or later, the Datastores workforce has applied throttling mechanisms and the circuit breaker sample to proactively stop overwhelming the database with extreme queries. They’ve additionally tailored the exponential back-off algorithm to make sure failed jobs address the state of dependencies and cease retrying.

General, these measures applied by the Datastores workforce, the workforce proudly owning the neglect consumer job and the workforce offering async job infrastructure assist safeguard the steadiness and reliability of Slack’s database infrastructure, making certain a easy consumer expertise and mitigating the impression of comparable incidents.