Polling vs WebSockets vs Webhooks

Five ways for one side to learn that something changed on the other: short polling, long polling, server-sent events, WebSockets and webhooks, and how to choose between them.

Overview

Many features need one side to find out that something changed on the other: a chat message arrived, a build finished, a payment succeeded, a price moved. There are only two ways to find out. The side that wants the news can keep asking (polling), or the side that has the news can tell it (push). Everything in this topic is a variation on those two.

Five techniques cover almost every design. Short polling asks on a timer. Long polling asks, and the server holds the question open until it has an answer. Server-sent events (SSE) keep one HTTP response open and stream events down it. WebSockets turn one connection into a two-way channel. Webhooks push events from one server to another with an ordinary HTTP POST.

They differ in who opens the connection, which way data flows, how late news arrives, how much work is wasted, and what the infrastructure must hold open. Interviewers want to see you pick the simplest one that meets the need, and say what happens when a connection drops.

What you will be able to do

  • Compare the five techniques by direction, delay, waste and cost to run.
  • Work out the request rate and the average delay of a polling design.
  • Explain how SSE resumes after a drop and how a WebSocket connection is opened.
  • Say what a webhook receiver must do to be safe and reliable.
  • Choose a technique for a feature, and combine them where one is not enough.

Key concepts

  • Short polling
  • Long polling
  • Server-sent events (SSE)
  • WebSockets
  • Webhooks
  • Who opens the connection
  • Delay vs wasted requests
  • Resuming after a drop

Interview questions

  • Compare short polling, long polling, SSE, WebSockets and webhooks. When would you use each?
  • 10,000 dashboards refresh every 5 seconds. What does that cost, and what would you change?
  • When would you choose SSE over WebSockets?
  • How does a WebSocket connection start, and what does that mean for your infrastructure?
  • How do you build a reliable webhook receiver?
  • A client was disconnected for a minute. How does it avoid missing updates?
  • Design how a user learns that their payment went through.
  • Why not use WebSockets for everything real-time?

Sign up free to read the full topic

More System Design topics

Start free on Cariara · Coding problems · System design studio