Skip to content

Resource · Guide or article

Design the data structures for a social network

Asks for the shortest friend path between two people in a network too large for one machine, without using a graph database. Good practice for candidates comfortable with breadth-first search who want to see it adapted to sharded data.

Reviewed

Open the original: Design the data structures for a social network (opens the original site in a new tab)

Practice this topicOpens the practice interviewer in System design mode; choose a question there.

Add to a roadmap

Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.

Source content: English

A worked system design exercise from “The System Design Primer”. Steps: Outline use cases and constraints; Create a high level design; Design core components; Scale the design.

PrepKit overview

PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.

Suited to
The source does not state a level
Format
Guide

What it covers

  • Assumes 100 million users with 50 friends each on average, which gives 5 billion relationships and 400 searches per second.
  • Starts with a single-machine BFS in Python, then rewrites it for users spread across person servers found through a lookup service.
  • Includes Python sketches of the lookup service, person server and user graph service.
  • Lists optimizations: caching BFS results, batching lookups per server, sharding by location, searching from both ends, and hop or time limits.

How to use it

PrepKit study advice, not a claim made by the source.

Write the single-machine BFS first, then explain aloud what each extra network hop costs once friends live on different servers. You should be able to defend two of the listed optimizations and say what each one trades away.

Scope and limitations

One possible design under the exercise's own assumptions and its rule against graph databases. It covers friend search only. The code leaves out error handling on purpose and has small bugs in the distributed version, and several scaling ideas are listed in a single line without detail.

Access and format

README on GitHub in the donnemartin/system-design-primer repository by Donne Martin, licensed CC BY 4.0. English, free to read online.

Reviewed by PrepKit on Oct 2, 2026. Listing source commit: ae9bbd7b02d9

Evidence behind this overview
  • solutions/system_design/social_graph/README.md

Pages that include this

Get the PrepKit newsletter

Optional. You don't need it to use PrepKit.

How we handle your email