Message Queues
Message queues pass market data, signals and orders between trading components. Learn pub sub and queues, tools like Kafka, Redis and ZeroMQ, and design trade offs.
As a trading system grows beyond one program, its parts need to talk: feed handlers publish prices, strategies consume them and publish orders, risk services check those orders and loggers record everything. Message queues and messaging systems carry these events between components. They decouple producers from consumers, absorb bursts and allow parts of the system to be restarted or scaled independently. The right choice depends on whether you need speed, durability or both.
Messaging patterns#
| Pattern | How it works | Trading example |
|---|---|---|
| Publish and subscribe | Producers publish to topics; every subscriber gets a copy | Price updates sent to several strategies |
| Work queue | Each message goes to one of several workers | Distributing backtest jobs |
| Request and reply | A request gets a direct response | Asking a risk service to approve an order |
| Event log | Messages stored in order and can be replayed | Recording all events for audit and replay. See Market Data Replay |
Common tools#
| Tool | Character | Typical trading use |
|---|---|---|
| Apache Kafka | Durable, high throughput event log; consumers can replay history | Data pipelines, audit streams, analytics |
| Redis (pub sub and Streams) | In memory, simple, fast | Live prices to dashboards, light messaging. See Redis and PostgreSQL for Trading |
| RabbitMQ | Flexible routing, reliable delivery | Job queues, back office workflows |
| ZeroMQ | A library for direct, brokerless messaging with low latency | Fast links between processes |
| NATS | Lightweight, fast pub sub | Service messaging |
| Shared memory ring buffers | Custom, lowest latency | Inside latency critical trading engines. See Lock-Free Programming and Ring Buffers |
Durability versus latency#
| Need | Choose |
|---|---|
| Every message stored and replayable | Kafka or Redis Streams with persistence |
| Lowest latency between processes on one machine | Shared memory or ZeroMQ over interprocess transport |
| Simple fan out of live prices | Redis pub sub or NATS |
| Reliable job distribution | RabbitMQ or a work queue |
Durability costs time: writing to disk and waiting for acknowledgements adds latency. Many systems use a fast path for trading and a separate durable path for recording.
Backpressure#
When consumers fall behind, something must give: messages queue up, get dropped or slow the producer. For market data, the latest price usually matters more than every intermediate one, so conflation (keeping only the newest update per symbol) is common for slow consumers. For orders and fills, nothing may be dropped. Decide this explicitly for each stream.
Ordering and duplicates#
- Ordering: many systems guarantee order only within a partition or topic; route all events for one instrument or account to the same partition.
- Duplicates: at least once delivery means a message can arrive twice; consumers should be idempotent. See Alerts, Error Handling and Reconnection and Sequence Numbers, Dropped Packets and Out-of-Order Messages.
Do you need a message queue?#
A single bot in one process can pass events through an in memory queue with no external system. Introduce messaging when you have several processes, need to record streams durably or want to scale parts independently. See Building Trading Bots.
Frequently asked questions#
What is a message queue in trading?#
A system that passes events such as prices, signals and orders between components, decoupling producers from consumers.
Is Kafka good for trading?#
Kafka is excellent for durable, replayable data pipelines and audit streams, but it is not usually used on the lowest latency trading path.
What is backpressure?#
What happens when consumers cannot keep up with producers; systems must queue, drop, conflate or slow messages, and the right choice depends on the stream.
Next, learn how Redis and PostgreSQL fit into trading setups in Redis and PostgreSQL for Trading.
3 quick questions on this lesson. Get them all right to finish it.
Turn on JavaScript to take the quiz.
Mentioned in
- Trading Infrastructure ExplainedTrading Infrastructure
- Docker and KubernetesTrading Infrastructure
- WebSocket Market Data StreamsProgramming and Data
- Data Pipelines and ETLProgramming and Data
- Order Book Feeds: Snapshots and Incremental UpdatesProgramming and Data