- Define where latency starts and ends before setting a target.
- Preserve sequence, recovery and schema semantics across protocol boundaries.
- Bound queues and separate market-data updates from order execution.
For engineers building market-data and transaction interfaces.
A binary message can be small and still arrive too late to be useful. Queueing, reconnect recovery and a slow client often matter more than the time spent serialising a value. In financial systems, correctness also depends on knowing which event was missed and whether a repeated request creates a duplicate action.
This guide describes an illustrative gateway that consumes a venue feed and serves authorised browser clients. It is an engineering design discussion, not investment advice or a claim of measured trading-system performance.
1. Define the latency boundary and the message meaning
Specify whether the target measures source-to-gateway, gateway-to-client or source-to-rendered-screen time. Those are different paths with different clocks. Record event timestamps, receive timestamps and processing milestones carefully; cross-machine comparisons need an understood clock synchronisation strategy.
Separate market-data events, order commands, acknowledgements and execution reports. They have different delivery and retention requirements. Dropping an obsolete price update may be acceptable for a display, while dropping an execution report can leave the application with an incorrect position.
Measure percentiles under realistic fanout and reconnect load. Average latency hides bursts and slow consumers. Include message size, subscription count and client location in the test conditions. A local process benchmark should never be presented as an end-to-end internet latency guarantee.
2. Keep WebSocket, FIX semantics and binary encoding distinct
WebSocket provides a bidirectional framed connection over TCP. It does not define business acknowledgements, account permissions or how an application recovers missed events. Those behaviours belong in the application protocol.
FIX is a family of financial messaging standards and practices. Simple Binary Encoding is a binary encoding standard from the FIX Trading Community; it should not be treated as a generic synonym for every FIX session or venue protocol. Confirm the exact specification and counterparty requirements before implementation.
At the gateway, validate the incoming schema and map fields deliberately. Preserve instrument identifiers, precision, units and event type. Represent monetary values with an agreed exact representation rather than casually converting everything to floating point. Keep the raw source reference needed to investigate a mapping error.
3. Design snapshots, sequence checks and reconnect recovery
Give each client a way to establish a consistent starting state and then receive ordered updates for the relevant stream. A snapshot without a sequence boundary can race with live updates, producing a view that never existed at the source.
Document how the client detects a gap and requests recovery. Decide when replay is available and when a fresh snapshot is required. Use the sequencing scope defined by the source rather than inventing a global order across unrelated feeds. Preserve enough history to meet the intended recovery window.
For an illustrative display protocol, the state machine might be:
CONNECT -> AUTHORISE -> SNAPSHOT -> LIVE
LIVE + sequence gap -> PAUSE APPLYING UPDATES
RECOVER -> replay from known boundary OR request new snapshotThis is a design sketch. A real implementation must define duplicate handling, snapshot consistency and the exact sequence rules of the upstream protocol.
4. Bound queues and handle slow consumers explicitly
Set limits on per-client buffered bytes, subscriptions and pending work. An unbounded queue turns a slow browser into a memory problem for the gateway. Decide what happens when the limit is reached: disconnect, resynchronise or coalesce updates where the business meaning allows it.
Coalescing may suit a latest-price display, but it must not silently remove transaction events that the consumer needs to reconstruct state. Keep separate channels or policies for these classes. Measure queue age as well as depth; a small queue of old events can still mislead a user.
Apply admission and rate controls before expensive processing. During reconnect storms, spread recovery requests and protect the upstream source from repeated full snapshots. Test a mix of fast and slow clients to confirm that one group does not degrade service for everyone else.
5. Protect account scope and consequential commands
Authorise subscriptions at the account and instrument scope required by the product and data licences. Validate origins and session state as appropriate, and expire access when permissions change. A successful socket handshake is not sufficient authorisation for every later message.
Keep order commands separate from display updates. A command needs a stable business identifier, validation and a clear acknowledgement model. If the client times out, it should query or reconcile the command result rather than blindly resend a potentially completed order.
Log enough information to trace a command through the gateway and external system, while limiting sensitive data exposure. Test revoked access, malformed messages, oversized payloads and replay attempts. The zero trust guide describes the broader resource-authorisation approach.
6. Benchmark recovery and correctness with latency
Create a reproducible load profile with normal traffic, bursts, sequence gaps and reconnect waves. Record hardware, runtime, schema version, fanout and network conditions. Measure dropped messages, recovery duration and state mismatches alongside latency and throughput.
Compare the client’s reconstructed state with a reference result after the run. Inject a slow consumer and an interrupted upstream connection. Verify that the documented backpressure and recovery policies actually occur, including the error states shown to users.
Publish performance figures only with their measurement boundary and conditions. Optimise the bottleneck shown by the trace, then rerun the same scenario. For replayable event storage and analytical consumers, continue with the data pipeline guide.
Common questions
Does binary encoding guarantee lower end-to-end latency?
No. It can reduce message size and processing overhead, but queueing, network distance, fanout and recovery may dominate the complete path. Measure them separately.
Can we discard old updates for slow clients?
Only when the message semantics allow it, such as a latest-value display with a resynchronisation path. Transaction and execution events generally need different handling.
Is a reconnect enough to restore the client state?
No. The client needs a consistent snapshot or replay boundary and rules for gaps and duplicates. A restored transport alone does not restore business state.
Sources & further reading
Primary documentation and research used for the technical context in this guide. Examples and checklists are illustrative; validate them against your system and the versions you deploy.
