Skip to content

ManikMittal-git/Matching_engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Low-Latency TCP Matching Engine

A high-frequency stock exchange and trade matching engine written in modern C++(20), capable of processing 10.6+ million orders per second with a blazing fast tick-to-trade latency of ~94 nanoseconds.

This project serves as a systems engineering sandbox to explore cache locality, zero-allocation memory architectures, and POSIX network programming.


Architecture & Core Features

  • $O(1)$ Time Complexity: Utilizes an Intrusive Doubly Linked List paired with a Red-Black Tree (std::map) to guarantee $O(1)$ constant time for order insertions, executions, and cancellations.
  • Zero-Allocation Hot Path: Completely eliminates dynamic heap allocation (new/delete) during active trading by utilizing pre-allocated continuous Memory Pools.
  • Cache Locality: Designed to keep order data contiguous in memory, maximizing CPU L1/L2 cache hit rates and eliminating pointer-chasing overhead.
  • Custom TCP Server: Bypasses heavy web frameworks by dropping down to POSIX C socket APIs to take live binary/text order flow directly from the Linux kernel network stack.
  • Price-Time Priority: Strictly enforces FIFO matching logic, preventing crossed books and accurately processing partial fills.

Performance Benchmarks

The core engine was stress-tested on an isolated hot loop (bypassing the network layer) to measure pure algorithmic and data-structure efficiency.

  • Total Orders Processed: 1,000,000
  • Total Processing Time: ~94 milliseconds
  • Tick-to-Trade Latency: ~94.9 nanoseconds / order
  • Maximum Throughput: ~10.6 Million orders / second

Note: The TCP network layer was independently stress-tested using a Python Market Maker bot, successfully handling a localized TCP flood of 12,000+ orders/second with zero packet loss.


Build and Run Instructions

Prerequisites

  • CMake (v3.10+)
  • GCC / G++ (with C++17 support)
  • Linux / WSL environment

Compilation

mkdir build
cd build
cmake ..
make

Execution Modes

1. Live TCP Server Mode Starts the exchange and listens for incoming connections on Port 8080.

./engine

(You can connect and trade manually using nc localhost 8080 or run python3 bot.py for the network stress test)

2. Core Engine Benchmark Mode Bypasses the network to test the sub-microsecond latency of the data structures.

./engine --benchmark

Development Journey

Day 1 Progress:

  1. Started building my matching_engine project (initialized some folders in directory).
  2. First wrote CMakeLists.txt (documentation helped to understand some commands).
  3. Wrote order.h first to easily access the feature of orders like prices,id etc.
  4. Did a test run on main.cpp by checking the features.
  5. Wrote book.h to check the .next and .prev feature, appending new orders to process .next and .prev.
  6. Did a test run on main.cpp by initializing some random orders and appending them to each other.

Day 2 Progress:

  1. Added remove_order function and introduced a new general process_order function, which essentially takes new orders, matches them against current resting orders, and does suitable trades.
  2. Did a test run on main.cpp by sending new trades and matching them.
  3. Added few other things in book.h, which stores the left out orders back to the orderbook.
  4. Also added a visual printbook to see the current situation of orderbook.
  5. Did a test run by taking extra orders and checking the orderbook before and after trade in main.cpp.

Day 3 Progress:

  1. Learnt about TCP server, and socket APIs for linux.
  2. Started implementing server.h which was responsible for accepting connections on local host and sending/receiving messages from the client.
  3. Changed main.cpp to take commands from the client manually and place orders accordingly.
  4. Built a python file bot.py to stress test any packet leak among 10k orders (randomly generated) sent by server, showing the complete list of orderbook.
  5. Finally built another function in main.cpp to run benchmark test to calculate the actual latency and throughput for 1 million orders.

Possible updates

  • Flat Array Order Book: Replace the std::map (Red-Black tree) with a contiguous flat array for active price levels to achieve absolute maximum CPU cache locality.
  • Asynchronous I/O: Upgrade the POSIX socket server to use epoll or io_uring to handle thousands of concurrent client connections efficiently without blocking.

About

A high-frequency trade matching engine in modern C++ achieving 10.6M+ orders/sec and sub-100ns latency using zero-allocation memory pools and POSIX sockets.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors