Push and pull
easy 25 minWhen 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
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
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
majorA whole interval of load arrives in one second, then the servers idle.
Wrongsleep(60 - now() % 60) # on the full minuteRightsleep(60 * random.uniform(0.8, 1.2))Unlimited buffers for slow clients
criticalOne phone on a bad network can use up the memory of the server that thousands share.
Wrongclient.buffer.append(item)Rightif len(client.buffer) == LIMIT: client.close("too slow")else: client.buffer.append(item)Trusting push as the only copy
criticalConnections break. An event that is sent but not stored is gone.
Wrongbroadcast(message)Rightmessage.id = store.append(message)broadcast_signal(message.id) # clients pull after their cursorAsking for changes since a time
majorClocks differ and commits finish late, so events fall between two answers.
WrongGET("/events?since=" + now())RightGET("/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.