Async Work: The N+1 Unfurl and the Fan-Out Nobody Watches

What Stays on the Critical Path, and What Doesn't

Synchronous — must finish before ack

Authenticate and authorise · validate · persist to MySQL · assign ts · publish to the Channel Server.

That's it. Everything here is required for "the message exists and is delivered."

Asynchronous — enqueued, done later

Link unfurling · push notifications · search indexing · outgoing webhooks and app events · analytics · thread and mention subscription bookkeeping · compliance export.

None of it is needed for the message to appear.

The send path is thin by design. But "asynchronous" does not mean "free" — it means the work moved to a place where nobody notices it is slow until it is catastrophic.

Unfurling is: someone pastes a URL, and Slack replaces it with a rich preview — title, description, image. Simple feature, and a textbook N+1.

N+1 is the pattern where fetching a collection costs 1 query for the list plus N more, one per item — when a single batched query would have done. Here it shows up in layers:

✗ N+1 — the naive unfurl worker

msg = load_message(ts)          # 1

for url in msg.urls:            # N links
    meta  = db.get_unfurl(url)  # +N queries
    perm  = db.can_see(user,url)# +N queries
    if not meta:
        meta = http_fetch(url)  # +N ext calls
    db.save_unfurl(url, meta)   # +N writes
    edit_message(ts, meta)      # +N edits …
                                #  …each re-fans
                                #  out to ALL n
                                #  channel members

✓ Batched — the shape that survives

msg  = load_message(ts)         # 1
urls = dedupe(msg.urls)         # collapse dupes

metas = db.get_unfurls(urls)    # 1 batched read
miss  = [u for u in urls
         if u not in metas]

fetched = http_fetch_all(miss)  # 1 parallel batch
                                #  with timeouts
db.save_unfurls(fetched)        # 1 batched write

edit_message(ts, all_metas)     # 1 edit
                                # 1 fan-out

The fixes follow directly: dedupe URLs within a message, batch the metadata lookup into one query, cache unfurls globally by URL (the preview of a public news article is identical for every workspace on Earth — this is one of the very few things in Slack that legitimately crosses the tenant boundary), while checking permission per workspace (whether you may see a preview of a private Google Doc is not shareable), enforce timeouts and circuit breakers on outbound fetches, and emit one edit when everything resolves rather than one per link.

The Incident: When Job Fan-Out Ate the Database

Slack's post "The Query Strikes Again" describes exactly the failure this part predicts — and it is worth tracing step by step, because every link in the chain is a design decision that looked fine in isolation.

  1. The trigger is mundane. A customer removes a large number of users from their workspace at once. Slack notes customers "tend to remove them in small groups" — so the bulk path was rare and under-exercised. Rarely-taken code paths are where the sharp edges live.

  2. The job fans out per user, per channel, per thread. "Forget user" must delete that user's subscription records everywhere. The implementation queried all of a user's thread subscription IDs across all channels — "for some users, this was tens of thousands" — and issued per-channel jobs. Users × channels × threads.

  3. Load lands unevenly. The DELETEs spread across Vitess shards, but one shard held “6% of the user's subscription data” and, at that request rate, was the first to show replication lag. Sharding distributes; it does not equalise.

  4. Replicas fall behind, then the primary dies. Replication lag grew under the write flood, and the same write volume drove the Vitess tablets on the shard primary out of memory — at which point the kernel OOM-killed the MySQL process.

  5. The recovery loop becomes the outage. A replica is promoted. A replacement starts catching up, can't finish fast enough, and is deprovisioned for being unhealthy. The new primary — still under the same unrelenting write load — is OOM-killed too. Repeat. The automation intended to heal the cluster is what prevents it from healing.

A job-layer fan-out becoming a self-sustaining database outage. Across the top, a one-way ignition: a customer removes hundreds of users at once, the forget-user job fans out per user times channel times thread, and tens of thousands of DELETEs land on Vitess, one shard holding six percent of them. That write flood feeds down into a closed four-step ring that runs clockwise and feeds itself: one, MySQL on the shard primary is OOM-killed as the Vitess tablets exhaust memory; two, automation promotes a replica and starts building a replacement; three, the replacement never catches up, so automation deprovisions it as unhealthy, leaving one replica fewer; four, the new primary is handed the same unthrottled write flood, and it dies the same way, which returns to step one. Nothing inside the ring removes load: capacity falls and load does not, so the automation meant to heal the cluster is what stops it from healing. A red cut across the feed arrow marks the only exit: the load has to be taken away. A footer lists the four fixes that cut links: collapse the fan-out into one unsubscribe-from-all job, throttle and circuit-break in front of Vitess, exponential backoff so retries stop becoming load, and the immediate stopgap of larger instances so the OOM stops.
A job-layer fan-out becoming a self-sustaining database outage

The fixes map one-to-one onto the failure modes. Immediate: provision larger instances so the OOM stops. Permanent: restructure the job to issue a single "unsubscribe from all threads" instead of per-channel jobs (kill the fan-out at the source); add throttling and circuit breakers in front of Vitess (bound the rate); add exponential backoff for failed jobs (stop retries from becoming the load).

  • Guarantees: The synchronous send path stays inside its latency budget regardless of how much downstream work a message implies, and async work survives restarts because it's queued rather than held in a request.
  • Does not guarantee: That deferred work is bounded, ordered, or timely. One trigger can generate tens of thousands of jobs; a slow third-party site can drain a worker pool shared with unrelated features; and without explicit throttling the queue will happily deliver more load than the datastore survives.