Graph database: the account with 10 million followers
Step 1 · The pattern
Walk the relationships
'People you may know' walks from you to your friends and on to theirs. With about 300 links per account, each query touches a few thousand edges and takes about 200 ms. (All numbers are illustrative.)
- a query
- answered
In short
Graph database: the account with 10 million followers: how it works and how it breaks
A graph database stores things (people, products, accounts) and the relationships between them, and answers questions by walking those relationships: friends of friends, accounts that share a device, products bought together. The cost of a query is roughly the number of edges it touches. Most nodes have a few hundred edges, so walks are fast. Real graphs are not even, though: a celebrity, a popular product or a shared office Wi-Fi address can have millions, a so-called supernode. Any query that passes through it reads all those edges, holds the database for seconds, and slows everyone else's queries. The usual defences: index each node's edges by the properties queries filter on (time, type), so a query reads only the edges it needs; cap how far and how wide a traversal may go; and treat known dense nodes specially, by precomputing their answers or splitting their edges into buckets.
What this simulation simplifies
- Celebrity queries cost a fixed time here; real cost grows with the node's edges.
- The index is a flag; building one on a live graph takes time.
How this comes up in interviews
"Design 'people you may know' for a social network with a few accounts that have tens of millions of followers."
Then they ask
- What does a query cost?
- How do you handle the biggest accounts?
- Graph database or precomputed tables?
A strong answer covers
- Query cost is roughly the edges touched; the degree distribution is skewed.
- Vertex-centric indexes so traversals read only the edges they need.
- Limit depth and fan-out; sample or rank edges.
- Precompute answers for very dense nodes, or exclude them.
- Watch the largest nodes' degree over time.
Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.
On one page: the track cheat sheet.
Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.