Skip to content

rogerio-castellano/real-time-betting-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

63 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🎯 Real-Time Betting Engine

🚧 Work in Progress β€” This evolving real-time betting engine is built for speed, scalability, and live insights. Some features are experimental or incomplete, with active exploration into resilience, profiling, and autoscaling.

A fast, horizontally scalable betting engine designed for real-time ingest, aggregation, and performance visualization β€” powered by Go, NATS JetStream, PostgreSQL, Redis, and Kubernetes.

⚑ Built for speed. πŸ“Š Tuned for stats. 🧠 Optimized for concurrency.

πŸ”₯ Features

  • JetStream + Queue Groups: Load-balanced message consumption with durable delivery
  • PostgreSQL-backed storage: Accurate persistence of betting data with automatic failover
  • Redis Odds Engine: Lightweight odds update logic using atomic Redis counters
  • Live Stats Dashboard: WebSocket-powered UI with real-time metrics, trends, and failure insights
  • Kubernetes Native: Full deployment stack with autoscaling via Horizontal Pod Autoscaler (HPA)
  • pprof & Prometheus hooks: Built-in memory profiling and metrics scraping for performance tuning

πŸ› οΈ Tech Stack

Layer Stack
Language Go 1.24
Messaging NATS JetStream
Database PostgreSQL
Key-Value Redis
UI Dashboard HTML + WebSocket + Vanilla JS
Orchestration Kubernetes (HPA enabled)
Observability pprof, Prometheus-ready /metrics
Container Docker & Kubernetes-ready images

πŸš€ How It Works

  1. Bets are published to the bets_stream using JetStream (e.g., via simulator)
  2. Backend pods subscribe to the stream via QueueSubscribe, enabling horizontal scaling
  3. Each bet is:
    • Persisted to the database
    • Used to update Redis-based odds
    • Emits stats.update messages to stats_stream
  4. Stats Aggregator merges per-pod metrics and feeds the WebSocket dashboard
  5. The dashboard presents live totals, per-second rates, and health indicators

πŸ“¦ Setup

1. Deploy Infrastructure

make create

This creates Redis, NATS (JetStream enabled), Postgres, and initializes streams and tables.

2. Launch Backend and Aggregator

make k8sbuild

The backend consumes bets, stores them, and updates stats. The aggregator merges pod stats and serves the WebSocket API.

3. Simulate Load

go run load-simulator/main.go 10 1000

β†’ Sends 1,000 bets at 10ms intervals.

πŸ“Š Live Dashboard

Visit the Real-Time Dashboard to see:

  • βœ… Total bets processed
  • πŸ’΅ Bet volume
  • πŸ” Odds updates
  • 🧯 Redis & DB failure metrics
  • 🎨 Color-coded accuracy bars
  • πŸ”Œ WebSocket status banner

Dashboard Preview

βš™οΈ Scaling & HPA

Pods auto-scale based on memory usage:

HorizontalPodAutoscaler:
  minReplicas: 3
  maxReplicas: 5
  target:
    averageMemory: 25Mi

Manual scaling also supported:

kubectl scale deployment betting-engine-backend --replicas=5

πŸ“ˆ Profiling & Metrics

  • Enable memory profiling:
curl http://localhost:6060/debug/pprof/heap
go tool pprof http://localhost:6060/debug/pprof/heap

Expose metrics via /metrics endpoint:

curl http://localhost:8081/metrics

Can be scraped by Prometheus + visualized in Grafana.

πŸŽ›οΈ Reset and Debug Tools

Use make reset to purge bets, odds, and stream history.

Streams:

nats stream info bets_stream
nats consumer info bets_stream bets_consumer

Database:

kubectl run pg-client --rm -it --image=postgres -- psql -U postgres

Redis:

kubectl run redis-inspect --rm -it --image=redis -- redis-cli

πŸ“œ License

MIT β€” see LICENSE for full details.

About

🎯 A real-time betting engine prototype built with Go, NATS JetStream, Redis, and PostgreSQL.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published