Design Swipe Matching

How to design a swipe-based dating app: storing swipes at high volume, detecting a mutual match exactly once, and building candidate stacks.

Case study26 minLesson 2 of 8
On this page
  1. Clarify the problem
  2. Estimate the scale
  3. Define the API (optional)
  4. Model the data
  5. High-level design
  6. Deep dives
  7. Bottlenecks and failures
  8. What a strong answer sounds like
  9. Follow-up questions

A swipe-based dating app shows one profile at a time. The user swipes right to say they are interested or left to pass, and when two people have swiped right on each other, both are told that they have matched. Interviewers ask for it because every swipe is a write, two people can swipe on each other at the same moment, and no profile may be shown twice.

The steps below follow A Framework for Any Design.

Clarify the problem

The first five minutes
Interviewer

Design the core of a swipe-based dating app.

You

I understand it as profiles with preferences, a stack of nearby people who fit them, a swipe on each one, and a match when both swipe right. Is chat in scope?

Interviewer

No, you can stop at the match. Assume 20 million daily users who each make about 100 swipes a day.

You

Does the second person to swipe have to see the match immediately?

Interviewer

Yes, and the first person gets a push notification.

You

Then a match must never be missed or announced twice, it must appear within 200 ms, and no profile may be shown twice. A stack that is a few minutes old is acceptable.

Functional requirements

  • A user has a profile with photos and preferences: an age range, the genders they want to see, and a maximum distance.
  • The app serves a stack of candidates who fit my preferences and whose preferences I fit.
  • Two right swipes create a match, and a user can list their matches.

Non-functional requirements

  • A swipe, including the match answer, completes in under 200 ms.
  • Each pair of mutual right swipes produces exactly one match, and no profile is shown to the same user twice.
  • Right swipes must be durable. A left swipe may occasionally be lost, because the worst outcome is that the user sees that profile a second time.

Out of scope

Chat (see Design a Chat App), payments, photo moderation and ranking are out of scope.

Estimate the scale

Swipe rate

20 million users making 100 swipes each is 2 billion swipes a day. Dividing by about 86,400 seconds gives roughly 23,000 swipes a second on average, and an evening peak of about 3 times that is 70,000 a second. Every swipe is a write. If 30% of swipes are right swipes, the peak is about 21,000 right swipes a second, and only those can create a match.

Storage and memory

  • Swipe history at about 50 bytes per swipe is 2 × 109 × 50 B = 100 GB a day, or about 36.5 TB a year.
  • Profiles for 100 million accounts at 2 KB each take 200 GB.
  • A cached stack holds 200 candidate ids of 8 bytes each, which is 1.6 KB per user, so 20 million daily users need 32 GB.
Estimate: Memory for "already seen" filters

Each daily user has swiped on 10,000 profiles. A Bloom filter with a 1% false-positive rate needs about 9.6 bits per item. How much memory do all the daily users' filters take together?

You know
  • 20 million daily users
  • 10,000 swiped profiles per user
  • About 9.6 bits per item for a 1% false-positive rate

Show the working
  1. One filter: 10,000 × 9.6 bits = 96,000 bits = 12 KB
  2. All users: 20 × 106 × 12 KB = 240 GB
  3. An exact set of 8-byte ids would be 10,000 × 8 B = 80 KB per user, so 1.6 TB in total

Estimate 240 GB (anything from 80 GB to 800 GB is a good answer)

The filter is about 7 times smaller than an exact set, at the cost of wrongly treating 1 in 100 unseen profiles as seen.

Your turn: Define the API

Write the endpoints for loading a stack and for swiping. How does the second swiper learn about the match immediately, and what happens if the phone retries a swipe?

Compare with a model answer

A GET /stack call returns the next profiles, and POST /swipes returns whether the swipe produced a match in the same response. Swipes are identified by the pair (swiper, target), so a retry returns the same result and changes nothing (see idempotency and retries).

Define the API Optional

Optional in the interview. Sketch the API only if the interviewer asks, if an API choice shapes the design, or if time permits. Otherwise say "I'll skip the detailed API unless you want it" and go to the data model.

HTTP API
PUT  /me/profile          name, photos, bio, age range, genders wanted, max distance
PUT  /me/location         { "lat": 51.51, "lng": -0.12 }   sent when the app opens
 
GET  /stack?count=20
  200 { "profiles": [ { "user_id": "u_91", "name": "Ben", "age": 31,
                        "distance_km": 4, "photos": [...] }, ... ] }
 
POST /swipes
  body: { "target_id": "u_91", "direction": "right" }
  200 { "matched": true, "match_id": "u_12:u_91" }
  200 { "matched": false }
  404 Not Found     target deleted their account
 
