Skip to content

Imran8125/ExchangeCore

Repository files navigation

ExchangeCore — High-Frequency Order Matching Engine

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.

Key Features

  • 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.

Architecture & Logical Process

The operational flow of ExchangeCore follows a stringent series of ring buffers, ensuring zero locks are required across threads.

1. Ingestion & API Gateway

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.

2. The Sequencer

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.

3. Journaling & Snapshotting

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.

4. Exchange Core (Matching Engine)

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 TradeEvent and BookUpdateEvent on successful fills or cancellations.

5. Event Publication

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.


Tech Stack

  • 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

Modules Directory

  • 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.

Building & Running

Build the application:

./gradlew build

Run Tests:

./gradlew test

Benchmarking Setup / Profiling:

./gradlew run

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors