A high-performance, distributed order matching and settlement system built with TypeScript, Node.js, Redis, and PostgreSQL. Processes 100+ orders per second with millisecond-level latency.
This is a complete real-time trading exchange platform with:
- Backend: High-performance order matching engine with async processing
- Frontend: Interactive trading dashboard with live order books and portfolio management
- Throughput: 100+ orders/second
- Trade Generation: ~140 orders/sec matched at 75%+ ratio
- Latency: P99 < 1000ms at 100 concurrent users
- Architecture: Async processing with Redis queues and worker threads
Frontend (Next.js)
β
REST API (Express) / WebSocket
β
Redis Queues (order_queue, trade_queue)
β
ββ Matching Worker (Order Processing)
β ββ Matching Engine (Trade Generation)
β ββ Redis (trade_queue)
β β
β Settlement Worker (Trade Settlement)
β ββ PostgreSQL (Persistence)
β β
β Metrics Collector
β
ββ WebSocket Server (Real-time Updates)
Components:
- API Layer: RESTful endpoints for orders, authentication, and portfolio
- Matching Engine: Price-time priority matching with side crossing validation
- Settlement Service: ACID-compliant trade settlement with idempotency
- Metrics System: Real-time performance tracking and throughput monitoring
- WebSocket Server: Live order books and trade execution feeds
- User places order via REST API β 202 ACCEPTED response
- Order queued to Redis
order_queue - Matching Worker consumes and processes orders
- Matched trades generated and published to
trade_queue - Settlement Worker batches and settles trades (50 trades or 15ms)
- Metrics and WebSocket updates sent to connected clients
- β Limit orders with price-time priority matching
- β Buy/Sell side crossing validation
- β FIFO queue within same price level
- β ACID-compliant trade settlement
- β Idempotent order processing
- β Trade history and portfolio tracking
- β Real-time metrics dashboard
- β WebSocket live feeds
- β Rate limiting (API protection)
- β JWT authentication
- β Comprehensive error handling
- β Worker thread isolation
- Node.js 18+
- PostgreSQL 14+
- Redis 7+
# Clone repository
git clone <repo-url>
cd order-matching-engine
# Backend setup
cd backend
npm install
cp .env.example .env
# Update .env with your database and Redis URLs
# Run migrations
npx prisma migrate dev
# Start backend
npm run dev
# Frontend setup (in new terminal)
cd frontend
pnpm install
npm run devBackend (.env):
DATABASE_URL=postgresql://user:password@localhost:5432/trading_db
REDIS_URL=redis://localhost:6379
NODE_ENV=development
PORT=8000Frontend (.env.local):
NEXT_PUBLIC_API_URL=http://localhost:8000
NEXT_PUBLIC_WS_URL=ws://localhost:8000cd backend
npm run loadtest 50 30 # 50 users, 30 secondsorder-matching-engine/
βββ backend/
β βββ src/
β β βββ index.ts # Express server & worker management
β β βββ api/ # REST endpoints
β β βββ auth/ # Authentication
β β βββ services/ # Business logic
β β βββ worker/ # Worker threads
β β βββ queue/ # Redis queue management
β β βββ realtime/ # WebSocket server
β β βββ middleware/ # Express middleware
β β βββ monitoring/ # Metrics collection
β βββ prisma/
β β βββ schema.prisma # Database schema
β β βββ migrations/ # Database migrations
β βββ tests/
β β βββ loadTest.ts # Load testing script
β βββ package.json
β
βββ frontend/
β βββ app/
β β βββ page.tsx # Home page
β β βββ login/ # Authentication
β β βββ dashboard/ # Trading dashboard
β β βββ layout.tsx # Root layout
β βββ components/ # React components
β βββ lib/ # Utilities
β βββ package.json
β
βββ README.md # This file
βββ ARCHITECTURE.md # System design details
βββ DEVELOPMENT.md # Developer guide
| Metric | Value |
|---|---|
| Orders Placed | 3,980 |
| Throughput | 100.42 orders/sec |
| Trades Executed | 14,787 |
| Avg Latency | 454.92 ms |
| P95 Latency | 751 ms |
| P99 Latency | 993 ms |
| Success Rate | 100% |
- Order Processing: 100+ orders/second
- Trade Matching: 75%+ of orders match (depends on price crossing)
- Settlement: Batched every 50 trades or 15ms
- Queue Latency: <50ms average
- Database Writes: Batched for efficiency
- ARCHITECTURE.md - System design and component details
- DEVELOPMENT.md - Development setup and contribution guidelines
- backend/API_ROUTES.md - API endpoint documentation
- backend/SETUP.md - Detailed backend setup instructions
- frontend/README.md - Frontend documentation
- Create a feature branch:
git checkout -b feature/your-feature - Make your changes and test thoroughly
- Run load tests:
npm run loadtest - Submit a pull request
Backend:
npm run dev # Start with ts-node
npm run build # Build TypeScript
npm run loadtest # Run load test (default: 50 users, 60s)
npm test # Run tests
npx prisma studio # Open database UIFrontend:
npm run dev # Start dev server
npm run build # Build for production
npm run lint # Run ESLintPOST /auth/register- User registrationPOST /auth/login- User loginPOST /auth/logout- User logout
POST /orders- Place new orderGET /orders- Get user's ordersGET /orders/:id- Get specific order
GET /portfolio- Get user portfolio
GET /metrics- System performance metrics
See API_ROUTES.md for detailed endpoint documentation.
MIT
For issues and questions, please open an issue on the repository.