GET  /matches?cursor=...

The API decision worth arguing is what the stack response leaves out. It could flag candidates who have already liked you, but then anyone inspecting the app's traffic could list everyone who liked them. The server keeps that to itself and checks it at swipe time.

Your turn: Model the data

Sketch the records for profiles, swipe history and likes. Which key lets you answer "do these two people like each other?" with one atomic step?

Compare with a model answer

Swipe history is keyed by swiper, which answers "who has this user already seen?". Likes are keyed by the pair of users, with the smaller id first and one flag for each direction. Both likes then live in one row, which one atomic update can check.

Model the data

Records
profiles                       -- relational, 200 GB
  user_id PK, name, birth_date, gender, bio, photo_urls,
  age_min, age_max, genders_wanted, max_distance_km,
  lat, lng, last_active_at
 
swipes                         -- wide-column, partition key swiper_id
  swiper_id, target_id, direction, created_at
  PRIMARY KEY ((swiper_id), target_id)    -- 2 billion rows a day
 
pair_likes                     -- sharded relational, key = the pair
  low_id, high_id              -- the two user ids, smaller first
  low_likes_high  BOOLEAN
  high_likes_low  BOOLEAN
  matched_at      TIMESTAMP NULL
  PRIMARY KEY (low_id, high_id)

The swipe history is append-only and read by swiper, which suits a wide-column store such as Cassandra. The pair row needs an atomic read-modify-write on a single key, so it lives in sharded relational tables or in a key-value store with conditional updates. It grows by up to 600 million rows a day (30% of 2 billion swipes), about 9 TB a year at 40 bytes a row, so it is sharded by a hash of the pair. Profiles are relational, and their change stream updates the cached profile cards and a search index with a geo field.

Your turn: High-level design

Sketch how the system serves a stack, how it refills the stack, and how it records a swipe that may produce a match.

Compare with a model answer

A stack service reads precomputed stacks from a cache. A stack builder queries the geo index and drops profiles the user has already seen. A swipe service appends to the swipe history and updates the pair row.

High-level design

Stacks are precomputed into a cache, and one atomic update on the pair row decides a match and triggers the push.Phone app to API gateway (HTTPS); API gateway to Stack service (GET /stack); API gateway to Swipe service (POST /swipes); Stack service to Stacks, filters, profiles (next 20); Swipe service to Stacks, filters, profiles; Swipe service to Pair likes DB (atomic update); Swipe service to Swipe history; Pair likes DB to Match notifier, asynchronous; Match notifier to Phone app (push), asynchronous; Stack builder to Profile geo index (nearby + prefs); Stack builder to Swipe history; Stack builder to Stacks, filters, profiles.Phone appAPI gatewayStack serviceSwipe serviceMatch notifierStack builderStacks, filters,profilesPair likes DBSwipe historyProfile geoindexHTTPSGET /stackPOST /swipesnext 20atomic updatepushnearby + prefs

request and replyasynchronous message

