BOOK DEEP READ · System Design Interview Vol 2 · Ch 2

Design Nearby Friends

System Design Interview Vol 2 · Ch 2 · Alex Xu & Sahn Lam · 2022

中文 →

What is this chapter about?

Apple has Find My. Facebook once had a feature called Nearby Friends: open a map and see which of your friends happens to be close by, with a little note saying "updated 3 minutes ago". This chapter builds that from nothing. It looks like the same kind of problem as the previous chapter — finding nearby restaurants — but the answer turns out to be almost the opposite.

A strange thing first

To find restaurants, the last chapter numbered every shop in the world and filed them into one enormous ledger sorted by location; you show up, you look it up. This chapter is looking for people, and people move every half minute. That ledger would need three million edits a second — you could never keep up. Stranger still: the right answer here is not a faster ledger. It is no ledger at all.

Why the old way breaks

Do it the old way and your phone has to ask the server, every half minute, "where are my few hundred friends right now?" — and the server has to go leaf through the ledger for each one. With a hundred million people asking at once, the servers struggle, and your phone battery does not survive until lunch. Uber worked this way early on, and has said publicly that the first thing to collapse was not the servers but the battery.

The idea: stop looking, start subscribing

Think of each person as a little broadcast channel. You and your few hundred friends each open one; you subscribe to theirs, they subscribe to yours. Whoever moves shouts into their own channel — "I'm over here now" — and only the people who subscribed can hear it. Nobody looks anything up. The locations come to you.

So who measures the distance? Whoever hears the shout. Your friend calls out, the server holding your connection hears it, holds up a ruler and checks: is he within five miles of me? If yes, show him; if not, pretend you never heard. The system never has to answer the big question "who in this city is near whom" — only a long series of tiny ones: "is he near me?"

Locations are written on notes that fade. Each person's current position is one sticky note that disappears by itself after forty seconds. The elegance is in what that buys: someone closes the app, steps into a lift, runs out of battery — their note simply evaporates, and they fade off their friends' maps on their own. Nobody has to sweep up.

What it costs, and when to use it

The whole thing works because of one precondition: you already know which few hundred people you care about. So it only handles friends. If you want to find strangers around you — a set nobody knows in advance — you have to go back to the previous chapter's big ledger. Real products run both.

And the cost is real: every phone must hold a wire open to the server at all times. The connection itself consumes resources, and even shipping a new server version means waiting for the old wires to empty out.

One line to remember

To find where your friends are, don't search — give everyone a broadcast channel, let movers shout, and let listeners measure the distance themselves. Write each position on a note that vanishes in forty seconds, so anyone who drops offline fades off the map on their own. Finding strangers is what the previous chapter's ledger is for.

Want the mechanisms, the numbers and the diagrams? → Switch to the deep read