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
- 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.
- .NET 8.0 or higher
- Visual Studio or any C# IDE
- NuGet package Disruptor (restored automatically via
dotnet restore)
- Clone the repository.
- Restore the dependencies:
dotnet restore- Build and run the application:
dotnet run --project LMAX-TradingSystemOrderPlacedEvent: 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.
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.
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.
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.
- 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.
If you'd like to contribute to this project, please feel free to submit a pull request or open an issue.
You can support this repository by your star ⭐