NAUTILUS / RUST source 3eb18933
Pages

Rust edition: this page follows the live DeepWiki structure but treats the current Rust crates as implementation authority. Non-Rust surfaces are identified at their boundary and are not presented as Rust APIs.

MessageBus and Communication Patterns

Relevant Rust source files

  • crates/common/src/msgbus/core.rs
  • crates/common/src/msgbus/typed_router.rs
  • crates/common/src/msgbus/typed_endpoints.rs
  • crates/common/src/msgbus/backing.rs
  • crates/infrastructure/src/redis/msgbus.rs

Overview

The MessageBus is the central communication backbone of NautilusTrader, providing a decoupled messaging layer for all system components including the DataEngine, ExecutionEngine, RiskEngine, Portfolio, strategies, and actors It supports high-performance, in-memory message passing with optional Redis backing for external streaming and persistence

The system utilizes three fundamental communication patterns :

  • Point-to-Point: Send messages to named endpoints via send_* functions.
  • Pub/sub: Publish messages to topics via publish_*, where subscribers receive all messages matching their pattern.
  • Request/Response: Register correlation IDs for response sequence tracking.

The Rust MessageBus provides in-process routing, typed endpoints, topic subscriptions, request/response correlation, and optional external ingress/egress backing.

MessageBus Architecture

The MessageBus supports typed hot paths and erased Any handlers where heterogeneous routing is required, while keeping endpoint and topic ownership explicit.

Core Routing Components

The Rust implementation at manages state using several specialized structures:

Component Role
TopicRouter<T> Optimized dispatch for specific data types (Quotes, Trades, Bars)
EndpointMap Registry for point-to-point command handlers
Subscription Container for a Pattern, MessageHandler, and priority
CorrelationIndex Maps UUID4 correlation IDs to return handlers for request/response

System Entity Mapping

The following diagram bridges the conceptual communication flow to the specific code entities implementing the bus.

Nautilus runtime graph centered on the MessageBus between strategies, data, risk, execution, portfolio, cache and selected adapters.

Typed topic routers carry hot market-data paths, endpoint maps deliver commands to named handlers, and the correlation index completes request/response lifecycles. All three remain in-process contracts even when an optional external backing mirrors selected messages.

Communication Patterns

Point-to-Point (Commands)

Point-to-point messaging delivers commands directly to a registered handler. Components register themselves at specific endpoints during the system initialization phase. For example, the ExecutionEngine registers endpoints to handle TradingCommand messages

Implementation Flow:

Publish/Subscribe (Events & Data)

The pub/sub pattern supports one-to-many broadcasting. Subscribers provide a Pattern which is matched against a message's Topic string

Topic Pattern Matching

NautilusTrader uses a greedy backtracking algorithm for pattern matching supporting:

  • *: Matches zero or more characters.
  • ?: Matches exactly one character.
Pattern Matches Does Not Match
data.quotes.* data.quotes.BINANCE.BTCUSDT data.trades.BINANCE.BTCUSDT
*.orders.??? strategy_1.orders.new strategy_1.orders.upd

Request/Response

Requests are tracked via a correlation_id (UUID4). When a component issues a response, the bus uses this ID to route the message back to the original requester's callback stored in the CorrelationIndex

Performance and Threading

To achieve maximum throughput and nanosecond-level precision, the bus utilizes Thread-Local Storage (TLS) Each thread in the system (e.g., the main engine thread) maintains its own bus instance and handler buffers.

  • Lock-free Dispatch: TLS ensures each thread gets its own MessageBus instance, eliminating synchronization overhead and the need for Send/Sync implementations
  • Typed vs. Any: The bus provides specialized "typed" paths for core market data (e.g., publish_quote). These paths avoid the overhead of runtime type checking and dyn Any downcasting, offering significantly higher throughput
  • Buffering: Handler buffers use SmallVec with an inline capacity of 64 (HANDLER_BUFFER_CAP) to minimize heap allocations during message bursts

External Streaming via Redis

In live trading contexts, the MessageBus can mirror selected messages to Redis Streams for distributed monitoring or integration. This egress is not the authoritative cache database or event store.

Implementation Mapping:

Persistence map distinguishing authoritative event-store capture and cache snapshots from optional Redis message-bus egress.

Core Message Categories

The bus is pre-configured with optimized paths for these core NautilusTrader types :

Category Specific Types
Market Data QuoteTick, TradeTick, Bar, OrderBookDeltas, OrderBookDepth10, OrderBook
Trading Events OrderEventAny, PositionEvent, AccountState, PortfolioSnapshot
Option Data OptionGreeks, OptionChainSlice, GreeksData
DeFi Data Block, Pool, PoolSwap, PoolLiquidityUpdate, PoolFeeCollect, PoolFlash