Stacks are precomputed into a cache, and one atomic update on the pair row decides a match and triggers the push.
Serving a stack, a swipe, a simultaneous match and a cache node failureUse the arrows or ← → keys
  1. Serving a stack, a swipe, a simultaneous match and a cache node failure, step 1Phone app to API gateway; API gateway to Stack service; API gateway to Swipe service; Stack service to Stacks, filters, profiles; Swipe service to Stacks, filters, profiles; Swipe service to Pair likes DB; Swipe service to Swipe history; Pair likes DB to Match notifier, asynchronous; Match notifier to Phone app, asynchronous; Stack builder to Profile geo index; Stack builder to Swipe history; Stack builder to Stacks, filters, profiles.Phone appAPI gatewayStack serviceSwipe serviceMatch notifierStack builderStacks, filters,profilesPair likes DBSwipe historyProfile geoindex
    stack left
    45

    1Ana opens the app, and the stack service takes the next 20 ids and their profile cards from the cache, with no database query. When fewer than 50 remain, it queues a refill that Ana does not wait for.

  2. Serving a stack, a swipe, a simultaneous match and a cache node failure, step 2Phone app to API gateway; API gateway to Stack service; API gateway to Swipe service; Stack service to Stacks, filters, profiles; Swipe service to Stacks, filters, profiles; Swipe service to Pair likes DB; Swipe service to Swipe history; Pair likes DB to Match notifier, asynchronous; Match notifier to Phone app, asynchronous; Stack builder to Profile geo index; Stack builder to Swipe history; Stack builder to Stacks, filters, profiles.Phone appAPI gatewayStack serviceSwipe serviceMatch notifierStack builderStacks, filters,profilesPair likes DBSwipe historyProfile geoindex1,000 found
    candidates
    1,000

    2The builder asks the geo index for active people within 15 km who fit her preferences and whose preferences fit her. 1,000 come back.

  3. Serving a stack, a swipe, a simultaneous match and a cache node failure, step 3Phone app to API gateway; API gateway to Stack service; API gateway to Swipe service; Stack service to Stacks, filters, profiles; Swipe service to Stacks, filters, profiles; Swipe service to Pair likes DB; Swipe service to Swipe history; Pair likes DB to Match notifier, asynchronous; Match notifier to Phone app, asynchronous; Stack builder to Profile geo index; Stack builder to Swipe history; Stack builder to Stacks, filters, profiles.Phone appAPI gatewayStack serviceSwipe serviceMatch notifierStack builderStacks, filters,profiles+200Pair likes DBSwipe historyProfile geoindex
    candidates
    1,000
    already seen
    360
    stack left
    245

    3The builder drops the 360 in her seen filter, ranks the other 640 and appends the best 200 to her stack.

  4. Serving a stack, a swipe, a simultaneous match and a cache node failure, step 4Phone app to API gateway; API gateway to Stack service; API gateway to Swipe service; Stack service to Stacks, filters, profiles; Swipe service to Stacks, filters, profiles; Swipe service to Pair likes DB; Swipe service to Swipe history; Pair likes DB to Match notifier, asynchronous; Match notifier to Phone app, asynchronous; Stack builder to Profile geo index; Stack builder to Swipe history; Stack builder to Stacks, filters, profiles.Phone appAPI gatewayStack serviceSwipe serviceMatch notifierStack builderStacks, filters,profilesPair likes DBSwipe historyProfile geoindex
    Ana likes Ben
    recorded

    4Ana swipes right on Ben. The swipe service appends (Ana, Ben, right) to her swipe history and adds Ben to her seen filter.

  5. Serving a stack, a swipe, a simultaneous match and a cache node failure, step 5Phone app to API gateway; API gateway to Stack service; API gateway to Swipe service; Stack service to Stacks, filters, profiles; Swipe service to Stacks, filters, profiles; Swipe service to Pair likes DB; Swipe service to Swipe history; Pair likes DB to Match notifier, asynchronous; Match notifier to Phone app, asynchronous; Stack builder to Profile geo index; Stack builder to Swipe history; Stack builder to Stacks, filters, profiles.Phone appAPI gatewayStack serviceSwipe serviceMatch notifierStack builderStacks, filters,profilesPair likes DBAna ✓ Ben ✗Swipe historyProfile geoindex
    Ana → Ben
    true
    Ben → Ana
    false
    matched
    false

    5One atomic upsert sets Ana's flag on the (Ana, Ben) row. Ben's flag is still false, so Ana's response says there is no match.

  6. Serving a stack, a swipe, a simultaneous match and a cache node failure, step 6Phone app to API gateway; API gateway to Stack service; API gateway to Swipe service; Stack service to Stacks, filters, profiles; Swipe service to Stacks, filters, profiles; Swipe service to Pair likes DB; Swipe service to Swipe history; Pair likes DB to Match notifier, asynchronous; Match notifier to Phone app, asynchronous; Stack builder to Profile geo index; Stack builder to Swipe history; Stack builder to Stacks, filters, profiles.Phone appAPI gatewayStack serviceSwipe serviceMatch notifierStack builderStacks, filters,profilesPair likes DBAna ✓ Ben ✓Swipe historyProfile geoindex
    Ana → Ben
    true
    Ben → Ana
    true
    matched
    now

    6Ben swipes right 3 ms later on another server. His update targets the same row, so it waits for any running update and then sees Ana's flag.

  7. Serving a stack, a swipe, a simultaneous match and a cache node failure, step 7Phone app to API gateway; API gateway to Stack service; API gateway to Swipe service; Stack service to Stacks, filters, profiles; Swipe service to Stacks, filters, profiles; Swipe service to Pair likes DB; Swipe service to Swipe history; Pair likes DB to Match notifier, asynchronous; Match notifier to Phone app, asynchronous; Stack builder to Profile geo index; Stack builder to Swipe history; Stack builder to Stacks, filters, profiles.Phone appAPI gatewayStack serviceSwipe serviceMatch notifierStack builderStacks, filters,profilesPair likes DBSwipe historyProfile geoindex
    matches created
    1

    7The same statement sets matched_at, so Ben's response says it is a match. The notifier reads the change stream and pushes to Ana, keyed by pair so a redelivered event sends no second push.

  8. Serving a stack, a swipe, a simultaneous match and a cache node failure, step 8Phone app to API gateway; API gateway to Stack service; API gateway to Swipe service; Stack service to Stacks, filters, profiles; Swipe service to Stacks, filters, profiles; Swipe service to Pair likes DB; Swipe service to Swipe history; Pair likes DB to Match notifier, asynchronous; Match notifier to Phone app, asynchronous; Stack builder to Profile geo index; Stack builder to Swipe history; Stack builder to Stacks, filters, profiles.Phone appAPI gatewayStack serviceSwipe serviceMatch notifierStack builderStacks, filters,profiles1 of 10 downPair likes DBSwipe historyProfile geoindex

    8A cache node dies, taking the stacks and filters of 1 in 10 users. The builder rebuilds their filters from swipe history and then builds stacks on demand.

