Skip to content

Latest commit

 

History

26 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Powered by .NET C# LMAX Architecture High Performance Asynchronous License: MIT GitHub stars

LMAX-TradingSystem

Overview

This project simulates a simplified Trading System based on the concepts used in the **LMAX Disruptor** pattern. The system processes OrderPlacedEvent and applies Business Logic to simulate trades. It leverages Event Sourcing to store events for replay in case of a failure and implements a Failover Mechanism to recover from crashes by switching between multiple processors.

For more information about the concept, read this article:

https://martinfowler.com/articles/lmax.html

Key Features

  • Disruptor Pattern: Uses a ring buffer pattern to process high-throughput events efficiently.
  • Event Sourcing: Stores every event, enabling the system to rebuild its state by replaying past events.
  • Snapshotting: Periodically takes a snapshot of the processor's state to speed up recovery.
  • Failover Mechanism: Simulates failover between multiple processors, allowing continued operation in the event of a crash.
  • Diagnostics: Supports replaying events for debugging and business diagnostics.

Getting Started

Prerequisites

  • .NET 8.0 or higher
  • Visual Studio or any C# IDE
  • NuGet package Disruptor (restored automatically via dotnet restore)

Setup

  1. Clone the repository.
  2. Restore the dependencies:
dotnet restore
  1. Build and run the application:
dotnet run --project LMAX-TradingSystem

How It Works

Event Sourcing

OrderPlacedEvent: Represents a customer placing an order (e.g., stock purchase). EventStore: Stores these events in a list (or a persistent store) to allow replaying events in case of failure. Each event has a Timestamp indicating when it was created.

Business Logic Processor

The BusinessLogicProcessor processes the OrderPlacedEvent to generate a TradeExecutedEvent. Each processed event is stored in the EventStore for future replay and is also used to update the system state, which can later be saved in a snapshot.

Snapshots

The system periodically takes snapshots of the current state (e.g., account balances). Snapshots are used to restore the system's state quickly after a crash.

Failover Mechanism

ReplicatedProcessor:

This class manages multiple instances of BusinessLogicProcessor. If the primary processor crashes, the system fails over to a backup processor, which restores the state from the last snapshot and replays the events that occurred after the snapshot.

Project Structure

  • LMAX-TradingSystem/BusinessLogic: Core business logic processing.
  • LMAX-TradingSystem/Domain: Domain events (e.g., OrderPlacedEvent).
  • LMAX-TradingSystem/EventSourcing: Event store and snapshotting.
  • LMAX-TradingSystem/Failover: Replicated processor / failover handling.
  • LMAX-TradingSystem/Diagnostics: Diagnostic event replay.
  • LMAX-TradingSystem/DisruptorSetup: Disruptor ring buffer configuration.

Contributing

If you'd like to contribute to this project, please feel free to submit a pull request or open an issue.

Support

You can support this repository by your star ⭐

About

Implement a small project with LMAX to show the concepts

Resources

Stars

15 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages