All writing
Engineering

Shipping in real-time: lessons from a year on a trading platform

I work on a real-time trading platform. Most days it works. Some days it doesn't, and those days teach you more than the good ones. Here's what running live market data for a year has shown me.

The 9:30 AM problem

Markets in Nepal open at 11:00 AM. By 10:45, our connections start warming up. By 11:00:00, a hundred WebSocket clients want to know what just printed. Everything works on a Wednesday at noon. The interesting questions are: what happens at 11:00:00 sharp on a Monday after a weekend of news?

Three things matter at that exact moment:

  • Backpressure. The upstream feed bursts. Clients can't drain that fast. Without something between them, the slow ones lock up the fast ones.
  • Reconnects. When the upstream blips, every client notices within a second. Restarting them in a thundering herd is how you turn one outage into two.
  • Stale state. Reconnects need a snapshot. Snapshots drift if you don't version them. Drift looks like phantom trades.

The shape of the fix

We sit a NestJS gateway between the upstream feed and the browser. It does three things, none of them clever on their own — but together they keep things calm at the open.

1. Drop, don't queue

For tick data, last-write-wins. If a client is 200ms behind, the price from 200ms ago is just noise. The gateway holds a small ring buffer per client and overwrites entries when they fill. Slow consumers never feel the burst — they just see the latest.

// Pseudocode of the per-client buffer
class ClientBuffer {
  private latest: Map<string, Tick> = new Map();

  push(t: Tick) {
    // Symbol becomes the key — newer ticks overwrite older.
    this.latest.set(t.symbol, t);
  }

  drain(): Tick[] {
    const out = [...this.latest.values()];
    this.latest.clear();
    return out;
  }
}

2. Stagger the reconnect storm

When the gateway restarts, every client comes back at once. We add per-client jitter to the reconnect delay — 0 to 800ms — so the wave becomes a trickle. It's one of those changes that looks pointless until you watch the CPU graph at 11:00:00 with and without it.

3. Version the snapshot

Every tick has a sequence number. When a client reconnects, it sends its last seq. The gateway either returns the diff since that seq, or — if the gap is too wide — sends a full snapshot with a new base seq. The client knows which one it got and reconciles.

What I'd tell past me

Real-time isn't about going fast — it's about going predictably. The fast path is easy. The slow path, the failure path, and the catch-up path are where reliability hides.

If you're building anything that streams, write the backpressure-and-reconnect story first, even on a napkin. Don't wait for it to break in production at 11:00:00 on a Monday.

The fast path is easy. The slow path, the failure path, and the catch-up path are where reliability hides.

I'll write more about the snapshot-versioning bit specifically — there's a whole class of bugs hiding in that handshake that took me a month to track down.