Ch.48: System Design: Tinder's Missing Match
Outline
- 0:00 Two successful swipes, no match
- 0:39 The complete architecture
- 1:06 Define the product promise
- 2:05 Workload, durability and failure
- 2:56 Why saving first still fails
- 3:43 One key for both directions
- 4:44 Commit pair, receipt and event
- 5:42 Replay simultaneous swipes
- 6:36 Lost replies and retry windows
- 7:31 Bound nearby discovery
- 8:31 Match lists are read views
- 9:29 Recover durable pending work
- 10:29 Keep unmatching permanent
- 11:19 Accept chat within a transaction
- 12:16 Hot keys and actual capacity
- 13:21 Choose the regional failure policy
- 14:25 Test the commit boundaries
- 15:23 Return to the architecture
- 15:58 Build the interview answer
Transcript
0:00 Two people swipe right on each other on Tinder. Both phones get success. Neither person gets a match. Our servers saved both likes, yet matching failed. Where did the relationship go? Each server could have looked for the other person's like before it arrived. Both saw nothing, then both saved. Two successful requests, one missing relationship. We'll design a Tinder-like system that makes those likes meet, survives a lost reply, and keeps an old request from reopening a match someone ended. That last part changes the question.
0:31 Saving a like is easy. Where do we put the shared decision about what these two people can do together? Now put Maya's and Leo's phones on this map. It has three paths. Discovery finds nearby profile cards. The middle commits what each pair decided. The right side turns committed matches into lists, notifications and permission to chat. Photos can come from the content delivery network while that middle is busy. But a cached card cannot decide that a match exists. Watch the middle pair record. We'll return to it when both likes share a commit point.
1:06 Before choosing those stores, define our functional requirements. After the second like, the successful reply includes the mutual match decision. Users also need profiles, preferences and nearby candidates, likes or passes, permanent unmatch and blocking that removes the pair from discovery. And chatting requires an active match. Tinder's help pages establish that requirement and say unmatching is permanent. Our implementation is an exercise; those product behaviors give it something real to preserve.
1:34 Source links are in the description. For example, Maya likes Leo, then Leo likes Maya. They should see the same match. If Maya ends it, a delayed Leo request must not recreate it. Then preserving Maya's decision is part of the core job. Premium rewind, payments and training a recommendation model stay outside it. We'll include discovery and communication without rebuilding every feature in a dating app. Good. Each tap must affect a shared relationship. What should success mean when traffic spikes?
2:05 Now the non-functional requirements. Suppose 10 million daily users make 100 swipes each: a billion requests a day, about 11,600 a second. A fivefold peak approaches 58,000. Those are our assumed numbers. That's before transaction writes, retries and indexes. I would be suspicious of a capacity estimate that buys exactly one database operation per swipe. So would I. Target 300-millisecond candidate pages and 500-millisecond regional swipe commands under normal load. A success reply means the decision committed durably.
2:38 Ranking may lag; match creation cannot silently miss reciprocal likes. And if the authority is unavailable, we can refuse a swipe. Calling it successful now and hoping to reconcile later would change that promise. That reliability tradeoff is deliberate. Why can two healthy servers still miss the match? Now imagine Maya's server checks Leo's likes: nothing. Leo's server checks Maya's likes: nothing. Then each saves. That's the opening failure. My first thought is to save before checking. But suppose each write goes to its user's partition and each check reads an old replica.
3:13 Both writes can finish while both checks still miss. Hold on. Changing the order isn't enough if the two decisions have separate authorities. A repair job could eventually notice, but then success doesn't include the immediate match result we chose. We could choose that weaker product. Here we'll make the mutual decision while committing the swipe. We need the storage key to bring those two decisions together. So partitioning by the person who swiped is convenient for history, but awkward for deciding a pair.
3:43 Next, those separate authorities suggest a different storage key. Take Maya's immutable user ID and Leo's. Sort and encode them unambiguously into one pair key. Maya-to-Leo and Leo-to-Maya route to that same key. The likes still have directions. Maya's yes isn't Leo's yes. The shared record holds both decisions, a revision number that advances on each committed change, and whether the pair is open, matched or permanently closed. An open record becomes active when both decisions are yes. Think of two signatures needed on one shared form.
4:16 That transition creates one stable match ID. A closed record cannot become active again. And the second signature completes the same form, whichever person signs first. I think one Leo row looks easier to query, but we'd make every admirer hit it. Pair keys separate that traffic. And it has a cost: listing all of Maya's matches now needs a separate read view. We'll build that after making their joint write correct. Let's make that joint decision concrete. What must commit before we tell Leo he has a match?
4:49 For this design, use DynamoDB in one authoritative Region. The service reads the pair, computes the next state, then submits a conditional transaction. It writes the revised pair, a receipt for this command, and, when a match is created, a pending event. The condition says the pair's revision must equal the value we read. If another request changed it, this whole transaction fails. Reread and recompute. Our matching commit saves three separate items: the pair, this tap's receipt and a dispatch obligation.
5:19 The revision check goes on the pair update. AWS documents that transaction boundary. Oh, so a crash leaves unfinished delivery work, rather than a match nobody knows about. The receipt stores this tap's answer; the event stores what we still owe downstream. Yeah, that's the useful split. Sending an external push comes later. The transaction records the obligation to send, not the delivery. Let's say both services read revision zero, with neither like present. What makes one of those requests lose?
5:49 Maya wins the conditional write. Revision one records her yes. Leo's attempt still expects zero, so it fails. His service rereads revision one and now sees Maya's decision. Oh, that stale attempt has to reconsider the relationship. It adds his yes and commits revision two with the active match, command receipt and match event. The missing relationship finally has a commit point. If Leo wins first, Maya does that second transition instead. The names don't determine the result; successful transaction order does.
6:21 Hmm, we're paying a retry for the simultaneous case. But each pair usually has two decision makers, not a million. Bound retries and return a retryable error if contention doesn't settle. Right. Never answer success merely because an attempted write was queued. Once that match commits, consider a reply failure: Leo's connection dies before he sees success. His phone retries the same command ID. The durable receipt gives the original result. We shouldn't run a second match transition or send another logical match event.
6:52 Bind that ID to the actor, pair and payload. A different payload with the same ID is an error. In our exercise, server-issued command tokens allow retries for 24 hours, and receipts stay for 48. That's application policy. DynamoDB's transaction request token only has a 10-minute idempotency window; it doesn't cover a phone retrying tomorrow. Right. After our retry window expires, reject that old command. The pair's recorded decisions and permanent closure still matter after its receipt expires. So the receipt records the earlier result.
7:24 Current state controls permission. A dinner receipt can prove yesterday's payment, but it can't tell me whether the restaurant is open now. Now that pair has a precise result. How do we avoid scanning 10 million profiles for Maya's next card? Profile updates feed a geographic and preference index. For this exercise, rank 100 nearby candidates, then fetch 20 cards' profiles and check pair eligibility. Elasticsearch's geographic filters are one possible implementation. Prefetch those 20 cards from object storage through nearby delivery servers.
7:53 Now suppose Leo is already in Maya's stack when she blocks him. Her stack can hold that old card. Before sending another result, filter blocked pairs through current relationship state. Location privacy adds another constraint: keep exact coordinates off other users' screens. So Leo's card may linger until Maya refreshes. We can't promise to recall pixels that were already sent. But if Maya taps it anyway, her command reaches the closed pair and is refused. That old suggestion can't authorize a new like.
8:24 Then we can serve approximate suggestions quickly while reserving strict ordering for the action a user actually takes. But discovery opens another read problem. Our pair key scatters Maya's matches across the store. Maintain a per-user match list from committed events so the app can fetch candidate match IDs efficiently. Candidate IDs again. If an old event adds Leo after Maya unmatched, what stops that list from reopening their match? Take a match-list page where Maya opens 20 IDs, including Leo from a delayed list update.
8:57 Fetch their current records and hide his closed pair. Her successful swipe reply can also show a new match before that list catches up. That explains a subtle difference: a lagging list can temporarily omit a real match, but it must not grant permission to a closed one. And a huge list needs bounded pages. Fetching several partitions for one page costs extra requests; measure those before choosing how to split the list. Pair ownership made the write simple. We pay for a separate browsing layout.
9:28 Next, imagine a worker crashing after the pair became active but before notifying either person. What's still on disk? The match event. A stream triggers quick dispatch. We also keep a list of unfinished events, split into buckets so repair workers can find what is still owed. Retry delivery to our downstream systems or intentionally suppress it. Suppose that worker stays down for 2 days. DynamoDB Streams keeps records for up to 1 day, so its alert expires. But the saved work order still owes Maya and Leo a list update.
10:01 Keep the pending event itself until resolved. Events have stable IDs, and workers updating match lists tolerate repeated delivery. Each event carries its own pair and revision; we don't assume all transaction items arrive as one ordered stream batch. Before sending a match notification, recheck current pair state. That catches a closure that happened while the worker was down. It reduces stale notifications. It cannot retract a push already accepted by the phone's delivery provider. Now Maya ends the relationship.
10:31 Her command uses the same pair authority and revision condition, committing closed state with an event to remove it from the lists. Leo has a delayed like still in flight. If it expects an earlier revision, its write fails. On reread, closed means no new match. Yes. Closure is permanent for these identities. Never expire that closed state while an old identity can still issue valid commands. Account deletion and data-retention policy need their own lifecycle design. What if Leo's like committed just before Maya closed it?
11:01 Then a match briefly existed and was ended. His original receipt can truthfully report that earlier result; opening it fetches current closed state. That's less magical than promising nobody ever sees a late notification. It gives us an enforceable rule: old work cannot recreate the relationship. But closure also affects chat. I'd let that service check active, then enqueue the message. Simple enough? There's a gap. It reads active; Maya unmatches; then it enqueues. The check and acceptance must share a transaction boundary.
11:32 So the communication service submits one transaction checking the authoritative pair is active while recording the message receipt and delivery obligation. Here we check the pair without updating it; the swipe transaction changed it. Closure modifies that pair. Transaction ordering either accepts the message before closure or rejects it afterward. A cached active flag or an old signed token cannot make that decision. Earlier accepted messages could still be queued, and delivered bytes can't be recalled.
12:00 Reads of the conversation consult the current relationship; we don't promise impossible remote erasure. Right. Ordinary message storage and socket delivery can be separate. Their authorization starts with this pair, not with a notification saying it's a match. So that permission rule sends commands to the pair authority. Now stress its layout: imagine a popular profile getting likes from a million people. An incoming-likes row keyed only by that profile would concentrate writes. Our canonical pair keys spread those admirers across independent records.
12:34 The popular user's own matches and recommendation counters can still become hot, so the read views may need buckets. And the request estimate was only a starting point. A swipe adds a strong read, several transactional item operations, projection writes, and sometimes a conflict retry. Measure bytes and consumed capacity under the peak, including retries. I would rate-limit repeated commands per actor and pair before buying more database capacity for a broken client. Otherwise we're scaling the retry button.
13:04 That's a lot of server work for no new admirer. Count requests per unique command so we know which load we're scaling. Discovery, photos and pair mutations still need separate budgets. And keep discovery bounded too. Spreading writes won't rescue an unlimited card query. But our popular profile may attract people on different continents. Why not let both nearby Regions accept their swipes? That needs a stronger cross-region protocol. Our pair store accepts commands in a single home Region. DynamoDB replicates data across three Availability Zones, physically isolated groups of data centers inside that Region.
13:41 A zone failure differs from losing the regional authority. Discovery and photos may be closer; commands still travel home. So an outage can leave browsing available while likes and chat acceptance are unavailable. We don't switch mutations to a stale asynchronous copy. Correct. Global availability would change the design and latency cost. I'd state that limitation in the interview rather than quietly remove the pair's ordering guarantee. Ordering protects the decision only if we know who's making it.
14:09 The edge derives Maya's identity from her session, not a user ID supplied in her request body. Users can name a target, not impersonate the other half of a pair. Right. Our state machine needs both the correct ordering and authenticated writers. Finally, those failure choices need tests at the actual commit boundaries. First, take simultaneous swipes reading revision zero. Require one durable active match after the retry, whichever person commits first. Then kill the process after commit but before reply.
14:39 The same command must resolve from its receipt. Kill the event worker past the stream horizon; the persisted pending event must still be recoverable. Next, replay a like and an index-add after closure. The pair stays closed and the app hides that projected row. Finally, race a message acceptance transaction against unmatch. A message ordered after closure is rejected. A message accepted before it can exist, even if its delivery is late. We should test that distinction, not call every late arrival a failed transaction.
15:10 Track pending-event age, conditional conflicts and commands accepted against closed state. That last count must remain zero. A low average response time can't prove it. Those tests finally tell us whether the architecture keeps its promise. Now show where those tests fit on the original map. How do the two phones finally reach one answer? The diagram's discovery and media branch put a card on each phone. Both swipes meet at the same pair record, where a conditional commit creates one durable match.
15:38 Leo's lost swipe reply comes back from his receipt. Persisted events build lists and notifications. Chat acceptance checks that same pair in its transaction, so closure governs what happens afterward. The separate read paths buy speed. The shared pair decision makes the two people agree on one relationship state. Now put our requirements into the interview answer. Where would you start if asked to design Tinder? So if I'm answering this interview, I start with the product promise: do we owe an immediate mutual match, or can matching happen later?
16:11 That answer changes where the two likes must meet. Exactly. Choose the joint decision's key and commit boundary, then explain how discovery, lists and communication follow it. That's how two successful swipes become one match instead of two unrelated database rows. Thanks for listening to Learning Podcasts.