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.