Skip to content

cse23-mora/CricketLiveScore-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cricket Live Score Backend (Rust)

This is a super fast and lightweight backend I built in Rust for pushing live cricket scores. It's designed to handle a ton of connections without hogging memory or CPU. I went with Rust because it's awesome for performance stuff like this – no garbage collector slowdowns!

Features

  • Blazing Fast: Rust's speed means everything runs quick.
  • Tiny Memory Use: Keeps things lean with smart data handling.
  • Live Updates: WebSockets for streaming scores in real-time.
  • Simple REST API: Easy endpoints to grab match info.
  • Redis for Speed: Quick lookups and pub/sub for updates.
  • Async Everything: Using Tokio so it doesn't block on I/O.
  • CORS: Set up so your frontend (like React) can connect without issues.

Architecture

┌─────────────────────────────────────────────┐
│ Frontend (React)                            │
└──────────────┬──────────────────────────────┘
               │
          HTTP + WebSocket
               │
┌──────────────▼──────────────────────────────┐
│ Rust Backend (Axum)                         │
│ ┌────────────────────────────────────────┐ │
│ │ REST API (GET /api/matches/live)       │ │
│ └────────────────────────────────────────┘ │
│ ┌────────────────────────────────────────┐ │
│ │ WebSocket Server (/ws)                 │ │
│ │ - Subscribe/Unsubscribe                │ │
│ │ - Broadcast updates                    │ │
│ └────────────────────────────────────────┘ │
│ ┌────────────────────────────────────────┐ │
│ │ Redis Pub/Sub Listener                 │ │
│ │ - Pattern: match_updates:*             │ │
│ └────────────────────────────────────────┘ │
└──────────────┬──────────────────────────────┘
               │
         Redis Client
               │
┌──────────────▼──────────────────────────────┐
│ Redis                                       │
│ - match:{id}:info                           │
│ - match:{id}:score                          │
│ - match:{id}:scorecard:{1|2}                │
└─────────────────────────────────────────────┘

Basically, frontend hits the backend via HTTP or WS, backend talks to Redis for data and listens for updates to push out.

Tech Stack

  • Framework: Axum 0.7 – love how lightweight and fast it is.
  • Runtime: Tokio for async magic.
  • Redis: redis-rs with async, keeps connections snappy.
  • Serialization: Serde and serde_json – standard but reliable.
  • Logging: tracing with tracing-subscriber for decent logs.
  • WebSocket: Axum's built-in support, no extra deps needed.

Prerequisites

  • Rust 1.70+ (grab it from rustup.rs – I always use the latest stable)
  • Redis running on localhost:6379 (or tweak the env var)

Installation

  1. Get Rust if you don't have it:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env
  1. Jump into the dir:
cd backend-rust
  1. Build it:
# For dev
cargo build
# For prod (way faster)
cargo build --release

Configuration

Set these env vars (defaults work for local):

Variable Default What it does
REDIS_URL redis://127.0.0.1:6379 Where Redis is
HOST 0.0.0.0 Bind address
PORT 3001 Port to listen on

Example:

export REDIS_URL="redis://localhost:6379"
export PORT=3001

Running

Dev Mode

cargo run

Prod Mode

cargo build --release
./target/release/cricket-live-score-backend

With Custom Stuff

REDIS_URL=redis://localhost:6379 PORT=8080 cargo run

API Endpoints

REST API

Get Live Matches

GET /api/matches/live

Example response:

[
  {
    "match_id": "match123",
    "team_a": "IND",
    "team_b": "AUS",
    "team_a_score": "105/2",
    "team_b_score": "-",
    "overs": "10.4",
    "status": "Live"
  }
]

Health Check

GET /health
{
  "status": "ok",
  "service": "cricket-live-score-backend"
}

WebSocket API

Connect to: ws://localhost:3001/ws

Subscribe

{
  "action": "subscribe",
  "match_id": "match123"
}

Gets you the full state back:

{
  "type": "full_state",
  "data": {
    "match_id": "match123",
    "info": { ... },
    "score": { ... },
    "scorecard_inn_1": { ... },
    "scorecard_inn_2": null
  }
}

Unsubscribe

{
  "action": "unsubscribe",
  "match_id": "match123"
}

Updates from Server

{
  "type": "score_update",
  "data": {
    "current_inning": 1,
    "batting_team": "India",
    "runs": 105,
    "wickets": 2,
    "overs": "10.4",
    ...
  }
}

Performance Characteristics

Here are some benchmark numbers I've pulled out of thin air

Memory

  • Startup: ~5-10 MB
  • Each WS connection: ~8-16 KB
  • Per match channel: ~1-2 KB

Throughput

  • REST: 10k+ req/sec on one core
  • WS messages: 50k+ /sec
  • Connections: Handles 10k+ easily

Latency

  • REST: <1ms with local Redis
  • WS broadcast: <100μs
  • Updates to clients: <5ms

Project Structure

backend-rust/
├── Cargo.toml  # All the deps and config
├── src/
│ ├── main.rs  # Starts the server, sets up routes
│ ├── api.rs   # REST handlers
│ ├── models.rs # Structs for data
│ ├── redis_client.rs # Redis get/set stuff
│ ├── websocket.rs # WS logic and broadcasting
│ └── pubsub.rs # Listening to Redis updates
└── README.md

Development

Tests

cargo test

Check

cargo check

Format

cargo fmt

Lint

cargo clippy

Auto-reload (nice for dev)

cargo install cargo-watch
cargo watch -x run

Production Deployment

Build the binary

cargo build --release

It's in target/release/cricket-live-score-backend – just copy and run anywhere with Rust runtime (none needed, it's static!).

Optimizations in release

Tweaked Cargo.toml for:

  • opt-level=3
  • lto=true
  • codegen-units=1
  • strip=true

Docker

FROM rust:1.70 as builder
WORKDIR /app
COPY . .
RUN cargo build --release

FROM debian:bookworm-slim
COPY --from=builder /app/target/release/cricket-live-score-backend /usr/local/bin/
CMD ["cricket-live-score-backend"]

Monitoring

Uses tracing for logs. Set RUST_LOG to control:

RUST_LOG=debug cargo run

Levels: error, warn, info, debug, trace.

Contributing

Pull requests welcome! Run cargo fmt and cargo clippy first, keep it Rusty.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors