AlgoViz

Push and pull

easy 25 min

When data moves between two parts of a system, somebody has to start the transfer.

See it run

The same events delivered by asking, waiting, sending, and a mix, and what happens when the receiver is too slow.

Operations

Short polling

The client asks every 1.5 seconds. An item appears at 2 s and another at 4 s.

Step 1 of 6

1.5 s: ask

Our systemafter 0?readClientFeed APIany serverEvents

Stored

nothing

Client has

nothing

The client asks for anything after 0.

Step 1: 1.5 s: ask

def poll(api, interval):    after = 0    while True:        items = api.get(after=after)        for item in items:            handle(item)            after = item.id        sleep(interval + jitter())
Delay
0 to 1.5 s
Extra requests
Empty polls
Push
one network trip, no empty request

Say the sum out loud: clients divided by interval is the request rate.

In the real world

The same push and pull doing real jobs. Pick one and step through it.

Two push servers hold 100,000 clients. One of them restarts.

Step 1 of 4

Normal

Our systemClientsPush server 160,000Push server 240,000

Server 1 holds 60,000 clients and server 2 holds 40,000.

Step 1: Normal

Common mistakes

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

  • Every client polls at the same moment

    major

    A whole interval of load arrives in one second, then the servers idle.

    Wrong
    sleep(60 - now() % 60)     # on the full minute
    Right
    sleep(60 * random.uniform(0.8, 1.2))
  • Unlimited buffers for slow clients

    critical

    One phone on a bad network can use up the memory of the server that thousands share.

    Wrong
    client.buffer.append(item)
    Right
    if len(client.buffer) == LIMIT:    client.close("too slow")else:    client.buffer.append(item)
  • Trusting push as the only copy

    critical

    Connections break. An event that is sent but not stored is gone.

    Wrong
    broadcast(message)
    Right
    message.id = store.append(message)broadcast_signal(message.id)   # clients pull after their cursor
  • Asking for changes since a time

    major

    Clocks differ and commits finish late, so events fall between two answers.

    Wrong
    GET("/events?since=" + now())
    Right
    GET("/events?after=" + last_id)

Interview tips

  • How to explain it

    • Pull: the consumer asks. Push: the producer sends.
    • The data flows the same way; only the first move differs.
    • Pull wastes requests, push keeps state.
  • Memory trick

    • PUSH: Producer initiates, Updates instantly, Server sends, Holds a connection.
    • PULL: Periodic requests, User asks, Latency varies, Load on each poll.
    • Hybrid: push to notify, pull the data.
  • Choose pull when

    • The user starts every action.
    • Seconds or minutes of delay are fine.
    • Consumers work at different speeds.
  • Choose push when

    • Updates must arrive at once.
    • Most polls would be empty.
    • You can keep connections, replay and limit buffers.