Moving 5,000 Vehicles on One Map with WebSocket

The demo disaster happened in early February, in the meeting room of our biggest prospective customer. Our sales manager opened the live map screen; he was going to show their 4,800-vehicle fleet on a single screen. The page loaded, the vehicles appeared, and 20 seconds later the tab froze. While the browser was showing its "page unresponsive" dialog, the sales manager was texting me: "URGENT. We froze." The problem wasn't even on the server; we were pushing every vehicle update as a separate WebSocket message, and the browser was choking on the main thread trying to parse 500 small messages per second and move 4,800 markers one by one.

The first lesson I learned that day: "moving 5,000 vehicles on a map" isn't a server problem; it's an end-to-end budget problem. A bandwidth budget, a message count budget, a browser main thread budget. If you don't manage all three separately, you sink in all three at once.

Breaking Away from Polling and the First Naive Version

Before we moved to WebSocket, the system did AJAX polling every 10 seconds; each time it fetched the last known location of the entire fleet, 900 KB of JSON on average. With 200 open screens, that meant more than 1 GB of repetitive data per minute from the server, and 95 percent of the data was identical to the previous response. That was exactly the promise of WebSocket: keep the connection open and send only what changes.

We wrote the naive version quickly: the moment a device message came out of the processing pipeline, JSON was pushed to every socket subscribed to that vehicle. It was wonderful for small fleets. The disaster blew up with the big customer watching thousands of vehicles on one screen, because we had never budgeted the number of messages. 500 messages per second × 200 bytes is innocent in terms of bandwidth (100 KB/s), but in the browser it means 500 onmessage calls, 500 JSON.parse calls and 500 DOM/canvas touches per second. In the Chrome profiler, 80 percent of the main thread was in our marker update function.

Polling sends the whole fleet every round, while WebSocket carries only what changed; but if the message count isn't budgeted, the browser freezes.
In the naive WebSocket version, 500 messages per second choked the browser.

One Breath per Second: The Batched Delta Frame

The backbone of the solution was tying updates to a tick model. On the server side, we kept an output buffer for each screen session; vehicle updates are written to this buffer rather than straight to the socket. Once per second the buffer is flushed: all vehicles that changed during that second go out in a single batched frame. If the same vehicle was updated twice within a second, only the last update is kept (conflation: in telemetry the latest value always overrides the previous one, so there's no point in building up a queue).

Vehicle updates accumulate in the session buffer and are sent once per second as a single batched delta frame, keeping only the latest value for each vehicle.
Thanks to conflation, there's no need to build up a queue.

The contents of the frame are a delta too: a vehicle's unchanged fields aren't sent. If the location changed, the location goes out; if only the heading changed, only the heading. We also carried field names not as JSON keys but as short, pre-agreed codes; compared with full JSON, the average message size per vehicle dropped from 210 bytes to 46 bytes. A binary format (we tried MessagePack at the time) gained another 20 percent, but it killed debuggability, so we stayed with compact JSON. I don't regret it; every debugging hour not spent in production is worth more than the bandwidth saved.

Not Sending Vehicles Nobody Can See

The second big win was viewport subscriptions. When a user has zoomed in on Istanbul, there's no point in them receiving updates for 800 vehicles in Ankara. When the map stops moving (with a 300 ms debounce), the client reports the bounds of the visible area to the server; the server keeps that session's subscription as a set of geohash cells. Yes, the same geohash from my spatial query post became a broadcast filter here. When a vehicle changes cells, the subscription sets are updated. A user who zooms out to view all of Türkiye receives not raw vehicles but per-cell count clusters (cluster summaries); at that zoom level, 5,000 separate markers have no visual meaning anyway.

With these two changes, the traffic a typical operations screen receives went from 500 messages per second to 1 message (a single frame with 40-120 vehicle deltas on average), and bandwidth usage per session went from 100 KB/s to 4-6 KB/s. On the browser side, with a single parse per frame plus batched drawing inside requestAnimationFrame, main thread usage dropped from 80 percent to 7 percent even on the demo machine. Three weeks later we gave the same demo to the same customer; this time the one who froze was me, unable to keep up with the sales manager's excitement.

The Server Side: 200 Screens Is Easy, 2,000 Is Not

On the fan-out side, PHP-FPM wasn't suited for this job (request-response model, can't hold persistent connections); we wrote the WebSocket layer as a separate process. Back then we started with Ratchet, and when its event loop wasn't enough, we moved to Swoole. A single process carried 2,000 concurrent screen connections with an average fan-out time of 3-4 ms per tick. The critical trick is serializing the frame once for sessions with the same subscription set: if 50 sessions are watching central Istanbul, JSON encoding runs once, not 50 times, and the same string is written to all the sockets. Serialization was the biggest CPU cost in fan-out; this deduplication decoupled it directly from the number of sessions.

Of course, we also went through the troubles that come with persistent connections. For customers behind corporate proxies, the WebSocket handshake was sometimes blocked; a client that can't connect within 5 seconds automatically falls back to the old polling mode. (That code path is still there today, and 6 percent of traffic uses it. Graceful degradation matters more than cool technology.) Then there's the silent disconnect: when a laptop on a mobile network goes to sleep and wakes up, the socket can look "open" and be dead. We put in an application-level rule of a 25-second ping-pong and reconnecting after two missed pongs; a reconnecting client reports the last tick number it received, and the server closes the gap with a full state frame.

Before putting this layer into production, we tested it with an army of fake clients: 10,000 virtual screens written in Node, each subscribed to a random viewport. In the first load test, the process hit the file descriptor limit at 6,200 connections (guess who forgot about ulimit). In the second, the tick time climbed to 40 ms at around 8,000; profiling showed most of the time was spent comparing subscription sets, and converting the cell sets to bitmaps fixed it. Every problem a load test finds is a problem you don't find in the demo room; after that February day, that sentence became a slogan on our wall.

Backpressure was the final boss. When a slow client's socket buffer fills up (a tablet on 3G, say), queuing up the frames accumulating on the server for that session is a poison that slowly eats away at memory. Our rule: if more than 3 ticks pile up in a session's send buffer, the intermediate frames are dropped and the next send becomes a full state frame. This is where location data is beautiful: losing intermediate values is recoverable, and the current state tells the whole story. If we were carrying financial tick data, we wouldn't have that luxury.

The WebSocket server applies serialization deduplication, fallback to polling, ping-pong reconnection and backpressure rules.
With location data, even if intermediate values are lost, the current state is enough.

Putting an incrementing tick number on the frames turned out to have an unexpected benefit in diagnostics. When a customer says "the map is stuttering," we pull the arrival times of the last 100 tick numbers from the client: if the numbers are consecutive but the intervals are irregular, the problem is in the network; if the numbers skip, the problem is in our backpressure drops; if both look normal, the problem is on the client machine. A single counter lets us tell three suspects apart within a minute.

Today the system carries 5,000 vehicles and 1,400 concurrent screens on two mid-sized virtual machines, with observable latency under 1.5 seconds. If you're racking your brain over real-time maps, fan-out or delta protocol design, write to us here; from tick rate to frame schema, we've built up plenty to discuss.