{"id":"design-social-graph","title":"Design the data structures for a social network","sections":[{"id":"overview","title":"Overview","html":"<p>A social network stores people and the friendships between them. When you open a stranger's profile on a professional network, the site often shows how you're connected: \"You → Priya → Marco → this person.\" That line is the <strong>shortest path</strong> between two people in the friendship graph. A <strong>graph</strong> here is just a set of people (called <strong>nodes</strong> or vertices) and the friendships that join pairs of them (called <strong>edges</strong>). The <strong>degree of separation</strong> between two people is how many friendships the shortest path uses.</p>\n<p>Finding that path for a handful of people is a first-year algorithms exercise. Doing it for 100 million people, whose friend lists don't fit on one machine, is a real design problem. Every step of the search needs someone's friend list, and those lists live on many different servers. A search that touches a few thousand people can mean a few thousand network calls, and a careless one can touch millions. Most of this design is about keeping that number small.</p>\n<p>This chapter follows the four steps from <a href=\"/learn/system-design-interview\">The system design interview, step by step</a>. It starts with breadth-first search on one machine, spreads the graph across servers, and then cuts the cost of a search by more than a thousand times with bidirectional search, depth limits and caching. Along the way it looks at how Facebook stores its own social graph.</p>","headings":[]},{"id":"step-1-outline-use-cases-constraints-and-assumptions","title":"Step 1: Outline use cases, constraints and assumptions","html":"<h3 id=\"step-1-outline-use-cases-constraints-and-assumptions\">Step 1: Outline use cases, constraints and assumptions</h3>\n<h4 id=\"use-cases\">Use cases</h4>\n<p>We'll scope the problem to these use cases:</p>\n<ul>\n<li><strong>A user searches for someone and sees the shortest path to that person.</strong> The answer is the list of people along the path, from the searcher to the target.</li>\n<li><strong>The service is highly available.</strong> Search should keep working when individual servers fail.</li>\n</ul>\n<h4 id=\"out-of-scope\">Out of scope</h4>\n<ul>\n<li><strong>Graph databases and other graph-specific products.</strong> The exercise asks you to build the graph from ordinary parts (sharded key-value storage, caches and services), so you understand what a graph database does for you. We'll come back to graph databases as a trade-off at the end.</li>\n<li><strong>Weighted edges.</strong> Every friendship counts as one step. \"Close friends count as half a step\" would need a different algorithm (Dijkstra's).</li>\n<li><strong>Finding the target's id.</strong> We assume the client already knows the person id of the profile being viewed. Name search is a separate system.</li>\n<li><strong>Adding and removing friends,</strong> friend suggestions (\"people you may know\"), privacy rules about who can see whose friends, and the news feed.</li>\n</ul>\n<h4 id=\"assumptions\">Assumptions</h4>\n<ul>\n<li>There are <strong>100 million users</strong>.</li>\n<li>Each user has <strong>50 friends on average</strong>. Some have far more.</li>\n<li>Users run <strong>1 billion friend searches a month</strong>.</li>\n<li>Traffic is uneven. Some searches are popular and repeated many times, and others are run only once.</li>\n<li>Friendships are mutual and unweighted: if Ana is Ben's friend, Ben is Ana's friend.</li>\n<li>The graph won't fit on a single machine.</li>\n</ul>\n<h4 id=\"calculate-usage\">Calculate usage</h4>\n<p><strong>Seconds in a month.</strong> 30 × 86,400 s = 2,592,000 s, about 2.6 million.</p>\n<p><strong>Search rate.</strong> 1,000,000,000 searches ÷ 2,592,000 s ≈ <strong>386 searches a second</strong> on average. Round it to 400. Traffic is uneven, so plan for a peak of about 3× the average: roughly <strong>1,200 searches a second</strong>.</p>\n<p><strong>Friend-list entries.</strong> 100 million users × 50 friends = <strong>5 billion entries</strong> across all friend lists. Each friendship appears twice, once in each person's list, so that's <strong>2.5 billion distinct friendships</strong>.</p>\n<p><strong>Friend-list storage.</strong> A person id is a 64-bit integer, which is 8 bytes. 5 billion entries × 8 bytes = <strong>40 GB</strong> of friend ids. One friend list averages 50 × 8 = <strong>400 bytes</strong>.</p>\n<p><strong>Person records.</strong> Add an id (8 bytes), a name (about 40 bytes) and a few other fields (about 50 bytes) to the 400-byte friend list, and a person is about <strong>500 bytes</strong>. 100 million × 500 bytes = <strong>50 GB</strong> of raw data. Data held in memory as hash tables and objects usually takes several times its raw size, so budget 4×: about <strong>200 GB</strong> of memory to hold everyone.</p>\n<p><strong>Cost of one search.</strong> This is the number that drives the design. Say the target is 4 friendships away, and assume no overlap between friend lists (real lists overlap, which makes the true numbers smaller). A plain search from the searcher outward has to read the friend lists of everyone within 3 steps before it can see people at step 4:</p>\n<ul>\n<li>1 (the searcher) + 50 + 2,500 + 125,000 = <strong>127,551 friend-list reads</strong> for one search.</li>\n<li>At 386 searches a second, that's 386 × 127,551 ≈ <strong>49 million friend-list reads a second</strong>.</li>\n<li>In bytes, 127,551 × 400 bytes ≈ 51 MB per search, or about <strong>20 GB a second</strong> moved across the network.</li>\n</ul>\n<p><strong>What these numbers tell us.</strong> Storage is modest. 50 GB of raw data, or 200 GB in memory, spread across a handful of servers is easy to hold, and all the friend lists together (40 GB) could fit in a cache cluster. The search rate is modest too: 400 a second is low for a web service. The problem is the fan-out of each search. <strong>Fan-out</strong> is how many new people each step reaches; with 50 friends each, every extra step multiplies the work by 50. A naive search turns 400 user requests a second into tens of millions of internal reads a second, each one possibly a network call to another machine. Step 4 shows how bidirectional search brings 127,551 reads down to about 100.</p>\n<p>The <a href=\"/learn/estimation\">estimation</a> chapter covers the method behind this kind of arithmetic.</p>","headings":[{"id":"step-1-outline-use-cases-constraints-and-assumptions","title":"Step 1: Outline use cases, constraints and assumptions","depth":3},{"id":"use-cases","title":"Use cases","depth":4},{"id":"out-of-scope","title":"Out of scope","depth":4},{"id":"assumptions","title":"Assumptions","depth":4},{"id":"calculate-usage","title":"Calculate usage","depth":4}]},{"id":"step-2-create-a-high-level-design","title":"Step 2: Create a high-level design","html":"<h3 id=\"step-2-create-a-high-level-design\">Step 2: Create a high-level design</h3>\n<figure class=\"diagram\" data-text=\"Client -> Web server (reverse proxy) (HTTPS); Web server (reverse proxy) -> Search API; Search API -> User graph service; User graph service -> Lookup service (which server?); User graph service -> Person servers (sharded) (friend lists)\"><div class=\"diagram-scroll\"><svg class=\"dg\" viewBox=\"0 -10 800 275\" width=\"800\" role=\"img\" aria-label=\"The friend-search path. Person data is sharded across person servers.\"><defs><marker id=\"dg-arrow\" viewBox=\"0 0 10 10\" refX=\"9\" refY=\"5\" markerWidth=\"7\" markerHeight=\"7\" orient=\"auto-start-reverse\"><path class=\"dg-arrowhead\" d=\"M0,1 L9,5 L0,9 z\"></path></marker></defs><line class=\"dg-edge\" x1=\"151\" y1=\"42\" x2=\"167\" y2=\"42\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"311\" y1=\"42\" x2=\"327\" y2=\"42\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"471\" y1=\"42\" x2=\"487\" y2=\"42\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"534.2\" y1=\"71\" x2=\"427.6\" y2=\"191\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"585.8\" y1=\"71\" x2=\"688\" y2=\"186\" marker-end=\"url(#dg-arrow)\"></line><rect class=\"dg-node dg-client\" x=\"12\" y=\"18\" width=\"136\" height=\"48\" rx=\"24\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"80\" y=\"46.5\">Client</tspan></text><rect class=\"dg-node dg-box\" x=\"172\" y=\"16\" width=\"136\" height=\"52\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"240\" y=\"38.5\">Web server</tspan><tspan x=\"240\" y=\"54.5\">(reverse proxy)</tspan></text><rect class=\"dg-node dg-box\" x=\"332\" y=\"18\" width=\"136\" height=\"48\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"400\" y=\"46.5\">Search API</tspan></text><rect class=\"dg-node dg-box\" x=\"492\" y=\"16\" width=\"136\" height=\"52\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"560\" y=\"38.5\">User graph</tspan><tspan x=\"560\" y=\"54.5\">service</tspan></text><rect class=\"dg-node dg-box\" x=\"332\" y=\"196\" width=\"136\" height=\"52\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"400\" y=\"218.5\">Lookup</tspan><tspan x=\"400\" y=\"234.5\">service</tspan></text><path class=\"dg-node dg-store\" d=\"M652,198 a68,7 0 0,1 136,0 v48 a68,7 0 0,1 -136,0 z\"></path><path class=\"dg-line\" d=\"M652,198 a68,7 0 0,0 136,0\"></path><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"720\" y=\"221.5\">Person servers</tspan><tspan x=\"720\" y=\"237.5\">(sharded)</tspan></text><rect class=\"dg-edge-bg\" x=\"138.5\" y=\"-6\" width=\"41\" height=\"18\" rx=\"4\"></rect><text class=\"dg-edge-label\" x=\"159\" y=\"7\" text-anchor=\"middle\">HTTPS</text><rect class=\"dg-edge-bg\" x=\"435.6\" y=\"122\" width=\"90.6\" height=\"18\" rx=\"4\"></rect><text class=\"dg-edge-label\" x=\"480.9\" y=\"135\" text-anchor=\"middle\">which server?</text><rect class=\"dg-edge-bg\" x=\"594.7\" y=\"119.5\" width=\"84.4\" height=\"18\" rx=\"4\"></rect><text class=\"dg-edge-label\" x=\"636.9\" y=\"132.5\" text-anchor=\"middle\">friend lists</text></svg></div><figcaption>The friend-search path. Person data is sharded across person servers.</figcaption></figure>\n<ul>\n<li><strong>Client:</strong> the web or mobile app. It asks for the path from the signed-in user to a profile.</li>\n<li><strong>Web server (reverse proxy):</strong> terminates HTTPS, handles authentication and forwards requests to the API servers. See <a href=\"/learn/reverse-proxy-and-api-gateway#what-a-reverse-proxy-is\">reverse proxies</a>.</li>\n<li><strong>Search API:</strong> validates the request, applies rate limits and calls the user graph service. It keeps the public API separate from the internal graph code.</li>\n<li><strong>User graph service:</strong> runs the shortest-path search. It holds the search state (who has been visited, how each person was reached) and fetches friend lists as it needs them.</li>\n<li><strong>Lookup service:</strong> tells the user graph service which person server holds a given person.</li>\n<li><strong>Person servers:</strong> each holds the records, including friend lists, for one slice of the users. Together they hold the whole graph.</li>\n</ul>","headings":[{"id":"step-2-create-a-high-level-design","title":"Step 2: Create a high-level design","depth":3}]},{"id":"step-3-design-core-components","title":"Step 3: Design core components","html":"<h3 id=\"step-3-design-core-components\">Step 3: Design core components</h3>\n<h4 id=\"use-case-user-searches-for-someone-and-sees-the-shortest-path-to-the-searched-person\">Use case: User searches for someone and sees the shortest path to the searched person</h4>\n<h5 id=\"on-one-machine-breadth-first-search\">On one machine: breadth-first search</h5>\n<p>Start small. Suppose five people, where an arrow means \"is friends with\":</p>\n<ul>\n<li>Ana (1) is friends with Ben (2) and Chen (3).</li>\n<li>Ben and Chen are both friends with Dev (4).</li>\n<li>Dev is friends with Eva (5).</li>\n</ul>\n<p>The shortest path from Ana to Eva is Ana → Ben → Dev → Eva, three friendships. (Ana → Chen → Dev → Eva is just as short; either answer is correct.)</p>\n<p><strong>Breadth-first search (BFS)</strong> finds this path. It explores the graph in rings. First it looks at everyone 1 step from the start, then everyone 2 steps away, then 3, and so on. Because it finishes each ring before starting the next, the first time it reaches the target it has used the fewest possible steps. This only works when every edge costs the same, which is true here.</p>\n<p>BFS keeps two things:</p>\n<ul>\n<li>A <strong>queue</strong> of people waiting to be expanded, in the order they were found. <strong>Expanding</strong> a person means reading their friend list.</li>\n<li>A <strong>parent</strong> map that records, for each person found, who we reached them from. It doubles as the \"visited\" set, so nobody is queued twice. At the end, we walk the parent map backwards from the target to rebuild the path.</li>\n</ul>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">from</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> collections </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">import</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> deque</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> shortest_path</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(friends, source, dest):</span></span>\n<span class=\"line\"><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">    \"\"\"friends maps person id -> list of friend ids.</span></span>\n<span class=\"line\"><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">    Returns the ids along a shortest path, or None if there is no path.\"\"\"</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> source </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">==</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> dest:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> [source]</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    parent </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> {source: </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">}          </span><span style=\"color:#6A737D;--shiki-dark:#6A737D\"># who we reached each person from</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    queue </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> deque([source])</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    while</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> queue:</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        person </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> queue.popleft()</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> friend </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> friends.get(person, ()):</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> friend </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> parent:     </span><span style=\"color:#6A737D;--shiki-dark:#6A737D\"># already found by a path at least as short</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">                continue</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">            parent[friend] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> person</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> friend </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">==</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> dest:       </span><span style=\"color:#6A737D;--shiki-dark:#6A737D\"># stop as soon as the target is found</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">                return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> walk_back(parent, dest)</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">            queue.append(friend)</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> walk_back</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(parent, node):</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    path </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> []</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    while</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> node </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">is</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> not</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        path.append(node)</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        node </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> parent[node]</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    path.reverse()</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> path</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">friends </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> {</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">1</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">: [</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">2</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">3</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">], </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">2</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">: [</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">1</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">4</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">], </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">3</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">: [</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">1</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">4</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">], </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">4</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">: [</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">2</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">3</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">5</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">], </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">5</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">: [</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">4</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">]}</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">shortest_path(friends, </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">1</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">5</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">)          </span><span style=\"color:#6A737D;--shiki-dark:#6A737D\"># [1, 2, 4, 5]</span></span></code></pre>\n<p>Two details matter later:</p>\n<ol>\n<li>The check <code>friend == dest</code> happens when a person is first <strong>discovered</strong>, before they're taken off the queue. Checking at discovery saves expanding a whole ring. In a graph where each ring is 50 times bigger than the last, that's a large saving.</li>\n<li>Compare ids with <code>==</code>. In Python, <code>is</code> checks whether two values are the same object in memory, and two equal large integers often aren't.</li>\n</ol>\n<p>On one machine, BFS takes time proportional to the people and friendships it touches. With everything in memory, each friend-list read takes well under a microsecond.</p>\n<h5 id=\"why-it-doesnt-fit-on-one-machine\">Why it doesn't fit on one machine</h5>\n<p>From step 1, the person records come to about 50 GB raw and about 200 GB as in-memory structures. Some large servers have that much memory, so why not use one?</p>\n<ul>\n<li><strong>Growth.</strong> 100 million users is our starting point. Facebook's 2016 study covered 1.59 billion active people with \"hundreds of billions of friendship connections.\" A design that needs one machine stops working well before that.</li>\n<li><strong>Throughput.</strong> Even the efficient search we'll build does about 100 friend-list reads per search. At peak, that's over 100,000 reads a second, plus the writes when people add and remove friends. Spreading the data spreads that load.</li>\n<li><strong>Availability.</strong> One machine is a single point of failure. If it goes down, every search fails.</li>\n</ul>\n<p>So we split the people across machines, which is called <strong>sharding</strong>. Each piece is a <strong>shard</strong>.</p>\n<h5 id=\"sharding-people-across-person-servers\">Sharding people across person servers</h5>\n<p>A <strong>person server</strong> holds the records for one or more shards and answers one question quickly: \"Give me the records for these person ids.\" Each record includes the person's friend list, so reading someone's friends is one lookup on one server.</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">from</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> dataclasses </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">import</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> dataclass</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#6F42C1;--shiki-dark:#B392F0\">@dataclass</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> Person</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">    id</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">: </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">int</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    name: </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">str</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    friend_ids: list[</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">int</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">]</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> PersonServer</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> __init__</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self):</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.people </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> {}                      </span><span style=\"color:#6A737D;--shiki-dark:#6A737D\"># person id -> Person</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> add_person</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, person):</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.people[person.id] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> person</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> get_people</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, ids):</span></span>\n<span class=\"line\"><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">        \"\"\"Batch read: the records this server holds for the given ids.\"\"\"</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> [</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.people[i] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> i </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> ids </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> i </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.people]</span></span></code></pre>\n<p><code>get_people</code> takes a list of ids on purpose. One call that returns 40 people costs about the same network time as a call that returns one, and step 4 relies on that.</p>\n<p>Which shard does a person go in? The simplest choice that spreads load evenly is to hash the person id: <code>shard = hash(person_id) mod number_of_shards</code>. Hashing scatters people, and their friends, across all shards. That's good for balance and bad for locality: a person's 50 friends are probably on 50 different shards. Step 4 looks at sharding by location instead. The <a href=\"/learn/database-internals#sharding\">sharding section</a> of the database chapter compares partitioning schemes in general.</p>\n<h5 id=\"the-lookup-service\">The lookup service</h5>\n<p>The user graph service needs to know where each person lives. The <strong>lookup service</strong> answers \"which person server holds person 1234?\"</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> LookupService</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> __init__</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, person_to_server):</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.person_to_server </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> person_to_server   </span><span style=\"color:#6A737D;--shiki-dark:#6A737D\"># person id -> PersonServer</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> lookup_person_server</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, person_id):</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.person_to_server[person_id]</span></span></code></pre>\n<p>This version is a <strong>directory</strong>: an explicit table with one row per person. A directory is flexible, because you can move one heavy user to a quieter server by changing one row. It's also big. 100 million rows of an 8-byte id and a 2-byte server number is 1 GB raw, several GB as a hash table, and it must be replicated so it doesn't become a single point of failure. The <a href=\"/learn/database-internals#directory-partitioning\">directory partitioning</a> section covers this trade-off. Step 4 replaces most of it with a computation.</p>\n<h5 id=\"the-user-graph-service-runs-the-search\">The user graph service runs the search</h5>\n<p>The <strong>user graph service</strong> runs the same BFS, with one change: it doesn't have the friend lists. For each person it expands, it does two network calls:</p>\n<ol>\n<li>Ask the lookup service which person server holds that person.</li>\n<li>Ask that person server for the person's record and read <code>friend_ids</code>.</li>\n</ol>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">from</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> collections </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">import</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> deque</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">class</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> UserGraphService</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> __init__</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, lookup_service):</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">        self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.lookup </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> lookup_service</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> get_person</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, person_id):</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        server </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.lookup.lookup_person_server(person_id)   </span><span style=\"color:#6A737D;--shiki-dark:#6A737D\"># network call 1</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        people </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> server.get_people([person_id])                </span><span style=\"color:#6A737D;--shiki-dark:#6A737D\"># network call 2</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> people[</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">0</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> people </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">else</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> shortest_path</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, source_id, dest_id):</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> source_id </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">==</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> dest_id:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> [source_id]</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        parent </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> {source_id: </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">}</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        queue </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> deque([source_id])</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        while</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> queue:</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">            person </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.get_person(queue.popleft())</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> friend_id </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> person.friend_ids:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">                if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> friend_id </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> parent:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">                    continue</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">                parent[friend_id] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> person.id</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">                if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> friend_id </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">==</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> dest_id:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">                    return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> walk_back(parent, dest_id)</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">                queue.append(friend_id)</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span></span></code></pre>\n<p>Notice that the queue and the parent map hold <strong>ids</strong> only. The search fetches a person's record only when it expands them. The parent map lives in the user graph service, because a person server shouldn't store per-search state, and many searches run at once.</p>\n<p>An easy mistake is to fetch every friend's record as soon as they're discovered. That fetches 50 records for every person expanded, most of which are never expanded, and multiplies the network calls by the fan-out.</p>\n<p>This code is correct and slow. Each expansion costs two network round trips. Assume a round trip inside one data center takes about 0.5 ms, the figure in the widely shared latency table credited to Jeff Dean and Peter Norvig. Expanding the 127,551 people from step 1 one at a time takes 127,551 × 2 × 0.5 ms ≈ <strong>128 seconds</strong>. Step 4 fixes that.</p>\n<p>Internal calls between the user graph service, lookup service and person servers use <strong>RPC</strong> (remote procedure calls), such as gRPC. They're private, high-volume and latency-sensitive, so a compact binary protocol suits them better than JSON over REST. See <a href=\"/learn/protocols-and-apis#rpc-calling-a-function-on-another-machine\">RPC</a>.</p>\n<h5 id=\"the-api\">The API</h5>\n<p>The public API is REST. The signed-in user is the source, so the request names only the target:</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">GET</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> /api/v1/friend_search?person_id=1234 </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">HTTP</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">/</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">1.1</span></span>\n<span class=\"line\"><span style=\"color:#22863A;--shiki-dark:#85E89D\">Host</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">:</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> social.example.com</span></span>\n<span class=\"line\"><span style=\"color:#22863A;--shiki-dark:#85E89D\">Authorization</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">:</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> Bearer &#x3C;token></span></span></code></pre>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">HTTP</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">/</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">1.1</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 200</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> OK</span></span>\n<span class=\"line\"><span style=\"color:#22863A;--shiki-dark:#85E89D\">Content-Type</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">:</span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\"> application/json</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">{</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">  \"degree\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">: </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">2</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">,</span></span>\n<span class=\"line\"><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">  \"path\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">: [</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    {</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">\"person_id\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">: </span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"100\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">\"name\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">: </span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"foo\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">\"link\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">: </span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"https://social.example.com/foo\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">},</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    {</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">\"person_id\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">: </span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"53\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">,  </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">\"name\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">: </span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"bar\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">\"link\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">: </span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"https://social.example.com/bar\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">},</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    {</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">\"person_id\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">: </span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"1234\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">,</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">\"name\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">: </span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"baz\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">, </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">\"link\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">: </span><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">\"https://social.example.com/baz\"</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">}</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">  ]</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">}</span></span></code></pre>\n<p>The path starts with the searcher (person 100) and ends with the target (person 1234). <code>degree</code> is the number of friendships on the path, which is one less than the number of people. If no path exists within the search limit (step 4), the response is <code>200 OK</code> with <code>\"path\": null</code> and a <code>\"reason\"</code> such as <code>\"beyond_search_limit\"</code>. It's a normal answer, so the status stays 200.</p>\n<p>Person ids are strings in the JSON because JavaScript numbers lose precision above 2⁵³, and 64-bit ids can be larger than that.</p>","headings":[{"id":"step-3-design-core-components","title":"Step 3: Design core components","depth":3},{"id":"use-case-user-searches-for-someone-and-sees-the-shortest-path-to-the-searched-person","title":"Use case: User searches for someone and sees the shortest path to the searched person","depth":4}]},{"id":"step-4-scale-the-design","title":"Step 4: Scale the design","html":"<h3 id=\"step-4-scale-the-design\">Step 4: Scale the design</h3>\n<figure class=\"diagram\" data-text=\"Client -> DNS; Client -> Load balancer; Load balancer -> Web servers; Web servers -> Search API; Search API -> User graph service; User graph service -> Lookup service; User graph service -> Memory cache (friend lists); User graph service -> Person servers + replicas; User graph service -> Precomputed traversals (NoSQL); Person servers + replicas -> Offline batch jobs (nightly export); Offline batch jobs -> Precomputed traversals (NoSQL)\"><div class=\"diagram-scroll\"><svg class=\"dg\" viewBox=\"0 0 800 620\" width=\"800\" role=\"img\" aria-label=\"The scaled design. Internal load balancers are left out.\"><defs><marker id=\"dg-arrow\" viewBox=\"0 0 10 10\" refX=\"9\" refY=\"5\" markerWidth=\"7\" markerHeight=\"7\" orient=\"auto-start-reverse\"><path class=\"dg-arrowhead\" d=\"M0,1 L9,5 L0,9 z\"></path></marker></defs><line class=\"dg-edge\" x1=\"80\" y1=\"69\" x2=\"80\" y2=\"193\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"151\" y1=\"42\" x2=\"167\" y2=\"42\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"311\" y1=\"42\" x2=\"327\" y2=\"42\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"471\" y1=\"42\" x2=\"487\" y2=\"42\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"560\" y1=\"69\" x2=\"560\" y2=\"191\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"489\" y1=\"222\" x2=\"473\" y2=\"222\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"631\" y1=\"222\" x2=\"647\" y2=\"222\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"560\" y1=\"251\" x2=\"560\" y2=\"366\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge\" x1=\"585.8\" y1=\"251\" x2=\"688\" y2=\"366\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge dg-dashed\" x1=\"590.2\" y1=\"436\" x2=\"692.4\" y2=\"551\" marker-end=\"url(#dg-arrow)\"></line><line class=\"dg-edge dg-dashed\" x1=\"720\" y1=\"553\" x2=\"720\" y2=\"438\" marker-end=\"url(#dg-arrow)\"></line><rect class=\"dg-node dg-client\" x=\"12\" y=\"18\" width=\"136\" height=\"48\" rx=\"24\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"80\" y=\"46.5\">Client</tspan></text><rect class=\"dg-node dg-box\" x=\"12\" y=\"198\" width=\"136\" height=\"48\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"80\" y=\"226.5\">DNS</tspan></text><rect class=\"dg-node dg-box\" x=\"172\" y=\"18\" width=\"136\" height=\"48\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"240\" y=\"46.5\">Load balancer</tspan></text><rect class=\"dg-node dg-box\" x=\"332\" y=\"18\" width=\"136\" height=\"48\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"400\" y=\"46.5\">Web servers</tspan></text><rect class=\"dg-node dg-box\" x=\"492\" y=\"18\" width=\"136\" height=\"48\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"560\" y=\"46.5\">Search API</tspan></text><rect class=\"dg-node dg-box\" x=\"332\" y=\"196\" width=\"136\" height=\"52\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"400\" y=\"218.5\">Lookup</tspan><tspan x=\"400\" y=\"234.5\">service</tspan></text><rect class=\"dg-node dg-box\" x=\"492\" y=\"196\" width=\"136\" height=\"52\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"560\" y=\"218.5\">User graph</tspan><tspan x=\"560\" y=\"234.5\">service</tspan></text><path class=\"dg-node dg-store\" d=\"M652,198 a68,7 0 0,1 136,0 v48 a68,7 0 0,1 -136,0 z\"></path><path class=\"dg-line\" d=\"M652,198 a68,7 0 0,0 136,0\"></path><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"720\" y=\"221.5\">Memory cache</tspan><tspan x=\"720\" y=\"237.5\">(friend lists)</tspan></text><path class=\"dg-node dg-store\" d=\"M492,378 a68,7 0 0,1 136,0 v48 a68,7 0 0,1 -136,0 z\"></path><path class=\"dg-line\" d=\"M492,378 a68,7 0 0,0 136,0\"></path><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"560\" y=\"401.5\">Person servers</tspan><tspan x=\"560\" y=\"417.5\">+ replicas</tspan></text><path class=\"dg-node dg-store\" d=\"M652,378 a68,7 0 0,1 136,0 v48 a68,7 0 0,1 -136,0 z\"></path><path class=\"dg-line\" d=\"M652,378 a68,7 0 0,0 136,0\"></path><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"720\" y=\"401.5\">Precomputed</tspan><tspan x=\"720\" y=\"417.5\">traversals (NoSQL)</tspan></text><rect class=\"dg-node dg-box\" x=\"652\" y=\"556\" width=\"136\" height=\"52\" rx=\"8\"></rect><text class=\"dg-label\" text-anchor=\"middle\"><tspan x=\"720\" y=\"578.5\">Offline</tspan><tspan x=\"720\" y=\"594.5\">batch jobs</tspan></text><rect class=\"dg-edge-bg\" x=\"592.9\" y=\"484.5\" width=\"96.8\" height=\"18\" rx=\"4\"></rect><text class=\"dg-edge-label\" x=\"641.3\" y=\"497.5\" text-anchor=\"middle\">nightly export</text></svg></div><figcaption>The scaled design. Internal load balancers are left out.</figcaption></figure>\n<p>Don't jump straight to this picture in an interview. Describe the loop: load-test the simple design, profile it to find the bottleneck, fix that bottleneck while weighing alternatives, and repeat. <a href=\"/learn/design-scaling-aws\">Design a system that scales to millions of users on AWS</a> walks through that loop step by step.</p>\n<h4 id=\"find-the-bottlenecks\">Find the bottlenecks</h4>\n<p>The step-1 numbers point at one thing. At about 400 searches a second, the front of the system (load balancer, web servers, search API) carries light traffic and scales by adding stateless copies. The trouble is inside each search:</p>\n<ol>\n<li><strong>Too many people expanded.</strong> A one-sided BFS to depth 4 reads about 127,551 friend lists.</li>\n<li><strong>Too many network round trips.</strong> Each read costs a call to the lookup service plus a call to a person server, one after the other.</li>\n<li><strong>Repeated work.</strong> Popular searches run again and again, and well-connected people's friend lists are read by many searches.</li>\n</ol>\n<p>The subsections below take these in order of impact, after a quick word on the front end.</p>\n<h4 id=\"load-balancer-web-servers-and-the-search-api\">Load balancer, web servers and the search API</h4>\n<ul>\n<li><strong>DNS</strong> points the client at the load balancer.</li>\n<li>A <strong>load balancer</strong> spreads requests across several <strong>web servers</strong> and <strong>search API</strong> servers. They hold no state between requests, so you scale them by adding copies, and a failed copy is simply taken out of rotation. See <a href=\"/learn/load-balancing\">load balancing</a> and <a href=\"/learn/load-balancing#horizontal-scaling-and-what-it-costs\">horizontal scaling</a>.</li>\n<li>The <strong>user graph service</strong> is stateless between searches too (its BFS state lives only for one request), so it scales the same way.</li>\n</ul>\n<p>This fixes availability at the front and costs some more machines to run. It does nothing for the cost of a search.</p>\n<h4 id=\"search-from-both-ends-bidirectional-bfs\">Search from both ends: bidirectional BFS</h4>\n<p><strong>Bidirectional BFS</strong> runs two searches at once: one outward from the searcher and one outward from the target. When a person discovered by one side has already been seen by the other, the two halves meet, and the path is the first half joined to the second half reversed. Friendships are mutual, so the backward search uses the same friend lists.</p>\n<p>Here is the arithmetic, with 50 friends each and no overlap between lists. Take a target 4 steps away:</p>\n<ul>\n<li><strong>One-sided BFS</strong> expands depths 0, 1, 2 and 3 before it discovers depth 4: 1 + 50 + 2,500 + 125,000 = <strong>127,551 friend-list reads</strong>.</li>\n<li><strong>Bidirectional BFS</strong> needs each side to reach depth 2, so each side expands only depths 0 and 1: 1 + 50 = 51 reads per side, <strong>102 in total</strong>.</li>\n</ul>\n<p>That's about <strong>1,250 times less work</strong>. A ring at depth d holds about 50^d people, and splitting the path in half turns one 50⁴ problem into two 50² problems. In general, with fan-out b and distance d, a one-sided search costs about b^d and a two-sided search about 2 × b^(d/2).</p>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<div class=\"table-wrap\"><table><thead><tr><th>Distance to target</th><th>One-sided reads</th><th>Two-sided reads</th><th>Ratio</th></tr></thead><tbody><tr><td>4 steps, 50 friends</td><td>127,551</td><td>102</td><td>about 1,250×</td></tr><tr><td>5 steps, 50 friends</td><td>6,377,551</td><td>2,602</td><td>about 2,450×</td></tr><tr><td>4 steps, 100 friends</td><td>1,010,101</td><td>202</td><td>about 5,000×</td></tr></tbody></table></div>\n<p>(For 5 steps, one side reaches depth 3 and the other depth 2, so the reads are 1 + 50 + 2,500 on one side and 1 + 50 on the other.)</p>\n<p>The higher the fan-out, the bigger the win. At 386 searches a second and 102 reads each, the system does about 39,400 friend-list reads a second, or roughly 118,000 at peak. That's about 16 MB a second of friend lists instead of 20 GB.</p>\n<p>Is 4 or 5 steps realistic? Facebook's 2016 research post \"Three and a half degrees of separation\" estimated that across its 1.59 billion active people, the average distance was 4.57 friendships, which is 3.57 people in between. Within the US it was 3.46 degrees. So most pairs of people sit 4 or 5 steps apart, which is right where bidirectional search pays off most.</p>\n<p>The code below adds two methods to <code>UserGraphService</code>. It expands one ring at a time. Each time, it expands whichever side has the smaller frontier (the set of people found in the last ring), because a smaller frontier means fewer friend lists to read.</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> shortest_path_bidirectional</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, source_id, dest_id, max_hops</span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">6</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">):</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> source_id </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">==</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> dest_id:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> [source_id]</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    parents_fwd, parents_bwd </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> {source_id: </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">}, {dest_id: </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">}</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    frontier_fwd, frontier_bwd </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> [source_id], [dest_id]</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    hops </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 0</span><span style=\"color:#6A737D;--shiki-dark:#6A737D\">                        # depth searched so far, both sides combined</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    while</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> frontier_fwd </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">and</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> frontier_bwd </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">and</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> hops </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">&#x3C;</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> max_hops:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        if</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> len</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(frontier_fwd) </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">&#x3C;=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> len</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(frontier_bwd):</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">            frontier_fwd, meet </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">._expand(frontier_fwd, parents_fwd, parents_bwd)</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        else</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">            frontier_bwd, meet </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">._expand(frontier_bwd, parents_bwd, parents_fwd)</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        hops </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">+=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> 1</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> meet </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">is</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> not</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> join(meet, parents_fwd, parents_bwd)</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    return</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span><span style=\"color:#6A737D;--shiki-dark:#6A737D\">                     # no path within max_hops</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> _expand</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, frontier, parents, other_parents):</span></span>\n<span class=\"line\"><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">    \"\"\"Expand one whole ring. Returns the next ring and a meeting person, if any.\"\"\"</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    friend_lists </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.friends_of(frontier)     </span><span style=\"color:#6A737D;--shiki-dark:#6A737D\"># batched, see the next section</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    next_frontier </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> []</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> person_id </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> frontier:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> friend_id </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> friend_lists.get(person_id, ()):</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> friend_id </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> parents:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">                continue</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">            parents[friend_id] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> person_id</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">            if</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> friend_id </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> other_parents:</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">                return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> next_frontier, friend_id</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">            next_frontier.append(friend_id)</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> next_frontier, </span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">None</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> join</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(meet, parents_fwd, parents_bwd):</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    path </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> walk_back(parents_fwd, meet)          </span><span style=\"color:#6A737D;--shiki-dark:#6A737D\"># source ... meet</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    node </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> parents_bwd[meet]</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    while</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> node </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">is</span><span style=\"color:#D73A49;--shiki-dark:#F97583\"> not</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\"> None</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">:                      </span><span style=\"color:#6A737D;--shiki-dark:#6A737D\"># meet ... dest</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        path.append(node)</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        node </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> parents_bwd[node]</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> path</span></span></code></pre>\n<p>Why is the first meeting point on a shortest path? Before this ring, the two sides had searched to depths a and b and shared nobody. That means no path of length a + b or less exists: if one did, the person a steps along it would already be in both sets. Expanding one side by a ring can only produce meetings on paths of length a + b + 1, so any meeting found is a shortest path.</p>\n<p>Bidirectional search has costs. The code is more complex. For directed graphs, such as \"follows\" on a network like Twitter, the backward side needs each person's list of followers, a second index that must be kept in step with the first (see <a href=\"/learn/design-twitter\">Design Twitter</a>). And it needs the target known up front, which is true here.</p>\n<h4 id=\"cut-the-cost-of-each-cross-server-hop\">Cut the cost of each cross-server hop</h4>\n<p>Bidirectional BFS reads about 100 friend lists. The naive service still reads them one at a time, with two round trips each: 102 × 2 × 0.5 ms ≈ 100 ms of pure network wait. Four changes shrink that.</p>\n<p><strong>1. Compute the server instead of asking for it.</strong> Give each person a shard number that can be computed from their id, such as <code>hash(person_id) mod 4096</code>, or one written into the id itself when the person is created. The lookup service then only needs a table of 4,096 shards to servers, a few kilobytes. Every user graph service keeps a copy in memory and refreshes it when shards move. Each read drops from two round trips to one. To move shards between servers without reshuffling everything, map shards to servers with <a href=\"/learn/consistent-hashing\">consistent hashing</a> or an explicit shard table. Facebook's TAO does exactly this: each object id embeds its shard id, and \"the number of shards far exceeds the number of servers.\"</p>\n<p><strong>2. Batch by server, one ring at a time.</strong> BFS can't know ring 3 until ring 2 is done, but within a ring all the reads are independent. Group the ring's ids by person server and send one batch request per server:</p>\n<pre class=\"shiki shiki-themes github-light github-dark\" style=\"background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">from</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> collections </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">import</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> defaultdict</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">def</span><span style=\"color:#6F42C1;--shiki-dark:#B392F0\"> friends_of</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">(self, ids):</span></span>\n<span class=\"line\"><span style=\"color:#032F62;--shiki-dark:#9ECBFF\">    \"\"\"Fetch friend lists for many people, one request per person server.\"\"\"</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    by_server </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> defaultdict(</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">list</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">)</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> person_id </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> ids:</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">        by_server[</span><span style=\"color:#005CC5;--shiki-dark:#79B8FF\">self</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">.lookup.lookup_person_server(person_id)].append(person_id)</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">    friend_lists </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> {}</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> server, batch </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> by_server.items():       </span><span style=\"color:#6A737D;--shiki-dark:#6A737D\"># in production, send these in parallel</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">        for</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> person </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">in</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> server.get_people(batch):</span></span>\n<span class=\"line\"><span style=\"color:#24292E;--shiki-dark:#E1E4E8\">            friend_lists[person.id] </span><span style=\"color:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> person.friend_ids</span></span>\n<span class=\"line\"><span style=\"color:#D73A49;--shiki-dark:#F97583\">    return</span><span style=\"color:#24292E;--shiki-dark:#E1E4E8\"> friend_lists</span></span></code></pre>\n<p><strong>3. Send the batches in parallel.</strong> With batching and parallel requests, a search costs one round of calls per ring. The 4-step search above expands four rings (two per side), so it waits for about 4 rounds × 0.5 ms = 2 ms of network time plus the servers' own work, instead of 100 ms.</p>\n<p>A ring that fans out to many servers waits for the slowest one. If each server is slow 1% of the time (a garbage-collection pause, a busy disk), a ring that touches 50 servers hits at least one slow server about 39% of the time. This is the tail-latency effect described in <a href=\"/learn/performance-and-latency#the-tail-at-scale\">The Tail at Scale</a>. Fixes include sending a backup request to a replica when the first is slow, and keeping per-ring timeouts.</p>\n<p><strong>4. Put friends on the same server.</strong> Friends tend to live near each other, so sharding people <strong>by location</strong> puts more of a person's friends on the same server. Then a ring needs fewer batches, and some friend-of-friend reads stay on one machine. The cost is uneven shards (a big city is a hot shard) and moving people when they relocate. The gain is also limited. The TAO paper says Facebook's graph is \"tightly interconnected\" and that \"it is not possible to group users so that cross-partition requests are rare.\" Measure whether location sharding helps before relying on it.</p>\n<h4 id=\"limit-the-search-depth\">Limit the search depth</h4>\n<p>Some pairs of people are far apart, and some aren't connected at all. Without a limit, a search for an unconnected person explores everyone reachable before giving up. Three ways to bound it:</p>\n<ul>\n<li><strong>A hop limit.</strong> LinkedIn labels people as 1st-degree (you're connected), 2nd-degree (\"connected to your 1st-degree connections\") and 3rd-degree (\"connected to your 2nd-degree connections\"). Members outside those circles (and not linked in other ways, such as a shared group) are \"out of network.\" A product that labels connections this way only ever needs paths of 3 steps or fewer. In our code it's <code>max_hops=3</code>: the two sides together search at most 3 rings, about 1 + 1 + 50 = 52 reads (each side expands its start person, then one side expands its 50 friends). The answer \"beyond 3rd degree\" is cheap and honest.</li>\n<li><strong>A time or work limit.</strong> Stop after, say, 200 ms or 10,000 friend-list reads, and tell the user: \"This could take a while. Keep searching?\" The work done so far can be kept for a few minutes so continuing doesn't start over.</li>\n<li><strong>Prefer well-connected people.</strong> People with many friends are more likely to shorten a path, so a search that expands them first often finds a path sooner. This turns BFS into a best-first search, which finds a short path quickly but no longer guarantees the shortest. It's a fair trade when the product shows \"a way you're connected\" rather than \"the shortest way.\" The safe version of this idea is already in the bidirectional code: always expand the smaller frontier.</li>\n</ul>\n<p>High-degree people cut both ways. LinkedIn caps members at 30,000 1st-degree connections, and a person with that many is a huge frontier on their own. Their friend list is 600 times longer than average, and all 30,000 people land in the next frontier. A search that reaches such a person can cap how many of their friends it follows, or expand them last.</p>\n<h4 id=\"cache-friend-lists-in-memory\">Cache friend lists in memory</h4>\n<p>Every search reads friend lists, and the same lists come up again and again. A well-connected person's list is read by many searches, and someone who searches several profiles in a row reads their own neighborhood each time. Put a <strong>memory cache</strong>, such as Memcached or Redis, between the user graph service and the person servers.</p>\n<ul>\n<li><strong>What to cache:</strong> each person's friend list, keyed by person id. This is caching at the object level (see <a href=\"/learn/cache#caching-at-the-object-level\">caching at the object level</a>). All 100 million lists come to about 40 GB, so a modest cache cluster could hold every one. In practice, the hot fraction is enough.</li>\n<li><strong>How to fill it:</strong> <a href=\"/learn/cache#cache-aside\">cache-aside</a>. The service checks the cache, falls back to the person server on a miss, and writes the result into the cache.</li>\n<li><strong>When friendships change:</strong> delete both people's cached lists after the person servers commit the change, and give entries a TTL so a missed delete doesn't last forever.</li>\n<li><strong>Why memory:</strong> in the latency table credited to Jeff Dean, reading 1 MB sequentially takes about 250 µs from memory, about 1 ms from an SSD (4×) and about 20 ms from a spinning disk (80×). These are rough figures from around 2012, and the ratios are what matter.</li>\n</ul>\n<p>The cache also helps person servers: hot lists are served from the cache, so a celebrity's shard isn't hammered by every search that passes through them.</p>\n<p>This is a large part of what Facebook built. TAO, described in a USENIX ATC 2013 paper, stores the social graph as <strong>objects</strong> (nodes, such as users and posts) and <strong>associations</strong> (typed, directed edges, such as FRIEND). Friendships are stored as two associations, one in each direction, and TAO keeps an edge and its inverse in step. Data lives in sharded MySQL, and an association is stored on the shard of its source object \"so that every association query can be served from a single server.\" In front of MySQL sit two tiers of graph-aware caches. Clients talk to <strong>follower</strong> caches, and followers forward misses and writes to a <strong>leader</strong> cache, one per shard, which talks to the database. The paper reports that TAO handles \"a billion reads and millions of writes each second,\" that writes were only 0.2% of requests, and that its read hit rate was 96.4%.</p>\n<p>TAO's API is deliberately small: fetch an object, fetch or count a node's edges of one type, page through them by time. The paper says TAO's goal \"is not to support a complete set of graph queries.\" A multi-hop search like ours would be built from many single-hop reads, which is why batching and caching single-hop reads matters so much.</p>\n<h4 id=\"store-traversals-cache-and-precompute\">Store traversals: cache and precompute</h4>\n<p>Friend-list caching speeds up each read. The next step is to avoid reads altogether.</p>\n<p><strong>Cache search results.</strong> Keep recent results keyed by (source, target) in the memory cache with a short TTL. Popular searches, such as many people viewing the same celebrity's profile, then cost one cache read. Results go stale when friendships change, but a path that was right a few minutes ago is usually fine to show.</p>\n<p><strong>Cache partial traversals.</strong> A user who searches several profiles in a row starts every search from the same place. Cache their forward rings (their friends and friends-of-friends, with parent pointers) for a few minutes, so each new search only has to run the backward side.</p>\n<p><strong>Precompute neighborhoods offline.</strong> A nightly batch job can compute, for every person, everyone within 2 steps along with the friend that leads to each one. Store the results in a <strong>NoSQL</strong> key-value or wide-column store, keyed by person id.</p>\n<ul>\n<li><strong>Size:</strong> about 50 + 2,500 = 2,550 people per person. With an 8-byte id and an 8-byte \"via\" friend each, that's about 40 KB per person, and 100 million × 40 KB ≈ <strong>4 TB</strong> before replication. That's large but ordinary for a distributed key-value store.</li>\n<li><strong>Query:</strong> any path of 4 steps or fewer is found by reading two rows, the source's and the target's, and intersecting them. Paths of 1 to 4 steps all show up this way.</li>\n<li><strong>Costs:</strong> the data is up to a day old, so a friendship made this morning won't appear. You can fix that by running a live search when the precomputed answer is missing or by updating rows incrementally as friendships change. Well-connected people blow up the row size: someone with 5,000 friends who each have 50 has 250,000 people within 2 steps. Cap the row or skip precomputing for such people. The job itself is a large distributed computation; see <a href=\"/learn/batch-processing\">batch processing</a>.</li>\n</ul>\n<h4 id=\"keep-the-service-available\">Keep the service available</h4>\n<p>The second use case is high availability. Each component needs a plan for failure:</p>\n<ul>\n<li><strong>Person servers</strong> are the source of truth, so each shard has <strong>replicas</strong> on other machines, ideally in other racks or zones. Reads go to any replica. If the primary fails, a replica is promoted. See <a href=\"/learn/availability#fail-over-and-replication\">fail-over and replication</a>.</li>\n<li><strong>The lookup table</strong> is small once shards are computed from ids, so every user graph service keeps a copy. The service that publishes changes to it can be down briefly without stopping searches, since the cached copy keeps working until shards move.</li>\n<li><strong>The memory cache</strong> is an accelerator. If it fails, searches get slower and person servers get busier, so size the person servers to survive a cold cache, and watch for a <a href=\"/learn/cache#cache-stampede-thundering-herd\">cache stampede</a> when it comes back.</li>\n<li><strong>Graceful degradation.</strong> If a person server is unreachable mid-search, the search can skip that shard's people and still return a path if one exists through other people. The path may not be the shortest, and the response can say so.</li>\n</ul>","headings":[{"id":"step-4-scale-the-design","title":"Step 4: Scale the design","depth":3},{"id":"find-the-bottlenecks","title":"Find the bottlenecks","depth":4},{"id":"load-balancer-web-servers-and-the-search-api","title":"Load balancer, web servers and the search API","depth":4},{"id":"search-from-both-ends-bidirectional-bfs","title":"Search from both ends: bidirectional BFS","depth":4},{"id":"cut-the-cost-of-each-cross-server-hop","title":"Cut the cost of each cross-server hop","depth":4},{"id":"limit-the-search-depth","title":"Limit the search depth","depth":4},{"id":"cache-friend-lists-in-memory","title":"Cache friend lists in memory","depth":4},{"id":"store-traversals-cache-and-precompute","title":"Store traversals: cache and precompute","depth":4},{"id":"keep-the-service-available","title":"Keep the service available","depth":4}]},{"id":"trade-offs-and-follow-up-questions","title":"Trade-offs and follow-up questions","html":"<h3 id=\"trade-offs-and-follow-up-questions\">Trade-offs and follow-up questions</h3>\n<p><strong>Exact shortest path or a good-enough path?</strong> An exact BFS with no limits can explode on unconnected or distant pairs. Bounding depth (LinkedIn's 3rd degree), bounding time, or preferring hubs makes cost predictable at the price of sometimes answering \"too far\" or showing a path that isn't the shortest. Pick the hop limit by default: users mostly care about close connections, and a clear \"3rd degree or beyond\" is better than a slow page.</p>\n<p><strong>Hash sharding or location sharding?</strong> Hashing gives even shards and simple routing, and nearly every friend is on another server. Location sharding keeps more friends together but gives uneven shards, needs moves when people relocate, and still leaves many cross-server reads in a tightly connected graph. Start with hashing into many small logical shards, which keeps the option to place shards cleverly later.</p>\n<p><strong>Directory or computed lookup?</strong> A per-person directory lets you move any single person anywhere, at the cost of a large, replicated table and an extra round trip per read. Computing the shard from the id (or embedding it in the id, as TAO does) removes the round trip, and a small shard-to-server table keeps flexibility. Use <a href=\"/learn/consistent-hashing\">consistent hashing</a> or many logical shards so adding servers moves little data.</p>\n<p><strong>Search live, or precompute?</strong> Live bidirectional search is always current and costs about 100 cached reads per search. Precomputed 2-step neighborhoods answer with two reads but are hours old and take terabytes. With caching, live search is fast enough here, so precompute only if latency targets demand it, or for the most-viewed profiles.</p>\n<p><strong>How stale can a friend list be?</strong> A cached list may still include someone who unfriended a person a few seconds ago, so the path might go through a friendship that no longer exists. TAO accepts this kind of staleness: its paper describes eventual consistency between cache tiers, with read-after-write consistency within one tier. A cheap safeguard is to re-check the 3 to 5 edges of the final path against the person servers before returning it. See <a href=\"/learn/cap-and-consistency#guarantees-for-one-user\">guarantees for one user</a>.</p>\n<p><strong>Why not a graph database?</strong> If the constraint is lifted, a graph database stores relationships so that following an edge is cheap, and its query language expresses multi-hop questions directly. See <a href=\"/learn/nosql-databases#graph-databases\">graph databases</a>. At hundreds of millions of people it still has to be sharded, and every hop between shards costs a network call, so the ideas in this chapter (bidirectional search, depth limits, caching) still apply. A related confusion: GraphQL, despite its name, is an API query language that lets a client choose the fields it gets back. It doesn't store graphs. See <a href=\"/learn/protocols-and-apis#graphql-the-client-picks-the-fields\">GraphQL</a>.</p>\n<p><strong>REST outside, RPC inside.</strong> The public endpoint is one simple REST call that browsers and apps handle easily. Inside, a single search makes dozens of batched calls, where RPC's binary encoding, persistent connections and generated clients pay off. See <a href=\"/learn/protocols-and-apis#rpc-and-rest-side-by-side\">RPC and REST side by side</a>.</p>","headings":[{"id":"trade-offs-and-follow-up-questions","title":"Trade-offs and follow-up questions","depth":3}]},{"id":"check-yourself","title":"Check yourself","html":"<h3 id=\"check-yourself\">Check yourself</h3>\n<details>\n<summary>Suppose the average user had 100 friends instead of 50, and the target is 4 steps away. How many friend-list reads do one-sided and bidirectional BFS need, ignoring overlap?</summary>\n<p>One-sided BFS expands depths 0 to 3 before discovering depth 4: 1 + 100 + 10,000 + 1,000,000 = 1,010,101 reads. Bidirectional BFS expands depths 0 and 1 on each side: 2 × (1 + 100) = 202 reads. The ratio is about 5,000, up from about 1,250 at 50 friends.</p>\n<p>The ratio grows with fan-out because the one-sided cost is about b^d and the two-sided cost about 2 × b^(d/2). Doubling b multiplies the first by 16 and the second by only about 4.</p>\n</details>\n<details>\n<summary>Your lookup service is a table with one row per person, and every friend-list read first calls it. A bidirectional search reads 102 lists. What does the lookup cost, and how would you remove it?</summary>\n<p>Read one at a time, that's 102 extra round trips, about 51 ms at 0.5 ms each, doubling the network time of the search. It also makes the lookup service as busy as all the person servers combined.</p>\n<p>Compute the shard from the person id instead, for example <code>hash(id) mod 4096</code>, or embed the shard number in the id when the person is created. The only table left maps 4,096 shards to servers. It's a few kilobytes, so every user graph service keeps it in memory and refreshes it when a shard moves. The lookup becomes a local calculation with no network call.</p>\n</details>\n<details>\n<summary>A user searches for a celebrity with 30,000 connections. What happens in the bidirectional search, and what keeps it cheap?</summary>\n<p>The searcher's side starts with 1 person and the celebrity's side with 1. After one ring each, the searcher's frontier has about 50 people and the celebrity's has 30,000. Because the code always expands the smaller frontier, it keeps growing the searcher's side, reading about 50 lists for the next ring instead of 30,000. The 2,500 friends-of-friends it discovers are checked against the celebrity's 30,000 connections in memory, which is cheap.</p>\n<p>What keeps it cheap is the smaller-frontier rule plus caching. The celebrity's friend list is read by many searches, so it's almost always in the memory cache. Popular targets also benefit from caching whole results keyed by (source, target).</p>\n</details>\n<details>\n<summary>The product changes from mutual friendships to one-way follows. What has to change in the search?</summary>\n<p>One-sided BFS from the searcher still works: it follows \"who does this person follow\" lists. Bidirectional BFS needs the backward side to walk edges in reverse, so it needs \"who follows this person\" lists, a second index. Each follow is then written twice, once in the follower's list and once in the followee's, and those two copies can be on different shards. TAO handles this with inverse associations: when an edge type has an inverse, adding or deleting the edge also adds or deletes the inverse. It doesn't make the two writes atomic, and a background job repairs edges left without their inverse.</p>\n</details>\n<details>\n<summary>Estimate the storage for precomputing everyone's 2-step neighborhood. Would you do it?</summary>\n<p>With 50 friends each and no overlap, a person has 50 + 2,500 = 2,550 people within 2 steps. Storing an 8-byte id and an 8-byte \"via\" friend for each is about 40 KB per person, and 100 million people gives about 4 TB, more with replication. Real lists overlap, which lowers this, and high-degree people raise it sharply.</p>\n<p>It answers any path of 4 steps or fewer with two reads. But the data is up to a day old and costs a large nightly job. Live bidirectional search with cached friend lists costs about 100 cache reads and finishes in a few milliseconds of network time, so I'd only precompute for the most-viewed targets or if latency targets demanded it.</p>\n</details>\n<details>\n<summary>After adding more person servers, the median search time stays the same but the 99th percentile gets worse. Why?</summary>\n<p>With more servers, each ring of a search fans out to more of them, and the ring finishes only when the slowest reply arrives. If each server is slow 1% of the time and a ring touches 50 servers, the chance that at least one is slow is 1 − 0.99⁵⁰ ≈ 39%. Adding servers raises the fan-out and with it the odds of hitting a slow one, which shows up in the tail.</p>\n<p>Fixes: send a backup request to a replica when a reply is late (hedged requests), set per-ring timeouts and continue without the slow shard, cache more of the hot friend lists, and use more, smaller logical shards placed so a ring touches fewer machines.</p>\n</details>\n<h3 id=\"further-reading\">Further reading</h3>\n<ul>\n<li><a href=\"https://www.usenix.org/conference/atc13/technical-sessions/presentation/bronson\" target=\"_blank\" rel=\"noopener noreferrer\">TAO: Facebook's Distributed Data Store for the Social Graph</a> (USENIX ATC 2013): how Facebook stores its graph as objects and associations on sharded MySQL behind two tiers of graph-aware caches, with production numbers on reads, writes, hit rates and high-degree nodes. The <a href=\"https://www.usenix.org/system/files/conference/atc13/atc13-bronson.pdf\" target=\"_blank\" rel=\"noopener noreferrer\">PDF</a> is short and readable.</li>\n<li><a href=\"https://research.facebook.com/blog/2016/02/three-and-a-half-degrees-of-separation/\" target=\"_blank\" rel=\"noopener noreferrer\">Three and a half degrees of separation</a> (Facebook Research, 2016): the study that measured an average distance of 4.57 friendships across 1.59 billion people, and how they estimated it without running a search for every pair.</li>\n<li><a href=\"https://www.linkedin.com/help/linkedin/answer/a545636/your-network-and-degrees-of-connection\" target=\"_blank\" rel=\"noopener noreferrer\">Your network and degrees of connection</a> (LinkedIn Help): LinkedIn's definitions of 1st, 2nd and 3rd-degree connections and the 30,000-connection cap, a real example of a product that bounds search depth.</li>\n<li><a href=\"https://en.wikipedia.org/wiki/Bidirectional_search\" target=\"_blank\" rel=\"noopener noreferrer\">Bidirectional search</a> (Wikipedia): the general algorithm and its b^(d/2) cost, with pointers to the variants for weighted graphs.</li>\n<li><a href=\"https://gist.github.com/jboner/2841832\" target=\"_blank\" rel=\"noopener noreferrer\">Latency numbers every programmer should know</a>: the table of rough latencies, credited to Jeff Dean and Peter Norvig, used here for round trips and memory versus disk reads.</li>\n</ul>","headings":[{"id":"check-yourself","title":"Check yourself","depth":3},{"id":"further-reading","title":"Further reading","depth":3}]}]}