AlgoViz

Design a Group Chat App (Slack/Discord)

hard 45 min

Design team chat: channels, threads, unread badges, presence and search.

See it run

Send, deliver, thread, search, presence and reconnect, one hop at a time.

Operations

Send a message

Check, number, commit, answer. Live delivery starts after the commit.

Step 1 of 13

Client sends

Our systemSenderChat serviceSession, membersChannel shardChannel serverGateways

text

Hello team!

A POST with the channel, the text and a client_msg_id. Why: HTTP gives a status and a safe retry.

Step 1: Client sends

def send(session, channel, client_id, text):    user = check_session(session)    check_member(user, channel)    check_limits(user, channel)    with shard(channel).transaction() as tx:        seq = tx.lock_and_next(channel)        row = tx.insert(channel, seq, user, client_id, text)        tx.outbox(channel, seq)    channel_server(channel).hint(row)      # best effort    return 201, row
Latency
about 40 ms
Load
5,800/s
Peak (3x)
17,400/s

A repeat of the same client id fails on the unique key. Return the first row, not an error.

In the real world

The same group chat doing real jobs. Pick one and step through it.

The push for 412 never arrives and the room is quiet.

Step 1 of 4

Push lost

Our systemSenderChat serviceSession, membersChannel shardChannel serverGateways

The channel server restarts while sending 412.

Step 1: Push lost

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Pushing before saving

    critical

    If the commit fails, people saw a message that does not exist.

    Wrong
    push(message)db.insert(message)
    Right
    db.insert(message)   # commitpush(message)
  • Ordering by clock

    major

    Clocks differ and can step back. NTP reduces the error but does not remove it.

    Wrong
    messages.sort(key=lambda m: m.time)
    Right
    messages.sort(key=lambda m: m.event_seq)
  • A counter for every member

    major

    One message in a 10,000 member room becomes 10,000 writes.

    Wrong
    for m in members:    incr(f"unread:{m}")
    Right
    unread = channel.last_msg - position[user]
  • Reading, then subscribing

    critical

    An event between the two steps is never seen.

    Wrong
    gap = read(after=last)subscribe(channel)
    Right
    subscribe(channel)gap = read(after=last)   # drop repeats
  • Filtering search afterwards

    critical

    Counts and empty pages reveal what is hidden.

    Wrong
    hits = index.query(words)return [h for h in hits if allowed(h)]
    Right
    index.query(words, filter=allowed_channels)

Interview tips

  • Lead with numbers

    • 5,800 messages a second, 290,000 deliveries.
    • 3M sockets, 100 gateways.
    • 91 TB a year splits the store.
  • Say the promises

    • Never lost: committed messages.
    • May be late: search and notices.
    • May vanish: typing and dots.
  • Follow the absent

    • Draw the offline member early.
    • Subscribe, then read.
    • Heartbeats carry the newest number.
  • Plan the bad day

    • Spread reconnects.
    • Drain gateways slowly.
    • Watch the age of the outbox.