Scrivio Interview library
DesignFoundations7 min read · updated 13 November 2026

A social graph records who follows whom. Profile pages show follower and following counts and lists, the feed needs each person's followees, and the app often asks whether one person follows another. A few accounts have tens of millions of followers.

Design the API and data model of a follow service for 500 million users who follow 200 accounts on average, covering follow, unfollow, lists, counts and the does-A-follow-B check.

The API is PUT and DELETE /users/{a}/following/{b} to follow and unfollow, paged GET lists of followers and following, and GET /users/{a}/following/{b} for the check. With 500 million users following 200 accounts each, there are about 100 billion edges, so we store each edge twice: a following table partitioned by follower and a followers table partitioned by followee, which makes both lists single-partition reads and the check a point lookup on (source, target). Counts live in separate counters updated on every real follow or unfollow, lists page with a cursor on created_at plus id, and follow and unfollow are idempotent.

Why this question is asked

Follow graphs sit under almost every social product: they decide what goes into a feed, who gets notified, and what a profile shows. The design is a good interview question because the obvious single table works for a prototype and fails at scale in three predictable places: listing followers of a big account, counting them, and checking a relationship millions of times per second. In this answer, we will estimate the size, define the API, design the two-table data model, and then look at counts, paging, idempotency and the celebrity accounts that stress all of it.

1Requirements and Rough Numbers

Users follow and unfollow accounts, see follower and following counts on every profile, page through both lists, and the app constantly asks whether one user follows another, for example to draw a Follow or Following button on every post in a feed. The feed service needs each user's full following list.

The size comes straight from the numbers: 500 million users times 200 follows on average is 100 billion edges. An edge is two 8-byte user ids and a timestamp, about 24 bytes of data and perhaps 60 to 80 bytes with storage overhead, so one copy is around 6 to 8 TB and two copies are 12 to 16 TB before replication. Traffic is dominated by reads: the follow check can run at hundreds of thousands per second, list reads are frequent, and follows and unfollows are perhaps 10,000 to 30,000 per second at peak.

2The API

We model a follow as a resource: the edge from user a to user b. PUT creates it and DELETE removes it, which makes both naturally idempotent, since sending the same PUT twice leaves the same state. Lists use cursors, and the profile returns counts.

Java
PUT /users/123/following/456 -> 204 (follow; repeating it changes nothing)
DELETE /users/123/following/456 -> 204 (unfollow; repeating it changes nothing)
GET /users/123/following/456 -> 200 { "following": true, "since": "2026-05-01T10:00:00Z" }
GET /users/456/followers?limit=50 -> { "users": [...50 ids...], "next_cursor": "MTcxNDU1..." }
GET /users/456/followers?limit=50&cursor=MTcxNDU1...
GET /users/456 -> { "id": 456, "followers": 48210933, "following": 312 }

3Storing Each Edge Twice

A single table of (source, target) cannot be partitioned so that both questions are cheap. If we partition by source, listing who I follow reads one partition, but listing my followers must ask every partition. So we store each edge in two tables, each partitioned by its first column. The following table, keyed by (source, target), answers who does user a follow and does a follow b. The followers table, keyed by (target, source), answers who follows user b. Both lists then read one partition, and the check is a single point lookup in the following table.

The store can be a sharded relational database such as MySQL or PostgreSQL with shards chosen by the partition key, or a wide-column store such as Cassandra or ScyllaDB that partitions by design. Within a partition we also want the list ordered by time for display, so we add a local index on (source, created_at, target) or, in Cassandra, a second table clustered by time.

SQL
-- following: partitioned by source
CREATE TABLE following (
source_id BIGINT NOT NULL,
target_id BIGINT NOT NULL,
created_at TIMESTAMP NOT NULL,
PRIMARY KEY (source_id, target_id),
KEY by_time (source_id, created_at, target_id)
);
-- followers: partitioned by target
CREATE TABLE followers (
target_id BIGINT NOT NULL,
source_id BIGINT NOT NULL,
created_at TIMESTAMP NOT NULL,
PRIMARY KEY (target_id, source_id),
KEY by_time (target_id, created_at, source_id)
);
CREATE TABLE user_counts (user_id BIGINT PRIMARY KEY, followers BIGINT, following BIGINT);
SELECT 1 FROM following WHERE source_id = 123 AND target_id = 456; -- 1 row: yes, follows

Notice that the two tables usually live on different shards, because user 123's partition and user 456's partition are chosen independently. Writing both copies is therefore two writes, and we need a plan for keeping them in step, which we come to below.

4Counts, Paging and Idempotency