1 / 8

Deep dives

Storing 70,000 swipes a second

Each swipe is a small append, and the only read is "all swipes by this user", which rebuilds a seen filter. A single relational primary cannot absorb 70,000 writes a second.

One option is to shard a relational database by swiper id, but that means rebalancing many shards for data that is never updated or joined. The other is a log-structured store such as a wide-column database (see indexing), which appends each write to memory and a commit log and flushes to disk in large sequential batches.

I choose the wide-column store, partitioned by swiper_id and clustered by target_id, so a repeated swipe overwrites one cell and a user's history is one partition of about 36,500 swipes a year, under 2 MB. Left swipes only feed the seen filter, so one replica's acknowledgement is enough for them, while right swipes use a quorum write so none is lost.

Detecting a match exactly once

Ana and Ben may swipe right 3 ms apart on different servers, and exactly one match must result.

Option 1: write my swipe, then read theirs

Each server writes "Ana likes Ben" under Ana's key, then reads whether Ben's key says he likes Ana. On an eventually consistent store both reads can run before either write has reached the replica they read, so neither server sees the other's like and the match is never created. With quorum reads and writes (R + W greater than N, no sloppy quorums) at least one request sees the other's like, but both can, so both would announce the match unless creation is idempotent.

Option 2: one row for the pair with an atomic update

Both directions are stored in one row keyed by the pair, and each swipe is an atomic update that sets its own flag. The database runs updates on one row one after the other, so the second update always sees the first one's flag and sets matched_at in the same statement. The cost is a second write for every right swipe.

Option 3: a stream partitioned by pair

Each right swipe is published to a stream partitioned by the pair key, and one consumer per partition processes both likes in order, with no locks. The weakness is that this is asynchronous, so the second swiper learns about the match later, by push, which breaks the 200 ms requirement.

Ways to detect a mutual right swipe
OptionGood atBad atPick it when
Write mine, read theirsIt needs no extra table.Replica reads can miss the match, and quorum reads can announce it twice.You already have a strongly consistent store and can accept deduplication work.
One pair row, atomic update Usual defaultExactly one request sees both flags and records the match.It adds a second write for each right swipe.The second swiper must see the match instantly, as here.
Stream partitioned by pairOne consumer sees both likes in order, with no locks.The second swiper hears about the match later, by push.Instant feedback is not required.

The pair row fits because the match must appear in the swipe response. The statement below is what Ben's server runs when he swipes right on Ana (Ana is user 12 and Ben is user 91). Ana's statement mirrors it.

Ben likes Ana: set my flag, match if theirs is set
INSERT INTO pair_likes (low_id, high_id, low_likes_high, high_likes_low)
     VALUES (12, 91, false, true)
ON CONFLICT (low_id, high_id) DO UPDATE
   SET high_likes_low = true,
       matched_at = CASE WHEN pair_likes.low_likes_high
                         AND pair_likes.matched_at IS NULL
                         THEN now() ELSE pair_likes.matched_at END
RETURNING matched_at;    -- not null: tell Ben "It's a match"

Only the second swipe can see both flags, and detecting and storing the match are one write. If they were two steps, a crash between them would lose the match, because no later swipe rechecks those likes. A retry finds matched_at already set and returns the same answer, and the pair key doubles as the match id, so a redelivered event cannot create a second match.

Building the stack

A good candidate is nearby, fits my filters, has me inside theirs, is unseen and is recently active. The query could run when the user opens the app, which gives fresh data but makes the user wait for a geo query and a seen check on every open. The alternative is to precompute the stack in the background and cache it, so that opening the app is a cache read. I choose the second, because the requirements allow a stack a few minutes old.

The builder runs one query against the search index, with a geo filter built from cells and neighbouring cells as in Design Ride Sharing, plus filters on age, gender, the other person's preferences, and activity in the last 30 days. It drops profiles I have seen, ranks the rest, and caches 200 ids. The stack service serves 20 at a time and requests a refill when fewer than 50 remain, so a fast swiper never waits.

The cost of precomputing is staleness. A stacked profile may have been deleted since, so the stack service checks each batch of 20 against deletions before returning it.

Never showing a profile twice

The builder must remove everyone I have already swiped on from about 1,000 candidates.

Option 1: read the swipe history

The builder looks up each candidate in the user's swipe history. The answer is exact and nothing extra is stored, but each refill needs a batched read of 1,000 keys.

Option 2: keep an exact set in the cache

The cache holds every id the user has swiped on, so each lookup is one in-memory operation. At 10,000 swipes of 8 bytes each, that is 80 KB per user and 1.6 TB for 20 million users, which is a lot of cache memory for this one purpose.

Option 3: keep a Bloom filter in the cache

A Bloom filter (see Design a Web Crawler) answers either "definitely not seen" or "probably seen", so it never lets a repeat through. At a 1% false-positive rate it needs 12 KB per user, 240 GB in total. The cost is that about 1 in 100 unseen profiles is wrongly treated as seen and never shown, and a filter cannot remove items.

I choose the Bloom filter because a profile must never be shown twice, and it can only err in the safe direction. In a small town 1% of 1,000 candidates is 10 people who are never shown, so users with a small pool should use the exact set. When a user outgrows the size the filter was built for, rebuild a larger one from the history, because an overfull filter's false-positive rate rises quickly.

Bottlenecks and failures

What breaks first at 10 times the load

At 10 times the load there are 700,000 swipes a second. The swipe history scales by adding nodes, because each user's partition is independent. The pair store adds shards for 210,000 right swipes a second, and a popular profile's likes are spread over many pair rows, so no row is hot.

What happens when each component fails

When this failsWhat happensWhat you do
A cache nodeIts users lose their stacks and seen filters.Rebuild the filter from history, then build stacks on demand.
A pair-store shard primaryRight swipes on that shard fail until failover.A synchronous replica keeps acknowledged likes, and the app retries.
A swipe-history nodeIts replicas take over its writes.With replication factor 3, quorum writes still succeed.
The stack builderStacks drain because they are not refilled.Refills start at 50 left, so users keep swiping during a restart.
The match notifierThe first swiper gets no push, but the match is already stored.It resumes from its stream position, and the match list already shows the match.

What a strong answer sounds like

LevelWhat the answer does
JuniorA junior candidate checks one swipes table for the reverse swipe and does not see the race until the interviewer points it out.
Mid-levelA mid-level candidate estimates the write rate, picks a write-optimised store and precomputes stacks.
SeniorA senior candidate shows how write-then-read can miss or duplicate a match, decides it with one atomic write on a pair key, and weighs the Bloom filter's 1% loss against its memory saving.
The simultaneous swipeStrong answer
Interviewer

Two people swipe right on each other in the same millisecond. What happens?

You

Both likes go to one row keyed by the pair, and updates to that row run one after the other. Only the second request sees both flags, so it sets matched_at in the same statement and tells its user. That gives exactly one match, stored before anyone hears of it.

Interviewer

Why not just read the other person's swipe?

You

Replica reads can both miss the other's swipe, and strongly consistent reads can both see it and announce twice. The pair row gives exactly one match, at the price of one extra write for each right swipe.

Follow-up questions

How would you add a "see who liked you" feature?

The data is the set of pair rows where their flag is set and mine is not, and querying it needs an index by user, so I would keep a per-user "liked by" list fed from the pair rows' change stream. The server returns it only to entitled users and never puts it in the stack.

A user travels to another city. What changes?

The location update moves the user's document in the geo index and discards their cached stack, so the next refill is built for the new place, and the seen filter is kept.

A user unmatches. Can they appear again?

No. The swipe stays in the history and the seen filter, so neither person is shown to the other again. The pair row keeps both flags and gains an unmatched_at time, so a retried swipe cannot recreate the match.

How would you shard the geo index?

Shard it by region and cap the maximum distance at about 100 km, so a query stays in one region. A profile near a border is stored in both regions.

How would you limit free users to a fixed number of swipes a day?

Keep a counter per user in the cache, with an expiry at the end of the user's day, and increment it before the swipe service accepts a swipe. This is a fixed-window rate limit.

Self-review: swipe matching0 of 13

After your attempt, tick what you really did. Be strict: "I thought about it" is not "I said it out loud".

Before drawing

Numbers and interfaces

Design and depth

Failure