A low-latency, multithreaded trading execution engine built with Spring Boot 3. This system simulates a High-Frequency Trading (HFT) environment, utilizing pessimistic locking and asynchronous processing to ensure strict ACID compliance and thread safety under intense concurrency.
- Concurrency Control: Implements
PESSIMISTIC_WRITEdatabase locking to serialize concurrent ledger updates and prevent race conditions. - Asynchronous Processing: Decouples order ingestion from execution using
CompletableFutureand non-blocking I/O. - Numerical Precision: Uses
BigDecimalexclusively for financial arithmetic to avoid floating-point rounding errors.
Build & Run
-
Build:
mvn clean install -
Start the application:
mvn spring-boot:run. The server will start on:http://localhost:8080
- Once the application is running, open your browser and go to:
http://localhost:8080/swagger-ui/index.htmlto explore all endpoints
POST /api/v1/trading/execute
{
"userId": 1, // integer - ID of the user placing the order
"symbol": "BTC", // string - Trading pair symbol (e.g. BTC, ETH, AAPL)
"payWith": "USD", // string - Quote currency (e.g. USD, USDT, EUR)
"quantity": 0.05 // number - Quantity to buy in base asset
}GET /api/v1/trading/portfolio/{userId}
A bash script is provided to verify the exactly-once execution logic:
chmod +x test_idempotency.sh
`````./test_idempotency.sh```
``
This script verifies that the engine processes a specific trade exactly once by simulating a network retry and confirming that the user's balance is only deducted once, despite receiving multiple identical requests.