Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

ChronosFeed — UDP Multicast Market Data System

A production-style low-latency market data feed built in C++17, with a real-time terminal dashboard. Mimics how exchanges like NSE/CME broadcast market data to subscribers.

┌─────────────┐   UDP Multicast 239.0.0.1:5000   ┌──────────────┐
│  Publisher  │ ─────────────────────────────────▶│  Subscriber  │
│             │                                    │              │
│  Retransmit │ ◀── RetransmitRequest (unicast) ── │  Gap Detect  │
│  Store      │ ──── Missing packets (unicast) ───▶│  + Buffer    │
└──────┬──────┘                                    └──────┬───────┘
       │ StatsMessage (UDP :5002)                         │ StatsMessage (UDP :5003)
       ▼                                                  ▼
┌──────────────────────────────────────────────────────────────────┐
│                     Stats Collector                              │
│              writes dashboard/data/stats.json                    │
└──────────────────────────┬───────────────────────────────────────┘
                           │ HTTP poll every 1s
                           ▼
              ┌────────────────────────┐
              │   Dashboard (HTML)     │
              │   Live charts + KPIs   │
              └────────────────────────┘

Quickstart

Windows

Requirements: CMake + Visual Studio 2019/2022 (or MinGW-w64)

build_windows.bat

Then run in three separate Command Prompts:

build\Release\publisher.exe
build\Release\subscriber.exe
build\Release\stats_collector.exe

Open dashboard/index.html in your browser.

Linux / macOS

Requirements: cmake, g++, pthread

chmod +x build_linux.sh
./build_linux.sh

Then run in three separate terminals:

./build/publisher
./build/subscriber
./build/stats_collector

Open dashboard/index.html in your browser.

Note: On Linux you may need to add a multicast route:

sudo ip route add 239.0.0.1/32 dev lo

Architecture

Publisher (src/publisher.cpp)

  • Creates a UDP socket and sends MarketMessage structs to multicast group 239.0.0.1:5000
  • Each message has a monotonically increasing sequence_number
  • Stores sent messages in a lock-free ring buffer (RetransmitStore) for replay
  • Runs a retransmit handler thread on port 5001 — receives RetransmitRequest from subscribers and resends missing packets
  • Broadcasts StatsMessage to port 5002 every 500ms

Subscriber (src/subscriber.cpp)

  • Joins the multicast group using IP_ADD_MEMBERSHIP
  • Tracks expected_seq — detects gaps when msg.sequence_number > expected_seq
  • On gap detection: sends a RetransmitRequest (unicast) to the publisher
  • Buffers out-of-order messages in a std::map and drains them once the gap is filled
  • Measures per-message latency (timestamp_ns in message - recv time)
  • Computes mean / p99 / max latency over a rolling window of 10,000 messages
  • Sends StatsMessage to port 5003 every 500ms

Stats Collector (src/stats_collector.cpp)

  • Non-blocking UDP sockets on ports 5002 and 5003
  • Merges publisher + subscriber stats into a single JSON snapshot
  • Writes dashboard/data/stats.json every second
  • Keeps 120 snapshots of history for chart rendering

Dashboard (dashboard/index.html)

  • Zero-dependency, self-contained HTML/CSS/JS
  • Polls data/stats.json every 1 second
  • Shows: throughput chart, latency history, gap chart, live order book, event log, ticker
  • Falls back to demo/simulation mode if no backend is running

Message Wire Format

All structs use #pragma pack(1) — no padding bytes. Safe to cast raw recv() bytes.

MarketMessage (49 bytes)
├── sequence_number  uint64  8 bytes
├── timestamp_ns     uint64  8 bytes
├── symbol           char[8] 8 bytes  (space-padded)
├── bid              double  8 bytes
├── ask              double  8 bytes
├── last_price       double  8 bytes
├── volume           uint64  8 bytes
└── msg_type         uint8   1 byte

RetransmitRequest (22 bytes)
├── from_sequence    uint64  8 bytes
├── to_sequence      uint64  8 bytes
├── subscriber_ip    uint32  4 bytes
└── subscriber_port  uint16  2 bytes

Benchmark Results (example, varies by machine)

Mode Throughput Mean Latency p99 Latency
UDP Multicast ~2.1M msg/sec 8–40 µs 25–80 µs
TCP Unicast ~340K msg/sec 55–120 µs 150–300 µs

(Run on Ubuntu 22.04, i7-12700H, loopback interface)

To control publish rate:

./build/publisher 100    # 100µs between batches = ~80k msg/sec
./build/publisher 10     # 10µs between batches  = ~800k msg/sec
./build/publisher 1      # 1µs between batches   = ~2M msg/sec

Known Limitations and Future Work

  1. Byte order: All multi-byte integers in the struct are sent in host byte order. For multi-machine deployment, wrap each field with htonl/htons on send and ntohl/ntohs on receive, or use a proper serialization format.

  2. Single publisher: The retransmit design assumes one publisher. For redundant publishers (hot-standby), add a publisher ID field to MarketMessage.

  3. Gap buffer: The subscriber gap buffer uses std::map with a lock. A lock-free MPSC queue would be more appropriate in production.

  4. Clock sync: Latency measurement assumes publisher and subscriber share the same clock (loopback). For multi-machine testing, use PTP/IEEE 1588 hardware timestamps.

  5. Security: No authentication or encryption. Real exchanges use HMAC-signed headers or TLS at the gateway layer.


Project Structure

ChronosFeed/
├── CMakeLists.txt          Build configuration
├── build_windows.bat       Windows one-click build
├── build_linux.sh          Linux/macOS one-click build
├── README.md               This file
├── include/
│   ├── config.h            Network constants
│   ├── message.h           Wire-format structs
│   ├── platform.h          Cross-platform socket/time
│   └── retransmit_store.h  Lock-free ring buffer
├── src/
│   ├── publisher.cpp       Multicast sender + retransmit handler
│   ├── subscriber.cpp      Multicast receiver + gap detection
│   ├── stats_collector.cpp Stats aggregator → JSON
│   └── retransmit_store.cpp (stub)
└── dashboard/
    ├── index.html          Live trading terminal UI
    └── data/
        └── stats.json      Written by stats_collector at runtime

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages