Quick revision
The short answer
I would use one join table follows(follower_id, followee_id, created_at, notify) with primary key (follower_id, followee_id), which also serves 'who do I follow', plus a second index on (followee_id, created_at, follower_id) for 'who follows me' with keyset pagination. A CHECK (follower_id <> followee_id) blocks self-follows, foreign keys to users are fine in one database, and follower counts live in a counter rather than COUNT(*). At larger scale the table is partitioned or sharded by one side and the other direction is kept as a separate copy.
What to remember
In this answer, we modeled follows as a self-referencing join table with a composite primary key that also serves 'who do I follow', added an index on (followee_id, created_at, follower_id) for 'who follows me', paginated it with keyset cursors, blocked self-follows with a CHECK, and kept follower counts in counters updated transactionally or asynchronously for very popular accounts. At larger scale the table is sharded by one side and the reverse direction is kept as a reconciled copy.
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.
I would use one join table follows(follower_id, followee_id, created_at, notify) with primary key (follower_id, followee_id), which also serves 'who do I follow', plus a second index on (followee_id, created_at, follower_id) for 'who follows me' with keyset pagination. A CHECK (follower_id <> followee_id) blocks self-follows, foreign keys to users are fine in one database, and follower counts live in a counter rather than COUNT(*). At larger scale the table is partitioned or sharded by one side and the other direction is kept as a separate copy.
Why this question is asked
Follows look like a plain many-to-many relationship, but the access pattern makes them interesting: the same rows are read from two directions, some users have millions of followers, and every profile shows counts. Interviewers ask this design question to see whether we let the queries drive the keys and indexes, and whether we know how such a table behaves at scale. In this answer, we will model the relationship, design the indexes for both directions, paginate a long list stably, handle the counts, and then look at what changes when one database is no longer enough.
1A Quick Recap of Join Tables
A many-to-many relationship, where each user can follow many users and be followed by many, is stored in a join table: one row per pair. Because the relationship has its own data, a created_at and a notification setting, those columns go on the join row itself. Here both sides of the pair point to the same users table, which is called a self-referencing many-to-many.
The composite primary key does two jobs. It makes a pair unique, so pressing Follow twice cannot create two rows, and its index, ordered by follower_id first, answers 'who does user 5 follow' directly.
2Serving Both Directions With Two Indexes
The primary key index is sorted by follower_id, so it cannot help with 'who follows user 9': those rows are scattered all over it. We need a second index that starts with followee_id.
| Query | Index | Order of entries |
|---|---|---|
| Who does user 5 follow | Primary key | follower_id, followee_id |
| Who follows user 9 | follows_followee_idx | followee_id, created_at DESC, follower_id |
| Does 5 follow 9? | Primary key | Single entry lookup |
Notice the column order of the second index. Starting with followee_id groups one user's followers together, created_at DESC lets the database return them newest first straight from the index, and follower_id at the end makes every index entry unique and lets the query read follower_id without visiting the table. For a user with 3 million followers, the first page reads 50 index entries, not 3 million rows.
3Paginating a Follower List Stably
OFFSET pagination ('skip 100,000 rows, take 50') gets slower with every page, because the database still reads the skipped rows, and it shows duplicates or skips rows when new follows arrive between page loads. Keyset pagination instead remembers the last row of the previous page and asks for rows after it.
The row comparison (created_at, follower_id) < (...) matches the index order exactly, so each page is an index range scan starting at the cursor, as fast on page 10,000 as on page 1. The follower_id tie-breaker matters because many follows can share the same created_at; without it, rows at a page boundary could be skipped or repeated.
4Keeping Follower Counts Cheap
Every profile view shows 'followers' and 'following'. Counting 3 million index entries per view is far too slow. So we store the counts, for example in a user_stats table, and update them when a follow is created or removed.
Doing it in the same transaction keeps counts exact, but a celebrity's stats row becomes a hot spot: thousands of follows per second all wait for the same row lock. For such accounts we update counts asynchronously, by consuming follow events and applying increments in batches, and accept that the number lags a few seconds. A nightly job can recount and correct any drift. The application must only increment when the insert actually added a row, which is why the ON CONFLICT result is checked.
5When One Database Is Not Enough
At very large scale the follows table outgrows one server. We then shard it, splitting rows across databases by a key. If we shard by follower_id, 'who do I follow' stays on one shard, but 'who follows me' would have to ask every shard. The common answer is to store each relationship twice: a following table sharded by follower_id and a followers table sharded by followee_id, both written when a follow happens, usually by an event consumer for the second copy.
That copy is denormalization with a clear source of truth (the following side, say) and a reconciliation job, and the foreign keys to users can no longer be database constraints because users live elsewhere. Within one PostgreSQL instance, table partitioning by hash of followee_id can also keep indexes smaller, but it is not needed until the table holds billions of rows.
- Follow APIWrites the following row, publishes
FollowCreated - Following storeSharded by follower_id: who do I follow
- Event consumerWrites the mirrored row, updates counts
- Followers storeSharded by followee_id: who follows me
- Reconciliation jobCompares both sides, repairs drift
5 layers, from the top down. Step through them one at a time.
6Follow-up questions
How do you paginate a follower list stably?
With keyset pagination on a unique, indexed order: ORDER BY created_at DESC, follower_id DESC, and for each next page a WHERE (created_at, follower_id) < (last seen values). It is stable because new follows are inserted before the cursor and do not shift later pages, and fast because each page is an index range scan. The API returns the last values as an opaque cursor token.
Why not a surrogate id on follows?
Nothing references a follow row, and the natural pair is exactly what we look up, so a surrogate id would add a column and an index without serving any query. We would still need the unique constraint on the pair, so the composite primary key is simpler.
How would you query users that both of us follow?
Intersect two primary key range scans: SELECT followee_id FROM follows WHERE follower_id = 5 INTERSECT SELECT followee_id FROM follows WHERE follower_id = 7. Both sides read one user's following list in index order, which is fast for normal list sizes.
7Conclusion
In this answer, we modeled follows as a self-referencing join table with a composite primary key that also serves 'who do I follow', added an index on (followee_id, created_at, follower_id) for 'who follows me', paginated it with keyset cursors, blocked self-follows with a CHECK, and kept follower counts in counters updated transactionally or asynchronously for very popular accounts. At larger scale the table is sharded by one side and the reverse direction is kept as a reconciled copy.
Open with
I would make a join table follows with follower_id, followee_id, created_at and a notify boolean. The primary key is follower_id plus followee_id, which stops duplicate follows and, because it is ordered by follower first, answers who do I follow from the index.
Then, if they want more
For who follows me I add a second index on followee_id, created_at descending and follower_id, so a user's followers are grouped together, already in newest-first order, and the query can be answered from the index alone. A check constraint follower_id not equal to followee_id prevents self-follows, and inside one database foreign keys to users with cascade delete are fine.
For paging through millions of followers I would use keyset pagination, ordering by created_at and follower_id and passing the last pair seen as a cursor, so every page is a quick index range scan and new follows do not shift pages, unlike OFFSET.
Counts should not be COUNT star on every profile view, so I keep followers and following counters in a stats table, updated in the same transaction normally, or asynchronously in batches for celebrity accounts, whose row would otherwise be a lock hot spot, with a nightly recount.
At very large scale I would shard by follower and keep a mirrored followers table sharded by followee, maintained by events and reconciled.
About this answer
- Last updated
- 12 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 documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
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 "Users can follow other users, and the relationship needs a created_at, a notification...": https://scrivio-interview-library.netlify.app/databases/data-modeling/85274206ad/