Real-Time Virtual Execution System

Real-Time Virtual Execution Architecture

A persistent low-latency execution system designed for high-frequency websocket streams, real-time trade state transitions, and automated recovery.

Inputs

Input Sources

Execution plane

Real-Time Execution Layer

Matching workers
Matching Worker 1

Entry / target / SL / expiry evaluation on every relevant tick.

Persistent Node.js workers evaluate ticks against entry, target, stoploss, and expiry. Stateful processes replace cron/Lambda intervals for lower tail latency and fewer duplicate evaluations.

Matching Worker 2

Entry / target / SL / expiry evaluation on every relevant tick.

Persistent Node.js workers evaluate ticks against entry, target, stoploss, and expiry. Stateful processes replace cron/Lambda intervals for lower tail latency and fewer duplicate evaluations.

Matching Worker N

Entry / target / SL / expiry evaluation on every relevant tick.

Persistent Node.js workers evaluate ticks against entry, target, stoploss, and expiry. Stateful processes replace cron/Lambda intervals for lower tail latency and fewer duplicate evaluations.

MongoDB

Source of truth

  • Source of truth
  • Trade history
  • State transitions
  • Durable persistence

Every transition that must survive restarts is durable in MongoDB. Workers write audited state; reads for heavy analytics stay off the hot path.

Mongo Watch Streams

  • Synchronizes Redis state
  • Detects updates
  • Removes stale execution state

Change streams propagate authoritative writes back into Redis, trimming ghost keys and aligning in-memory snapshots with persisted documents.

Monitoring & Alerts

  • Latency alerts
  • Provider failures
  • Websocket disconnects
  • System health monitoring

Grafana + Prometheus watch SLO-style signals: lag, error budgets, provider health. Paging hooks for disconnect storms or reconciliation backlog.

Hindsight Recovery Engine

  • Detects outages
  • Fetches missing historical prices
  • Re-runs matching logic
  • Reconciles state
  • Restores realtime execution

When gaps are detected, historical bars replay through the same rules engine, reconcile against live-advanced state, then hand control back to the websocket path.

Tick Processing Flow

  1. 1Tick received from websocket
  2. 2Active ticker check against Redis pool
  3. 3Fetch active trades for symbol
  4. 4Match engine evaluates entry / SL / TP / expiry
  5. 5Update Redis execution snapshot
  6. 6Persist transition to MongoDB
  7. 7Emit downstream events

Recovery Flow / Hindsight Engine

1Websocket disconnect
2Provider failure
3Matching paused
4Trigger hindsight recovery
5Detect gap range
6Fetch missing historical prices
7Re-run matching logic
8Reconcile Redis + Mongo
9Resume realtime stream
AutomaticReliableLow-latency

Redis + MongoDB Synchronization

Redis
  • Active trades
  • Trade states
  • In-memory indexes
MongoDB
  • All trades
  • Trade history
  • Audit log

Sync mechanism

Change streams propagate authoritative documents; workers read Redis first, Mongo for recovery and analytics. Writes finalize in MongoDB; watch streams push deltas into Redis. Periodic reconciliation jobs heal drift if a worker crashes mid-flight.

Websocket Subscription Lifecycle

Trade ingestion

New recommendation allocates symbol interest.

Ticker deduplication

Collapse duplicate advisor requests per symbol.

Subscription add

Open channel only if pool count transitions 0→1.

Tick streaming

Fan-out ticks to subscribed worker queues.

Subscription retention

Keep channel while any trade references symbol.

Subscription remove

Tear down when last trade closes.

Key Engineering Challenges

  • Handling ~1000 websocket ticks/sec without subscribing to the full market.
  • Replacing interval-based Lambda matching with persistent low-latency workers.
  • Maintaining synchronized execution state across Redis and MongoDB.
  • Designing automatic hindsight recovery after websocket/provider outages.
  • Managing dynamic ticker subscription pools at scale.

Key Design Decisions

  • Why persistent workers replaced cron/Lambda
  • Why Redis became the execution state layer
  • Why MongoDB remained source of truth
  • Why hindsight matching is fallback-only after gaps
  • Why ticker subscription pooling mattered at scale

Outcomes

  • Real-time trade visibility
  • Reduced execution latency
  • Stable high-frequency processing
  • Automatic recovery & reconciliation
  • Strong Redis/Mongo consistency
  • Operational reliability at scale