1. Functional requirements
- Create a profile with preferences: age range, interests, maximum distance.
- View a stack of potential matches within the preferences and the maximum distance of the current location.
- Swipe right or left on profiles one at a time: yes or no.
- Get a match notification when two users both swipe yes.
Below the line: upload pictures, chat after a match, super swipes and other premium features.
Scale: 20M DAU × 100 swipes/user/day is 2B swipes/day; ~23K swipes/s average, ~70K/s at a 3x evening burst; ~100 B per swipe is ~200 GB/day, ~73 TB/year, so the swipe store shards from day one. Hello Interview says 200 swipes in one spot; we use 100. Profile traffic is a small fraction of that; the 70K/s burst of swipes, each a write that must also find its reverse swipe, drives the design.
2. Non-functional requirements
- Strong consistency on swipes: a mutual yes always matches, even simultaneous
- Low latency: the stack loads in under 300 ms
- Never show a profile the user has already swiped on
- Scale: 20M DAU and 2B swipes/day, write heavy
Below the line: fake-profile protection, monitoring and alerting.
3. Core entities
- Users: a user of the app and also a profile shown to others; preferences, location
- Swipes: yes or no, from swiping_user about target_user
- Matches: two users who both swiped yes
Match is its own entity, never a list inside the profile row: the swipe path creates it, both users list it, its id is the idempotency key for the push.
4. API routes
Identity from the JWT, never a userId in the body or a header: a JSON Web Token is a signed token in the Authorization header; the server verifies the signature and reads the user id from inside it. Simple enough to start; no pagination on the feed, the app calls it again when the stack runs out.
Create a profile
POST /profile
body: age_min, age_max, distance, interestedIn female | male | both, ...
returns: ok
Get the stack
GET /feed?lat={}&long={}&distance={}
returns: User[] age and interests load server-side from the profile
Swipe
POST /swipe/{userId} the userId in the path is the profile swiped on
body: decision yes | no a no is stored too: it is the seen-set for 6.3
returns: match true | false
Notifications are not a route: the swiper learns of a match in the swipe response; the other user gets a push through APNS or FCM, sent by the swipe service.
5. High-level design
The simplest system that satisfies the three routes: one client, one gateway, two services with a database each, push to phones. Two services because profile reads and writes are far less frequent than swipe writes. The deep dives change this picture.
Boxes, drawing order: client · API gateway · profile service, swipe service · user DB under profile, swipe DB under swipe · APNS/FCM beside swipe.
One flow per route:
- Profile:
POST /profile→ gateway verifies the JWT → profile service → upsert the row in the user DB. - Feed:
GET /feed?lat&long&distance→ profile service → one query on the user DB: within distance, in my age range, matching interests, not me → User[]. Incredibly inefficient at this stage: a scan with a distance calculation per row. - Swipe to match:
POST /swipe/{B}yes from A → swipe service → write the swipe → read B’s swipe on A → yes: write the match row, return match true, push “You matched!” to B through APNS or FCM.
Data model:
User id, name, age, gender, lat, long, interestedIn, age_min, age_max, distance, interests[]
Swipe swiping_user_id, target_user_id, direction, created_at partition key: swiping_user_id
Match id, user_a, user_b, created_at
User DB: Postgres; profile writes are rare, reads are one row by id, the feed query is the one thing it does badly. Swipe DB: Cassandra; 2B writes a day only fit in a store that spreads rows across nodes by a partition key, the column whose hash picks the node that holds the row; here swiping_user_id. Matches: a table beside the swipes.
Parked flaws, fixed in the deep dives: the swipe write and the reverse-swipe read are two steps, so two yeses at the same instant miss each other and no match is made (6.1); the feed is a scan over 20M rows, nowhere near 300 ms, repeated on every refill (6.2); nothing stops the feed from returning a profile the user swiped last week (6.3); the push to the other user can be lost (6.4).
6. Deep dives
Order in the room, 60 minutes: 6.1 must · 6.2 must · 6.3 must · 6.4 one line. Each deep dive opens with three spoken sentences: Problem, Pick, Cost. Everything under them is what you say when the interviewer digs, as a ladder: Bad, Good, Great.
6.1 Consistent swipes: Redis Lua on the pair key, Cassandra behind it
Problem. A and B swipe yes on each other at the same instant; each check for the reverse swipe runs before the other write is visible, both find nothing, and the match is never made.
Pick. The swipe write and the reverse-swipe read become one atomic step on one key: Redis, key swipes:{smaller_id}:{larger_id}, a Lua script that sets my swipe and reads the other one in one run. Both directions of a pair hash to the same key, so the same shard, which runs scripts one at a time, so the second script always sees the first. Cassandra stays the durable store, partitioned by the same sorted pair; Redis keeps recent swipes and flushes them to Cassandra.
Cost. A Redis cluster to run: node failures, ring rebalancing, memory for the recent swipes. Lose Redis and you lose only match detection for the very recent swipes not yet flushed; everything older is in Cassandra. Rejected: lightweight transactions, Paxos round trips on every swipe at 70K/s.
Draw: Redis in front of the swipe DB with the script inside it; the swipe DB’s partition key changes from swiping_user_id to the sorted pair.
Ladder
- Bad: poll the database for matches. A job scans recent swipes for mutual yeses. Latency between polls, load on the database.
- Good: Cassandra lightweight transactions, Cassandra’s compare-and-set: Paxos among the replicas of one partition, linearizable. Single partition only, several round trips per write, and 2B swipes a day cannot sit in one partition.
- Great: sharded Cassandra, single-partition batch. Partition key the sorted pair, so both directions live in one partition; a
BEGIN BATCHinserts my swipe and selects the reciprocal one. Drawbacks: partitions grow without bound, hot partitions for very active users, something has to archive. - Great, the pick: Redis for the atomic step. The key holds a hash with fields
{smaller}_swipeand{larger}_swipe; a Lua script is a small program Redis runs start to finish with nothing interleaved: HSET my field, HGET the other, both yes is a match. Consistent hashing routes the key to one shard.
Flow
- A swipes yes on B:
POST /swipe/B {yes}→ swipe service →pair = sort(A, B)→EVAL swipe.lua swipes:{pair} A yes. A = 17, B = 42: both swipes hitswipes:17:42, one shard, one queue; keyed by swiper, the HLD key, they would be two keys on two shards, each script reading the other’s key before the other’s write lands. - The script:
HSET swipes:{pair} {A}_swipe yes, thenHGET swipes:{pair} {B}_swipe; yes back is a match: write the match row, return match true, push to B (6.4). - Swipes flush from Redis to Cassandra in batches, same pair key; a Redis miss falls through to one read of the pair partition in Cassandra, so an old yes still matches and Redis only has to catch the recent ones.
21:00:00.100 A yes on B swipes:17:42 HSET 17_swipe yes · HGET 42_swipe → nil no match yet
21:00:00.102 B yes on A swipes:17:42 HSET 42_swipe yes · HGET 17_swipe → yes match: B's response, push to A
21:03:40.000 A no on C swipes:17:88 HSET 17_swipe no · HGET 88_swipe → yes no match, C's yes stays
6.2 The stack under 300 ms: a geo index plus a precomputed stack
Problem. The feed is a query over 20M rows with a distance calculation per row, nowhere near 300 ms, and the app repeats it every time the stack runs out. Pick. Both: a search database with a geospatial index on profiles, Elasticsearch or OpenSearch, fed by CDC from the user DB; and a precomputed stack per active user in a cache. Serve the cached stack first, switch to live index queries as it drains, refresh when few profiles remain. Cost. Two copies of every profile with the index seconds behind the database; cache memory for every recently active user; a stack can be stale by up to its TTL.
Draw: a search index beside the user DB with a CDC arrow into it; a feed cache beside the profile service with a background job feeding it.
Ladder
- Bad: the HLD query, a scan of the user table with a distance per row.
- Good: an indexed database for real-time queries: indexes on age and preferences, and a geospatial index on location, lat/long stored as cells, geohash or quadtree, so “within 10 km of here” is a lookup of a few cells, not a scan; Elasticsearch or OpenSearch has one built in. Drawback: a second copy to keep in sync, by CDC, change data capture: the user DB’s change log streamed into the index in batches, so writes go to one place and the index follows within seconds.
- Good: precompute and cache. A background job builds a stack per user and caches it, run off peak. Drawbacks: very active users drain it, it goes stale, a drained cache falls back to the expensive query.
- Great, the pick: the combination, cached first, live as it drains.
Cache lifecycle
- Built: a background job, off peak, only for recently active users; one list per user in Redis,
feed:{userId}, TTL under an hour. - Drains: each feed call takes the next batch off the list; under about 10 left, one live index query, distance plus age plus interestedIn minus already swiped (6.3), tops it up, so the user never waits on an empty stack.
- Stale: the TTL, a filter change or a significant location move throws the list away; a live query refills it now, the scheduled job recomputes it next run.
6.3 Never show a swiped profile: client cache plus a Bloom filter
Problem. The feed does not know what the user already swiped, so last week’s profile comes back; checking the full swipe history on every feed call scans a list that only grows. Pick. The phone keeps the last K swipes and drops them from any stack it gets, the server filters candidates against the swipe history, and users with a long history get a Bloom filter: a fixed-size bit array that answers “did I swipe this?” with no false negatives. Cost. False positives, tunable by size: some profiles the user never saw are hidden for good. No deletes, so the filter is rebuilt from the swipe DB after a node outage. The client cache assumes one device per user.
Draw: a swipe list in the client; a Bloom filter per user beside the feed cache, rebuilt from the swipe DB.
Ladder
- Bad: query the swipe DB for everything I swiped, partitioned by swiping user, and run a contains check per candidate. Eventual consistency can re-show a swipe from seconds ago; the contains check gets slow as the history grows.
- Great: client cache of the K most recent swipes plus the server filter. The most recent swipes are exactly the ones the database may not have yet, and they are on the phone. Valid because one person uses one device.
- Great, the pick: add a Bloom filter for users above a swipe-history threshold, so the server check is constant time and constant space whatever the history.
Bloom filter
- A bit array of m bits and k hash functions, one filter per user, keyed by the target id.
- Insert on swipe: hash the target id k ways, set those k bits.
- Lookup on feed: hash k ways, read the k bits; any zero means definitely not swiped, show it.
- All ones means maybe swiped, hide it; no deletes, and the false-positive rate climbs as it fills, so size it for the history.
For the user a false positive is a profile they never saw and never will; a repeat is worse, and the requirement forbids it.
6.4 Match notification: the notification-system slice
Problem. The swiper gets the match in the swipe response; the other user is not in a request, and a push can be dropped. Pick. The swipe service writes the match row, returns match true, and pushes “You matched!” to the other user through APNS for iOS or FCM for Android, at least once, with the match id, the sorted pair, as the idempotency key, so a retried push is one match, not two. Cost. Push is best effort, so the app also pulls its matches on open; the match row is the truth, the push is a hint.
Final design
Two stores, two mechanisms, say both: a geospatial index on profiles in the search database, for finding who is near; a partition key on swipes in Cassandra, for placing a row on a node. The key changes once: swiping_user_id in the HLD, the sorted pair from 6.1.
Trade-offs, the two to say:
- Redis Lua on the pair key over Cassandra lightweight transactions: one atomic step on one shard at memory speed, at the cost of a cluster to run and match detection lost for unflushed swipes if a node dies.
- Cached stack plus live index over either alone: the first cards come from memory and the refill from the geo index, at the cost of two copies of every profile and a stack that can be stale by its TTL.
Study only, not for the room: the numbers worked, the 6.3 flow.
Numbers
20M DAU × 100 swipes = 2B/day. 2B / 86,400 s ≈ 23K/s; × 3 for the evening ≈ 70K/s. 100 B per swipe × 2B ≈ 200 GB/day; × 365 ≈ 73 TB/year.
6.3 flow
On swipe: the phone appends the target id to its local list of K; the swipe service adds it to bloom:{userId}. On feed: the index returns candidates, the server drops any whose k bits are all set, the phone drops any in its list. Beyond the page: the per-user history is a second Cassandra table keyed by swiping_user_id, written on the same flush as the pair table, for the server filter and for rebuilding a lost filter.