Bugpit

Lessons › Design skills

Push or poll

A live cricket score app has 3 million people watching. The app asks your servers for the score every few seconds, and nearly every answer is "nothing changed". You want the server to tell people when something happens instead.

Play this level

Part of Bugpit Pro. It runs in your browser: build the design, press Run, and watch what breaks.

The lesson

HTTP is ask and answer: the server can't speak until the client asks. So the simplest way to stay up to date is polling, which means asking again every few seconds. It works everywhere, and most of the answers are wasted.

Long polling is a small improvement. The server holds the request open until it has news, answers, and the client asks again at once. There are fewer wasted answers, but a request is still tied up for each user.

Server-sent events keep one connection open and let the server push down it, one way only. That fits scores, feeds and notifications. It's plain HTTP, so it passes through proxies easily.

A WebSocket is open both ways, so the client can send over it too. That fits chat, games and shared editing. With either kind every online user holds a connection, so the limit becomes how many you can hold open.

Between your own services the choice is different. REST with JSON is easy to read and debug. gRPC sends compact binary messages and checks them against a shared definition, so it's faster and stricter, and harder to inspect by hand.

This simulator has one Realtime Gateway part, priced like a WebSocket service. It doesn't model the drop in requests when polling stops, so the traffic here stays the same.

What you have to achieve

4,000 requests a second at the busiest hour, 20% of them writes.

What you learn

3 million people now hold a connection and hear about a wicket the moment it falls. For one way updates like this, server-sent events would do the job with less to run. Pick the simplest thing that carries the direction your data flows.

Check yourself

A stock ticker only sends prices from the server to the browser. What's the simplest fit?

Show the answer

Server-sent events. The data flows one way, so a one way push is enough, and it's the simplest to run.

Which of these needs a connection that's open in both directions?

Show the answer

A multiplayer game. Players send moves and receive everyone else's, many times a second. That's what WebSockets are for.

What's the main cost of polling every 2 seconds?

Show the answer

Most requests return nothing new, and your servers still answer them all. Polling is simple and works anywhere. You pay for it in requests that carry no news.

Two of your own services call each other thousands of times a second and must agree on the shape of every message. What fits?

Show the answer

gRPC. gRPC's shared definition catches mismatched messages early, and its binary format is cheap to send.

Try it yourself

More in Design skills

← Is that server alive? The cache that took down the database →