AlgoViz

Real-time APIs

medium 27 min

HTTP lets the client ask and the server answer.

See it run

Polling, long polling, server-sent events and WebSocket: who speaks first, what stays open and how you catch up.

Operations

Short polling

Ask every second. Most answers say nothing new.

Step 1 of 4

0:00 ask

GET /messagesBrowserServer

Requests

1

304

GET /messages answers 304: nothing changed.

Step 1: 0:00 ask

while True:    r = GET("/messages", after=last)    if r.status == 200:        show(r.body)    sleep(1)
Delay
Up to 1 s
Requests
86,400 a day
Average delay
half the interval

Polling is not shameful. For a page that changes every few minutes it is the cheapest thing to run.

In the real world

The same real-time apis doing real jobs. Pick one and step through it.

Prices only flow one way, so server-sent events are enough.

Step 1 of 3

Open once

event streamBrowserServer

price $42.50

The page opens /events and shows $42.50.

Step 1: Open once

Common mistakes

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

  • No way to catch up

    critical

    Every phone loses its connection many times a day. Messages sent in the gap are gone unless they are numbered and saved.

    Wrong
    socket.on("open", listen)
    Right
    connect(after=last_seen)# server: send history first, then live
  • Taking messages out of one shared queue

    critical

    With long polling, the first client to poll empties the queue and the others never see those messages. Each client needs its own cursor.

    Wrong
    messages = queue.take(10)
    Right
    messages = log.after(client_cursor, 10)
  • Reconnecting at once, all together

    major

    After a restart every client returns in the same second and takes the server down again.

    Wrong
    on_close(connect)
    Right
    sleep(random(0, min(30, 2 ** tries)))connect()
  • Letting a slow client fill the memory

    major

    If one client reads slowly, its messages pile up in the server. Give each client a limited buffer and close it when full: it can catch up.

    Wrong
    client.buffer.append(m)      # no limit
    Right
    if len(client.buffer) >= 256:    client.close()           # it will catch up

Interview tips

  • How to explain it

    • Short polling asks often. Long polling asks and waits.
    • SSE is one long answer: server to client.
    • WebSocket is a two-way connection.
    • All of them need a way to catch up.
  • Memory trick: WSL

    • W: WebSocket, two ways, for chat and games.
    • S: server-sent events, one way, for feeds and logs.
    • L: long polling, the fallback that works everywhere.
  • Reach for it when

    • People wait for each other: chat, editing, games.
    • A value changes while someone watches.
    • Seconds matter. If minutes are fine, poll.
  • Say in an interview

    • How many connections, and memory per connection.
    • How a message finds the right server.
    • What a client does after a drop.
    • What happens on a deployment.