Your system needs to find out that something happened somewhere else: a payment completed, a message arrived, a build finished. There are three broad ways to learn it. You can keep asking. You can ask to be told. Or you can keep a line open and listen. Each has a place, and the fashionable choice is not always the right one.

Polling: keep asking

Your client sends a request every so often: “Is my payment done?” The server answers yes or no. That is the whole idea.

Its virtues are plain. It works through firewalls and ordinary HTTP, it needs no special server support, and the client controls the pace. If the poller crashes and restarts, it simply asks again and catches up, which makes it robust. Its cost is waste and delay. Take an illustrative case: 10,000 users each polling every 5 seconds is 2,000 requests per second, and most answers say “nothing yet”. And a change that happens just after a poll is not noticed until the next one, so the delay can be as long as the interval.

Two refinements help. Conditional requests let the server answer “no change” cheaply, using validators such as ETags. Long polling holds the request open until something happens or a timeout passes, then the client immediately asks again, which gives near-instant delivery over plain HTTP at the cost of many open requests.

Webhooks: be called back

With a webhook, you give the other system a URL on your server. When the event happens, it sends an HTTP request to that URL. Payment gateways, source-hosting platforms and messaging providers commonly work this way. It is efficient: nothing flows until something happens, and notification is fast.

But the burden moves onto you. Your endpoint must be publicly reachable, which is awkward on a laptop or behind a corporate firewall. Delivery is usually at least once, meaning the sender retries on failure, so you will sometimes receive the same event twice and must handle duplicates; an event ID plus a record of processed IDs is the standard answer. Events can also arrive out of order. You should verify that a call genuinely came from the sender, typically by checking a signature computed over the body with a shared secret, since anyone can post to a public URL. And answer quickly: acknowledge receipt, then process in the background, or the sender will time out and retry.

The other trap is the downtime gap. If your server is down for an hour, most senders retry for a limited time and then give up. That is why careful integrations pair webhooks with a periodic reconciliation job that polls for anything missed. Which means that, in the end, a robust design often contains both.

WebSockets: keep a line open

A WebSocket starts as an HTTP request and then upgrades to a persistent two-way connection. After that, either side can send messages at any time, cheaply, without new request overhead. This is the natural fit for chat, live cricket scores, collaborative editing, trading dashboards, and multiplayer games: situations with frequent updates where a human is watching.

What you pay for is state. Each connected user holds an open connection on some server, so capacity is measured in concurrent connections, and your load balancers and proxies must be configured to allow long-lived connections. Deployments get harder, since restarting a server drops its connections and thousands of clients reconnect at once. Clients must handle disconnects, reconnect with backoff, and work out what they missed. If you have many servers, a message for user A must reach whichever server holds A’s connection, so you usually add a publish-subscribe layer such as Redis or a message broker between them.

Server-Sent Events are worth a mention here: a simpler, one-way (server to client) stream over ordinary HTTP, with built-in reconnection in browsers. If you only need to push updates down to the browser, they are often enough, and simpler than WebSockets.

My side: start with polling

The common instinct is to reach for WebSockets because “real-time” sounds modern. I think that is usually premature. Ask how fresh the data genuinely needs to be. A dashboard that updates every 30 seconds satisfies most business users, and a poll every 30 seconds from 1,000 users is only about 33 requests per second, trivial for any server. A nightly report certainly does not need a persistent connection.

A rough guide, which you should bend to your situation:

  • Updates are occasional and a few seconds’ delay is fine: polling, simple and robust.
  • One server system must notify another when something rare but important happens: webhooks, backed by a reconciliation poll.
  • A person is watching, updates are frequent, and delay would be noticed: WebSockets or Server-Sent Events.

Whichever you choose, design for the failure case, not the happy one: a missed event, a duplicate event, a dropped connection. The approaches differ mainly in how they behave when the network misbehaves, and that is where your real work will be.

Figures and names in the examples are illustrative.

Leave a Reply

Trending