Quick revision
The short answer
The edges are written to two shards without a transaction, so a crash or timeout between the two writes leaves an edge in one direction only, and retries that are not idempotent increment the counters twice, which is where the 0.5 percent drift comes from. The celebrity timeouts come from every follow landing on one followers partition and one counter row. The fix is to make the following edge the record, written first with an idempotent upsert, propagate the reverse edge and counter changes asynchronously from an outbox or change log until applied, change counters only on a real state change, split big accounts' followers and counters into buckets, and run repair jobs and sampled audits to catch drift.
What to remember
In this answer, we saw that one-direction edges come from two non-atomic writes to different shards, counter drift comes from retries that repeat increments, and celebrity timeouts come from one hot partition and counter row. Making the following edge the record, writing an outbox event only on a real state change, and applying the reverse edge and counters idempotently fixes the first two. Buckets and sharded counters fix the hot spot, and repair jobs with sampled audits correct the past and catch future drift.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The edges are written to two shards without a transaction, so a crash or timeout between the two writes leaves an edge in one direction only, and retries that are not idempotent increment the counters twice, which is where the 0.5 percent drift comes from. The celebrity timeouts come from every follow landing on one followers partition and one counter row. The fix is to make the following edge the record, written first with an idempotent upsert, propagate the reverse edge and counter changes asynchronously from an outbox or change log until applied, change counters only on a real state change, split big accounts' followers and counters into buckets, and run repair jobs and sampled audits to catch drift.
Why this question is asked
Inconsistencies in a follow graph are a classic slow-burning bug: nothing fails loudly, but after a year, users notice that a profile says 1,204 followers while the list shows 1,198, or that someone appears in a follower list but the person says they never followed. Interviewers like it because it tests whether we can reason about partial failure in a dual write, and because the fix is a pattern we use far beyond social graphs. In this answer, we will see exactly how each symptom arises, then redesign the write path, the counters and the storage for large accounts, and add the checks that keep the graph honest.
1A Quick Recap of the Current Write Path
A follow from user 123 to user 456 currently does four things in sequence from the application: insert (123, 456) into the following table on 123's shard, insert (456, 123) into the followers table on 456's shard, increment 123's following count, and increment 456's follower count. Each step is a separate write on a separate shard, and the client retries the whole request on a timeout.
Nothing here is atomic across the four steps. Each step can succeed while the next fails, and a retry runs all four again.
2How Edges End Up in One Direction Only
Suppose the service writes the following row, then the process crashes, is deployed over, or times out talking to 456's shard before writing the followers row. The user sees an error or nothing at all, and the edge now exists in one direction only: 123's following list contains 456, but 456's followers list does not contain 123. If the order is the other way round in some code path, or an unfollow fails halfway, we get the opposite asymmetry. At a few thousand follows per second, even a 0.01 percent failure rate produces hundreds of thousands of broken edges in a year.
3How the Counters Drift
The counters drift for a related reason. A timeout does not tell the caller whether the write happened. If the first attempt actually incremented the counters and then timed out, the retry increments them again. The edge exists once, but the count went up by two. Unfollows have the same problem in reverse, and a follow and unfollow racing each other can each decide to increment or decrement based on stale state. Over a year, 0.5 percent of users accumulating a small error is exactly what this produces.
7 messages in order. Play them, or step through them one at a time.
A timeout does not mean the write failed; a blind retry repeats side effects.
4Redesigning the Write Path
We make one direction the record: the following edge on the follower's shard. The follow service writes it with an idempotent upsert, and in the same local transaction writes an outbox row describing the change, but only if the edge really changed state. A relay reads the outbox, or a CDC stream reads the change log, and applies the reverse edge and counter changes on the other shards, retrying until each is applied. Each outbox event has a unique id, and the consumers record applied ids, so a redelivered event changes nothing.
So a repeated follow request finds the edge already there, inserts no outbox row, and changes no counter. A redelivered event finds its id already applied and stops. The consumer's three statements run in one local transaction, so the dedup record, the reverse edge and the counter change land together. The reverse direction is now eventually consistent, typically within a second, but it can no longer be permanently missing.
5Handling Celebrity Accounts
A celebrity gaining 5,000 followers a minute sends all of those writes to one followers partition and one counter row. The row lock on the counter serialises every update, and the partition becomes a hot spot, which is why those writes time out. We split both. The followers table key becomes (target_id, bucket, source_id), where bucket is a hash of the source id modulo, say, 64 for accounts above a threshold, so writes spread over 64 partitions. Reads page through the buckets in turn, carrying the bucket number in the cursor. The counter becomes 64 sharded counter rows, each updated by the consumer for its bucket and summed on read, or summed periodically into a cached total.
| Symptom | Cause | Fix |
|---|---|---|
| Edge in one direction only | two shard writes, crash between | record edge + outbox, async reverse |
| Count off by a few | retry repeats increments | count only on real state change, dedup ids |
| Celebrity writes time out | one hot partition and counter row | buckets and sharded counters |
| Drift goes unnoticed | no checks | sampled audits and repair job |
6Repairing and Monitoring
The new design stops new drift, but the existing 0.5 percent must be repaired, and future bugs must be caught. A repair job recounts flagged users, those reported by support or found by audits, by counting their edges in the record table and fixing the counter and any missing reverse edges. For large accounts it counts bucket by bucket during quiet hours.
An audit job samples a few thousand random users every hour, compares the following table with the followers table in both directions, compares counts with list sizes, and publishes the mismatch rate as a metric. An alert fires if it rises above a small threshold, which tells us about a regression in days rather than a year.
7Follow-up questions
Why not use a distributed transaction across the two shards?
Two-phase commit would make the pair atomic, but it adds latency and locks rows across shards, and a coordinator failure can leave rows locked. For a follow graph, a reverse copy that lags by a second is acceptable, so an outbox gives us guaranteed eventual consistency with far less cost.
What if the outbox consumer is down for an hour?
Follows still succeed, because the record is written synchronously. The outbox fills up, and when the consumer returns it works through the backlog. Followers lists and counts are stale for that hour, which we would show on a dashboard as consumer lag.
Could counts be computed from the lists instead of stored?
For ordinary accounts with a few hundred edges, counting on read is cheap and always right, and some systems do that below a threshold. For large accounts it is far too slow, so stored counters with periodic correction are the practical answer.
8Conclusion
In this answer, we saw that one-direction edges come from two non-atomic writes to different shards, counter drift comes from retries that repeat increments, and celebrity timeouts come from one hot partition and counter row. Making the following edge the record, writing an outbox event only on a real state change, and applying the reverse edge and counters idempotently fixes the first two. Buckets and sharded counters fix the hot spot, and repair jobs with sampled audits correct the past and catch future drift.
Open with
All three symptoms come from the write path. We write the following row and the followers row on different shards with no transaction, so a crash or timeout between them leaves an edge in one direction.
Then, if they want more
A timeout also does not mean the write failed, so the client retries, and the counters get incremented twice; that is the 0.5 percent drift. And a celebrity's follows all hit one followers partition and one counter row, which serialises and times out.
I would make the following edge the record: an idempotent upsert, and in the same local transaction an outbox event, only if a row actually changed. A consumer applies the reverse edge and counter changes on the other shards, deduplicating by event id, and retries until applied, so the reverse direction is eventually consistent but never permanently missing.
For big accounts I split the followers key into target plus a bucket, say 64 buckets, page through buckets on read, and use sharded counters summed on read. Then a repair job recounts flagged users to fix existing drift, and an hourly audit samples users, compares both directions and counts, and alerts on the mismatch rate.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain how each of these follow-graph problems arises, and how you would redesign the...": https://scrivio-interview-library.netlify.app/architecture/system-design-problems/35a5b3a816/