Video in progress

Order status is currently fetched by polling every 5 seconds. How do you migrate it to WebSocket safely, step by step?

I add WebSocket alongside polling without turning it off: first the push only gives an "updated" signal and the client fetches the data from the same REST endpoint. I raise the share gradually with a feature flag, stretch the polling interval for clients connected over WebSocket, and once I'm confident, polling stays only as a fallback.

📖 Answer

The biggest mistake is "let's move everything to WebSocket over one weekend". Polling is simple and reliable, and you shouldn't lose that reliability all at once.

I split it into stages like this:

1. Push is only a signal. The server sends {"type":"order-changed","id":42,"version":7}. The client calls the old GET /orders/42. The source of data doesn't change, and authorization stays in REST. A lost WebSocket message is not a disaster: the next poll catches it anyway.

2. Run side by side. A client connected over WebSocket stretches its polling interval from 5 s to 60 s. If the connection drops, it goes back to 5 s immediately. That way the server load drops gradually, and a WebSocket problem isn't visible to the user.

  1. Feature flag and share. First internal staff, then 5%, 25%, 100%. At each stage I compare: the time from "the change happened" to "it appears on screen", the number of disconnects, and server resources.

  2. Add the data to the push. Once there's confidence, the state itself is added to the payload and REST calls decrease. Thanks to the version field, an old message doesn't overwrite a newer one.

  3. Polling stays as a fallback. For old mobile versions and corporate networks that cut WebSocket. I'm in no hurry to remove it.

Preparation is needed on the infrastructure side too: upgrade and timeouts on proxies, the load balancer, capacity for the number of connections, reconnects during deploys. It's cheap to try these in stage 1, while traffic is small.

The result has to be measurable: for example, polling requests dropped 80%, and the state reaches the screen in 300 ms instead of 5 s. Without those numbers the migration is just an "interesting technology".