Skip to content

Repository files navigation

CEX — Centralized Exchange

A centralized exchange built from scratch, with an in-memory order-matching engine decoupled from the database via Redis — the same core pattern real exchanges use to keep the matching path fast while persistence happens asynchronously.

Live Demo →

Why This Project

Order matching can't wait on database writes. So instead of one monolithic API doing everything, the system is split into independent services connected by Redis: the HTTP API only enqueues requests, a dedicated in-memory engine matches orders and updates the order book, and a separate worker persists the results to Postgres afterward. A WebSocket service listens on the same Redis pub/sub channel to push live price and order-book updates to connected clients. This means the matching engine never blocks on I/O, and every piece can be scaled or restarted independently.

Features

  • User authentication (bcrypt-hashed passwords)
  • Place, match, and cancel orders in real time
  • Live order book and trade execution via an in-memory matching engine
  • Real-time price updates and candlestick charts over WebSockets
  • Asynchronous trade/order persistence — writes never block matching
  • Interactive trading UI with live charts (lightweight-charts)

Tech Stack

  • Next.js 16 + React 19 + Tailwind CSS — client
  • Express — HTTP API
  • Bun — runtime for the engine, WebSocket, and worker services
  • Redis — message queue + pub/sub between services
  • PostgreSQL + Prisma — persistence layer
  • Turborepo — monorepo structure
  • Docker + Nginx — containerized deployment and reverse proxy
  • GitHub Actions — CI/CD

Architecture

Architecture

Project Structure

cex/
├── apps/
│   ├── client/         # Next.js trading UI
│   ├── http/           # Express API — auth, enqueues orders
│   ├── engine/         # In-memory order-matching engine
│   ├── ws/             # WebSocket server — live updates via Redis pub/sub
│   └── db-worker/      # Consumes Redis events, persists to Postgres
├── packages/
│   ├── db/             # Prisma schema + client
│   ├── redis/          # Shared Redis pub/sub + queue client
│   └── common/         # Shared types/constants
├── ops/                # Deployment/infra configs

Getting Started Using Git Clone

# Clone the repo
git clone https://github.com/DevDesignAmitesh/cex.git
cd cex

# Install dependencies
bun install

# Set up environment variables
cp .env.example .env

# Set up the database
bun run db:generate
bun run db:migrate

# Run everything in dev mode (Turborepo)
bun run dev

Getting Started Using Docker

# Clone the repo
git clone https://github.com/DevDesignAmitesh/cex.git
cd cex

docker compose -f ./ops/dev.docker-compose.yml up

License

MIT

About

A centralized crypto exchange with an in-memory order-matching engine, Redis-based async architecture, and real-time WebSocket price updates.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages