ExchangeCore is a high-throughput, ultra-low latency order matching engine constructed in Java 21. Designed to simulate the core exchange infrastructure of a Tier-1 financial exchange, it accepts buy and sell orders, matches them utilizing a strict Price-Time Priority (FIFO) model, and broadcasts real-time market data to downstream consumers.
The system is architecturally derived from the LMAX Architecture, which guarantees deterministic, single-threaded execution at microsecond-level latency without GC pressure, thread-lock overhead, or context switching logic.
- Ultra-low Latency: Target of < 1 ms p99 processing time.
- High Throughput: Capable of processing 100,000+ orders per second efficiently.
- Zero Allocation on Critical Path: Relies on Zero-GC object pooling and primitive data collections.
- Deterministic Execution: Order replay yields 100% reproducible outcomes.
- Fast Recovery: Can completely reconstruct the L2 Order Book in under 10 seconds via sequential Write-Ahead Logging.
The operational flow of ExchangeCore follows a stringent series of ring buffers, ensuring zero locks are required across threads.
Incoming OrderCommand requests (LIMIT, MARKET, CANCEL) arrive via an HTTP Gateway. Basic schema and API Key validation occur here before the system funnels valid requests downstream.
Before an order enters the primary ring buffer, it passes through the Sequencer. This highly optimized, single-threaded component attaches a monotonically increasing, globally unique sequence number to every order. This sequence forms the absolute source of truth governing execution.
While the system advances, a dedicated Journal Writer thread concurrently captures ExchangeEvent data and serializes it asynchronously to an append-only flat binary file (MappedByteBuffer). By performing this asynchronously via multicast routing on the lock-free ring buffer:
- The execution thread is not delayed waiting for I/O.
- The system achieves complete disaster recovery transparency. Every 1,000,000 (configurable) commands, a Snapshot Manager captures a full state snapshot to optimize replay times.
As the engine's core processor, this single-threaded event handler fetches orders chronologically from the buffer and resolves:
- Balance Verification: Confirms sufficient accessible funds prior to executing new entries.
- Order Matching: Processes LIMIT and MARKET orders directly against resting bids and asks under Priority rules, discarding the need to acquire mutex locks.
- Event Dispatch: Triggers
TradeEventandBookUpdateEventon successful fills or cancellations.
Outbound results enter a separate event ring buffer where a secondary Event Publisher thread manages the fan-out dissemination of real-time trading data to endpoints like WebSockets or data streams.
- Languages / Platform: Java 21 LTS
- Concurrency primitives: LMAX Disruptor (For Lock-free Ring-Buffer Architecture)
- High-Performance Memory Mapped I/O: Chronicle Bytes
- Ring Buffers / Primitives: Agrona
- HTTP/Gateway: Javalin
- Build Tool: Gradle
exchangecore.domain─ Core domain entities (Order,TradeEvent).exchangecore.pool─ Zero-allocation RingBuffer Object Pools.exchangecore.engine─ Core Execution Logic (MatchingEngine,OrderBook,BalanceManager).exchangecore.disruptor─ Concurrent topologies encompassing Sequencers and Handlers.exchangecore.journal─ Binary WAL codecs and state recovery utilities.exchangecore.api─ Outbound and validation APIs.
Build the application:
./gradlew buildRun Tests:
./gradlew testBenchmarking Setup / Profiling:
./gradlew run