Skip to content

aryarajeev000/order-execution-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

Order Execution Engine (Backend)

A backend system that simulates market order execution on Solana DEXs with smart routing, asynchronous execution, real-time updates, and persistent order history.


Features

  • Market order execution simulation
  • Smart DEX routing (Raydium vs Meteora)
  • Asynchronous execution using BullMQ and Redis
  • Concurrency control and retry with exponential backoff
  • Real-time lifecycle updates via WebSockets
  • Persistent order history stored in PostgreSQL
  • Simple frontend for live order tracking
  • Clean, production-style architecture

System Architecture

Client (Postman / Frontend) ↓ Express API ↓ BullMQ Queue (Redis) ↓ Worker (Concurrency + Retry) ↓ DEX Router (Raydium vs Meteora) ↓ Execution Simulation ↓ PostgreSQL (Order History) ↓ WebSocket (Live Status Updates)


Tech Stack

Component Technology
Backend Framework Node.js + Express
Queue & Retry BullMQ
In-memory Store Redis
Database PostgreSQL
Real-time Updates Socket.IO
Frontend HTML + JavaScript
Environment Config dotenv

Design Decisions

Redis and BullMQ

Redis is used to manage active and in-flight orders. BullMQ provides job queuing, concurrency limits, retries, and backoff strategies to ensure non-blocking and resilient execution.

PostgreSQL

PostgreSQL stores finalized order history. This ensures persistence, auditability, and enables historical queries while keeping Redis focused on transient state.

WebSockets

WebSockets are used to stream real-time lifecycle events to clients, eliminating the need for polling and improving responsiveness.


Order Lifecycle

Each order follows the lifecycle below:

pending routing building submitted confirmed failed

Lifecycle events are streamed via WebSocket, and the final state is persisted in PostgreSQL.


API Endpoints

Create Order

POST /api/orders/execute

Request Body

json { "tokenIn": "SOL", "tokenOut": "USDC", "amount": 1 }

Response { "orderId": "uuid", "status": "accepted" }

Get Order History (Optional)

GET /api/orders/history

Returns all past orders stored in PostgreSQL.

WebSocket Usage

Subscribe to Order Updates socket.emit("subscribeOrder", orderId);

Receive Lifecycle Events

socket.on("status", (data) => { console.log(data); });

Database Schema

CREATE TABLE orders ( id UUID PRIMARY KEY, token_in TEXT NOT NULL, token_out TEXT NOT NULL, amount NUMERIC NOT NULL, dex TEXT, price NUMERIC, status TEXT NOT NULL, error TEXT, created_at TIMESTAMP DEFAULT NOW(), completed_at TIMESTAMP );

Environment Variables

Create a .env file in the backend root:

PORT=3000

PG_HOST=localhost PG_PORT=5433 PG_USER=postgres PG_PASSWORD=postgres PG_DATABASE=order_engine

REDIS_HOST=127.0.0.1 REDIS_PORT=6379

How to Run the Project

Start Redis redis-server

Start PostgreSQL

Ensure the PostgreSQL service is running and the database exists.

Install Dependencies npm install

Start Backend

npm run dev

Run Frontend

Open the file below in a browser:

frontend/index.html

Testing

Submit orders via Postman

Observe lifecycle logs in the terminal

Verify retries and concurrency handling

Check stored orders in PostgreSQL:

SELECT * FROM orders;

Future Enhancements

Real Solana SDK integration

Limit and sniper order types

On-chain transaction execution

Authentication and user accounts

Metrics and monitoring

Distributed workers

Bull Board monitoring UI

Deployment

This project is currently demonstrated using local Redis and PostgreSQL instances.

In a production deployment, the backend can be deployed to a cloud platform (Render, Railway, Fly.io) by connecting it to managed Redis and PostgreSQL services.

The application architecture is designed to be cloud-ready, with all infrastructure dependencies externalized via environment variables.

Author

Rajeev Sutrakar, Software Developer

About

A backend system that simulates market order execution on Solana DEXs with smart routing, asynchronous execution, real-time updates, and persistent order history.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors