# Sequence Numbers, Dropped Packets and Out-of-Order Messages

> Sequence numbers let trading systems detect lost, duplicated or out of order messages. Learn how gap detection, recovery and duplicate handling work in practice.

Source: https://learn.tradelabsai.com/programming/sequence-numbers/  
Track: Programming and Data · Level: Advanced · Updated: 2026-10-03  
Publisher: TradeLabs AI (https://tradelabsai.com). Education, not financial advice.  
Cite as: TradeLabs Learn, "Sequence Numbers, Dropped Packets and Out-of-Order Messages", https://learn.tradelabsai.com/programming/sequence-numbers/

When a venue sends a stream of messages, each one usually carries a sequence number that increases by one with every message. That simple counter is one of the most important reliability tools in trading technology. If message 1,001 is followed by 1,004, two messages are missing. If 1,003 arrives twice, one is a duplicate. If 1,005 arrives before 1,004, they are out of order. Without sequence numbers, a system cannot know whether its view of the market or its own orders is complete.

## What sequence numbers detect

| Pattern | Meaning | Response |
|---|---|---|
| 100, 101, 102 | Normal | Process |
| 100, 101, 104 | Gap: 102 and 103 missing | Recover the missing messages or rebuild state |
| 100, 101, 101 | Duplicate | Ignore the repeat |
| 100, 102, 101 | Out of order | Buffer briefly and reorder, or treat as a gap |
| 5,000 then 1 | Reset | Often a new session or a venue restart; resynchronise |

## Where sequence numbers appear

| Context | Example | Lesson |
|---|---|---|
| Exchange market data feeds | Per channel sequence numbers on multicast feeds | [Feed Handlers and Normalization](https://learn.tradelabsai.com/programming/feed-handlers-and-normalization/) |
| Crypto WebSocket books | Update IDs linking each delta to the previous one | [Order Book Feeds: Snapshots and Incremental Updates](https://learn.tradelabsai.com/programming/order-book-feeds/) |
| FIX sessions | MsgSeqNum on every message, with resend requests | [FIX Protocol](https://learn.tradelabsai.com/programming/fix-protocol/) |
| Your own systems | Event IDs in logs and message queues | [Logging, Audit Trails and Incident Response](https://learn.tradelabsai.com/algo-trading/audit-trails/) |

## Recovering from gaps

| Method | How it works |
|---|---|
| Retransmission request | Ask the venue to resend specific messages; common in FIX and some exchange feeds |
| Snapshot and resync | Request a full current state, then continue with live messages |
| A/B feed arbitration | Many exchanges send the same data on two redundant lines; take each message from whichever arrives first |
| Replay from recordings | Fill gaps from a recorded copy for research data |

**Example: A/B feed arbitration**
An exchange publishes identical market data on feed A and feed B through different network paths. A feed handler listens to both. Message 2,501 arrives on A first, so it is processed and the copy from B is discarded as a duplicate. Message 2,502 is lost on A because of a dropped packet but arrives on B, so the handler takes it from B. Only if a message is missing on both lines does the handler need to request a retransmission or a snapshot. This is why sequence numbers make redundancy simple: duplicates are recognised instantly.

## Sequence numbers for your own orders

Your systems should also number their events. Combined with unique client order IDs, this lets you detect whether every order acknowledgement and fill was received, which matters when reconnecting after a dropped session. See [Alerts, Error Handling and Reconnection](https://learn.tradelabsai.com/algo-trading/error-handling/) and [Trade Accounting and Reconciliation](https://learn.tradelabsai.com/industry/trade-reconciliation/).

## Implementation tips

- **Track expected next number per channel,** not globally, since venues number channels separately.
- **Set a short reorder window** for out of order delivery, then treat missing messages as a gap.
- **Stop using affected state** (such as an order book) until recovery completes.
- **Log every gap** with its size and time, and alert if gaps become frequent. See [Monitoring Positions, P&L and Risk](https://learn.tradelabsai.com/algo-trading/live-monitoring/).
- **Handle resets** at session start or after venue restarts.

## Why gaps happen

Network packet loss, slow consumers that fall behind and drop messages, venue failovers and your own process restarts. Frequent gaps usually point to a capacity problem on your side, such as a receive buffer that is too small or processing that is too slow. See [Networking for Traders](https://learn.tradelabsai.com/infrastructure/networking-for-traders/).

## Frequently asked questions

### What is a sequence number in trading data?

A counter on each message that increases by one, letting the receiver detect missing, duplicated or out of order messages.

### What should I do if my feed has a gap?

Stop trusting the affected state, recover the missing messages or request a fresh snapshot, then resume.

### What is A/B feed arbitration?

Receiving the same exchange feed on two redundant lines and taking each sequence numbered message from whichever line delivers it first.

Next, learn how to measure delays in your system in [Exchange vs Receive Timestamps and Latency Measurement](https://learn.tradelabsai.com/programming/latency-measurement/).

## Continue learning

- Next lesson: [Exchange vs Receive Timestamps and Latency Measurement](https://learn.tradelabsai.com/programming/latency-measurement/)
- Previous lesson: [Feed Handlers and Normalization](https://learn.tradelabsai.com/programming/feed-handlers-and-normalization/)
- Related: [Feed Handlers and Normalization](https://learn.tradelabsai.com/programming/feed-handlers-and-normalization/): Feed handlers connect to exchange data feeds and convert each venue's format into one standard internal format. Learn the design, symbol mapping and common pitfalls.
- Related: [Order Book Feeds: Snapshots and Incremental Updates](https://learn.tradelabsai.com/programming/order-book-feeds/): How exchanges publish order book data as snapshots and incremental updates, and how to build and maintain an accurate local order book in code without errors.
- Related: [FIX Protocol](https://learn.tradelabsai.com/programming/fix-protocol/): FIX is the standard messaging protocol for institutional trading. Learn how FIX sessions, messages and tags work, a sample order message and when traders use FIX.
- Related: [WebSocket Market Data Streams](https://learn.tradelabsai.com/programming/websocket-market-data-streams/): How WebSocket streams deliver live trades, quotes and order book updates. Learn subscriptions, heartbeats, reconnecting safely and handling gaps in Python.
- Related: [Alerts, Error Handling and Reconnection](https://learn.tradelabsai.com/algo-trading/error-handling/): Trading systems face rejected orders, disconnects, bad data and partial fills. Learn how to classify errors, retry safely, use idempotent orders and fail closed.
