A microservices-based trading platform built with Node.js, featuring real-time trade execution through Kafka message streaming and dual-layer state management. 1
The platform uses a microservices architecture with three core components:
- Backend Service (Express.js on port 5000): HTTP API gateway handling authentication, request orchestration, and database operations
- Engine Service (Node.js): Isolated trade processing with in-memory state for low-latency execution
- Message Broker (Apache Kafka): Asynchronous inter-service communication with correlation IDs for request-response patterns 2
- Runtime: Node.js with TypeScript
- Backend Framework: Express.js
- Message Queue: Apache Kafka
- Database: TimescaleDB (PostgreSQL)
- Cache: Redis (for sessions and magic links)
- Authentication: JWT with magic link signup 3
- Open/close trades with leverage support
- Real-time price updates from external poller
- Automatic liquidation price calculation
- In-memory order book for fast execution 4
- Passwordless magic link authentication
- JWT-based session management
- Protected trade and balance endpoints 5
- In-Memory State (Engine):
latestPrice,openOrders,userBalancefor sub-millisecond access - Persistent Storage (TimescaleDB): User accounts, asset definitions, trade history 6
| Endpoint | Method | Auth | Description |
|---|---|---|---|
/api/v1/signup |
POST | None | Generate magic link |
/api/v1/signin/post |
GET | None | Validate magic link |
/api/v1/trade/create |
POST | JWT | Open new trade |
/api/v1/trade/close |
POST | JWT | Close existing trade |
/api/v1/balance/usd |
GET | JWT | Query USD balance |
/api/v1/balance/ |
GET | JWT | Query total balance |
The system implements a synchronous request-response pattern over Kafka using correlation IDs:
- Backend generates unique ID and publishes command to
trade-pricetopic - Engine consumes message, executes business logic, publishes result to
acknowledgetopic with same ID - Backend consumer matches ID and resolves the waiting promise (5-second timeout) 7
The engine routes messages based on data.task field:
createUser: Initialize user with 5000 USD balancecreate: Open trade (validate balance, calculate liquidation)close: Close trade (calculate PnL, update balance)balance/usdBalance: Return current balance
Price updates from external poller (identified by from: "poller") update the latestPrice object without acknowledgment. 8
- Node.js
- Kafka broker
- TimescaleDB instance
- Redis server
KAFKA_BROKER=<kafka-broker-url>
TIMESCALE_CONNECTION_STRING=<postgres-connection-string>
Backend:
cd backend
npm install
npm startEngine:
cd engine
npm install
npm startThe platform currently supports three trading pairs (hardcoded on initialization):
- SOL_USDC (Solana)
- ETH_USDC (Ethereum)
- BTC_USDC (Bitcoin) 9
This project is open source and welcomes contributions from developers, students, and enthusiasts interested in trading infrastructure, distributed systems, and Kafka-based architectures.
- Fork the repository
- Create a new branch for your feature or fix
git checkout -b feature/your-feature-name- Commit your changes
git commit -m "Add: your feature description"- Push to your fork
git push origin feature/your-feature-name- Submit a Pull Request (PR) with a clear explanation of your change 10
- Follow consistent code formatting (Prettier / ESLint recommended)
- Add comments where logic is complex or business-critical
- Use TypeScript types and interfaces wherever possible
- Write descriptive commit messages
- Test your changes before creating a PR
Your name will be added to the Contributors section once your PR is merged 🎉 11
Akash Raj MERN Stack & Backend Developer
This project is licensed under the MIT License — free to use, modify, and distribute with attribution. 12