A celebrity can have 50 million followers. Running COUNT(*) on that partition for every profile view is impossible, so we keep counts in a user_counts table and change them only when an edge actually changes state. That condition is the important part: a repeated follow must not add 1 again. We get it by inserting the edge and checking whether a row was really inserted, and only then incrementing.

Lists use a cursor rather than an offset. With OFFSET 1000000, the database still walks past a million rows to find page 20,001. A cursor carries the last item's (created_at, id), and the next page asks for rows older than that, which the by_time index finds directly however deep we are.

SQL
-- follow: counts change only if the edge is new
INSERT INTO following (source_id, target_id, created_at) VALUES (123, 456, NOW())
ON DUPLICATE KEY UPDATE source_id = source_id; -- affected rows: 1 new, 0 already there
-- if 1: insert into followers, then increment both counters
-- next page of 456's followers after cursor (2026-05-01 10:00:00, 98765)
SELECT source_id, created_at FROM followers
WHERE target_id = 456
AND (created_at, source_id) < ('2026-05-01 10:00:00', 98765)
ORDER BY created_at DESC, source_id DESC
LIMIT 50; -- 50 rows, index range scan

Unfollow is the mirror image: delete the edge, and decrement only if a row was really deleted.

5The Write Path and Its Weak Spots

A follow therefore touches up to four places: the following row, the followers row, and two counters, on different shards. Doing all four in one distributed transaction would be slow and fragile. The usual design makes the following row the record, written first and synchronously, and propagates the followers row and the counters asynchronously through an outbox or change stream, retried until applied. The user sees the button change at once, and the follower list of the other account catches up within a second or so.

Step throughA follow from user 123 to user 456
ClientFollow servicefollowing shardOutbox relayfollowers shard
PUT /users/123/following/456
insert edge + outbox row, one transaction
1 row inserted
204, button shows Following
read outbox row
insert (456, 123), bump counters

6 messages in order. Play them, or step through them one at a time.

The record is written synchronously; the reverse copy and counters follow within a second.

Celebrity accounts stress this path: thousands of follows per minute land on one followers partition and one counter row. Splitting a big account's followers into several buckets and using sharded counters solves that, and it is worth mentioning in the interview even if the basic design does not need it on day one.

6Follow-up questions

Why not use a graph database?

For this workload, the queries are one hop: a list of neighbours or a single edge. Partitioned edge lists answer those with one partition read at very high rates, and they scale by adding shards. A graph database helps when we need multi-hop traversals, which are better served by separate offline jobs at this scale.

How would the feed service use this?

The feed reads a user's following list, up to a few thousand ids, from one partition of the following table, often cached in Redis because it changes rarely. For celebrities with tens of millions of followers, feeds usually pull their posts at read time rather than pushing to every follower.

What happens if a user follows someone, then immediately loads their own following list?

The following table is written synchronously, so their own list shows the new account at once. Only the other account's followers list and counts lag slightly, which users rarely notice. If needed, the client can also show its own optimistic state.

7Conclusion

In this answer, we designed a follow service for 100 billion edges. Each edge is stored twice, in a following table partitioned by follower and a followers table partitioned by followee, so both lists are single-partition reads and the check is a point lookup. Counts are separate counters that change only when an edge really changes, lists page with a (created_at, id) cursor, and follow and unfollow are idempotent PUT and DELETE calls. The following row is the record, and the reverse copy and counters are propagated asynchronously.

What to say in the interviewAbout 80 seconds aloud

Open with

First the size: 500 million users times 200 follows is 100 billion edges, roughly 6 to 8 TB per copy, and reads dominate, especially the does-A-follow-B check. The API is PUT and DELETE on /users/a/following/b, which are idempotent, paged GETs for followers and following, GET on the edge for the check, and counts on the profile.

Then, if they want more

I would store every edge twice: a following table keyed by (source, target) and partitioned by source, and a followers table keyed by (target, source) and partitioned by target. Then both lists are single-partition reads, and the check is a point lookup on (source, target).

Each table also has a time-ordered index so I can page with a cursor on created_at plus id instead of an offset, which keeps deep pages cheap.

Counts are separate counters per user, because counting 50 million rows per profile view is impossible, and they change only when an insert or delete really changed a row, so retries do not double count. The following row is the record, written synchronously with an outbox entry, and the followers row and counters are applied asynchronously.

For celebrities I would later split the followers partition into buckets and use sharded counters.

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 "Design the API and data model of a follow service for 500 million users who follow 200...": https://scrivio-interview-library.netlify.app/architecture/system-design-problems/7ad3b19d6d/