Skip to content

About

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

CHESS AND WHATEVER (GAMBIT)

Complete System Architecture, Engineering & Failure Analysis

Document Type: Comprehensive Technical Research & System Architecture Study
Application Name: Gambit (Repository: codealisha99/chessandwhatever)
Auditor / Author: Principal Software Architect, Distributed Systems Engineer & SRE
Target Audience: Software Architects, Backend Engineers, and Systems Learners
Status: Complete Independent Engineering Study (Read-Only Investigation)


EXECUTIVE INTRODUCTION

This study is an exhaustive, repository-grounded engineering dissection of Chess and Whatever (Gambit). Every technical claim, architecture diagram, failure mode, and protocol specification in this document is derived directly from the source code, tests, and configuration files of the repository.

The objective of this document is to serve as a master-level system design textbook based on an actual, running production-grade multiplayer system. It explains:

  • How real-time state synchronization, turn-based clock systems, and distributed lease ownership are engineered.
  • Why specific distributed systems primitives (atomic Lua scripts, Redis sorted sets, PostgreSQL advisory locks, and WebSockets) were chosen.
  • Where the architecture succeeds, where edge cases cause catastrophic failure, and how to reason about distributed failures under real-world network conditions.

PART 1: PRODUCT AND SYSTEM OVERVIEW

1.1 Product Identity & Problem Statement

Gambit (gambit-chess) is a full-featured, low-latency, multiplayer online chess platform. It provides rated matchmaking, private game rooms with shareable invitation links, in-game real-time chat, live game spectating, historical game archive review with PGN export, and single-player practice against a client-side Stockfish AI engine.

The Engineering Challenges Solved:

  1. Clock Synchronization under Latency: In fast-paced blitz chess (e.g., 3+0 or 5+3), network round-trip time (RTT) variance can distort player clocks. Gambit implements a server-authoritative monotonic clock with client-side optimistic time extrapolation.
  2. Atomic Fair Matchmaking: Preventing race conditions when hundreds of players queue simultaneously for identical time controls without pairing a user against themselves or generating duplicate games.
  3. Multi-Instance Horizontal Scale: Allowing players connected to different physical server instances to play against each other seamlessly via Redis Pub/Sub, while designating a single server instance as the authoritative "owner" of the game clock.
  4. Resilient Game State Recovery: Guaranteeing that if a server instance crashes mid-game, a peer server instance can atomically adopt the game from Redis snapshots and resume turn clocks without losing moves, clock times, or chat history.

1.2 Target Personas & Primary User Journeys

  1. Casual Unauthenticated Players (Guests): Can instantly play rated chess against humans or bots without a registration wall. Guest accounts are automatically provisioned with ephemeral identities and purged after 7 days of inactivity.
  2. Competitive Rated Players: Create persistent accounts, earn Elo ratings via symmetric rating calculations, review past game archives, and appear on public leaderboards.
  3. Private Challengers: Generate 6-character room codes or direct URLs (/join/:code), allowing two friends across separate networks to connect, negotiate piece colors, and play privately with real-time chat.
  4. Spectators: Browse active high-rated matches via live discovery (GET /api/games/live), watch move updates in real time, and observe spectator counts.
  5. Solo Analysts: Practice against local Stockfish AI across 8 calibrated difficulty tiers or analyze completed games with move-by-move engine evaluations.

1.3 Subsystem Decomposition & Communication Protocols

flowchart TD
    subgraph BrowserClient["Browser Client (React 19 + Vite)"]
        UI["React UI Tree"]
        AuthCtx["AuthContext (Local Storage Token)"]
        OnlineCtx["OnlineContext (WebSocket Manager)"]
        StockfishWorker["Stockfish 19 Web Worker"]
    end

    subgraph EdgeGateway["Cloudflare Global Edge"]
        WorkerGateway["Worker Gateway (worker/index.ts)"]
        EdgeCORS["0ms Edge CORS Preflight"]
        WSRelay["WebSocketPair Bidirectional Relay"]
    end

    subgraph ServerCompute["Backend Node.js 24 Server (backend1)"]
        HttpServer["HTTP REST Server (src/http.ts)"]
        WSServer["WebSocket Server (src/index.ts)"]
        GM["GameManager (src/GameManager.ts)"]
        GameEngine["Chess Rules & Clocks (src/Game.ts)"]
        CleanupScheduler["Guest Cleanup Scheduler (src/cleanup.ts)"]
    end

    subgraph DistributedState["Distributed Coordination (Redis 7)"]
        LuaScripts["Lua Matchmaker & Leases (src/redis.ts)"]
        PubSub["Redis Pub/Sub Channels (src/redis.ts)"]
        Snapshots["Serialized Game Snapshots (src/redis.ts)"]
    end

    subgraph PersistentDB["Relational Database (Neon PostgreSQL)"]
        UsersTable["users (Identity & Ratings)"]
        GamesTable["games (JSONB Moves & Results)"]
        SessionsTable["sessions (Revocation Tracking)"]
        TokensTable["password_reset_tokens"]
        OAuthTable["provider_identities"]
    end

    UI --> AuthCtx
    UI --> OnlineCtx
    UI --> StockfishWorker
    AuthCtx -->|"REST /api/*"| WorkerGateway
    OnlineCtx -->|"WSS /ws?ticket=..."| WorkerGateway
    WorkerGateway --> EdgeCORS
    WorkerGateway --> WSRelay
    WSRelay -->|"TCP Proxy"| WSServer
    WorkerGateway -->|"HTTP Proxy"| HttpServer

    HttpServer --> GM
    WSServer --> GM
    GM --> GameEngine
    GM --> DistributedState
    HttpServer --> PersistentDB
    GM --> PersistentDB
    CleanupScheduler --> PersistentDB
Loading

1.4 Comprehensive Feature Inventory

The following master inventory cross-references every functional feature against its concrete code locations, database schemas, Redis operations, and failure vulnerabilities:

Feature Detailed Description Frontend Components Backend Handlers Database Tables Redis Keys / Operations Communication State Location Status Failure Risks
User Registration Creates persistent user account with unique username and scrypt-hashed password. Auth.tsx, AuthContext.tsx POST /api/auth/signup in http.ts:L130 users, sessions KEYS.rate(auth, ip) HTTP POST PostgreSQL + LocalStorage Token IMPLEMENTED Username race condition (handled via ON CONFLICT DO NOTHING). Rate limit exhaustion.
Password Login Authenticates credentials, creates active session record, issues signed JWT token. Auth.tsx, AuthContext.tsx POST /api/auth/login in http.ts:L138 users, sessions KEYS.rate(auth, ip) HTTP POST PostgreSQL + LocalStorage Token IMPLEMENTED Brute force attacks (mitigated by IP rate limiting). Hash computation DoS.
Guest Accounts Creates ephemeral Guest_XXXXXX user without password; grants instant play access. Join.tsx, Auth.tsx POST /api/auth/guest in http.ts:L151 users, sessions KEYS.rate(auth, ip) HTTP POST PostgreSQL + LocalStorage Token IMPLEMENTED Table bloat (mitigated by automated 7-day cleanup scheduler in cleanup.ts).
Google OAuth 2.0 / OIDC Federated Google login with PKCE state/nonce in Redis and JWKS verification. AuthCallback.tsx GET /api/auth/google, GET /api/auth/google/callback in http.ts:L183 users, provider_identities, sessions KEYS.oauthState(state) (10m TTL) HTTP GET / 302 Redirect Redis (State) -> PostgreSQL IMPLEMENTED Upstream Google outages. Nonce replay attacks (mitigated via redis.getdel).
Session Invalidation Revokes active JWT session instantly across HTTP APIs and open WebSockets. AuthContext.tsx POST /api/auth/logout, logout-all in http.ts:L171 sessions (sets revoked_at) CH.user(userId) (publishes session_revoked) HTTP POST + WebSocket broadcast PostgreSQL + Redis Pub/Sub IMPLEMENTED Disconnected client missing revoke event (mitigated by DB check on WS ticket creation).
Password Reset Issues single-use 15-minute SHA-256 hashed token sent via email to update password. ForgotPassword.tsx, ResetPassword.tsx POST /api/auth/forgot-password, reset-password in http.ts:L259 password_reset_tokens, users, sessions KEYS.rate(auth, ip) HTTP POST PostgreSQL IMPLEMENTED Token brute forcing (mitigated by 32-byte entropy and 15-minute TTL).
Matchmaking (FIFO) Enqueues player for time control (1+0, 3+0, 3+2, 5+0, 5+3, 10+0) and matches atomically. Home.tsx, OnlineContext.tsx init_game in GameManager.ts:L189 None (In-memory until finished) KEYS.queue(tc) (List), KEYS.userQueue(uid), Lua matchPop WebSocket Redis FIFO List IMPLEMENTED Ghost queueing on abrupt disconnect (mitigated by socket close hooks).
Private Game Rooms Generates 6-character room code; allows host to pick color and time control. Home.tsx, LobbyModal create_room in GameManager.ts:L196 None KEYS.room(code) (1h TTL), KEYS.userRoom(uid) WebSocket Redis String IMPLEMENTED Code collisions (mitigated by 32-character alphabet and retry loop).
Invitation Joining Second player claims room code; room is atomically consumed and game begins. Join.tsx, OnlineContext.tsx join_room in GameManager.ts:L200 None KEYS.room(code) (GETDEL) WebSocket Redis -> Game Memory IMPLEMENTED Concurrent joins (mitigated by atomic GETDEL in rooms.take).
Chess Clock Engine Server-authoritative turn timers with initial allowance and per-move increment. LiveGame.tsx, Clock.tsx Game.ts:L250, scheduleTurnTimer None (saved on finish) KEYS.game(gid) (clocks, turnStartedAt) WebSocket (move, game_state) Node.js Memory + Redis Snapshot IMPLEMENTED Clock drift under network latency. Stalled Node timers (mitigated by epoch ms).
Move Validation & Execution Validates legal chess moves with chess.js, updates board FEN, detects mate/draws. Board.tsx, LiveGame.tsx move in Game.ts:L244 None KEYS.game(gid) (Snapshot) WebSocket (move, move_rejected) Node.js Memory + Redis Snapshot IMPLEMENTED Illegal client moves (rejected with move_rejected and authoritative state sync).
In-Game Chat Allows playing opponents to exchange text messages (up to 50 retained per game). Chat.tsx, LiveGame.tsx chat in Game.ts:L348 games.moves (not persisted in SQL, only Redis) KEYS.game(gid) (chat array) WebSocket (chat) Node.js Memory + Redis Snapshot IMPLEMENTED XSS payloads (sanitized/escaped by React JSX). Flooding (bucket rate limited).
Live Game Spectating Spectators stream moves, clocks, and chat of active games in real time. Watch.tsx, LiveGame.tsx spectate, unspectate in GameManager.ts:L205 None KEYS.live (ZSet), KEYS.watchers(gid) (Set), CH.watch(gid) WebSocket Redis Sorted Set + Pub/Sub IMPLEMENTED Spectator socket leaks (mitigated by ref-counted channel unsubscriptions).
Disconnection & Grace Timer When player drops, arms a 60-second abandonment countdown before auto-forfeit. PlayerStrip.tsx, OnlineContext.tsx setConnected in Game.ts:L366 None KEYS.game(gid) (abandonAt) WebSocket (presence) Node.js Memory (setTimeout) + Redis IMPLEMENTED Server crash resets timer (mitigated by absolute epoch ms abandonAt in snapshot).
Completed Game Persistence Saves finished game, PGN, moves JSONB, and Elo deltas atomically in PostgreSQL. LiveGame.tsx, Profile.tsx onFinished in GameManager.ts:L317 games, users KEYS.live (ZREM), KEYS.game(gid) (15m TTL) Async Event -> PostgreSQL Transaction PostgreSQL IMPLEMENTED Dual rating application on failover (mitigated by SQL INSERT ... ON CONFLICT DO NOTHING).
Leaderboard & Player Stats Displays top rated players and career records (wins, losses, draws, rating). Leaderboard.tsx, Profile.tsx GET /api/leaderboard, GET /api/users/:name in http.ts:L317 users, games KEYS.online(iid) (Checks live presence) HTTP GET PostgreSQL IMPLEMENTED Slow table scans on scale (mitigated by partial index users_rating WHERE NOT is_guest).
Stockfish AI Practice Single-player chess against client-side Stockfish 19 WASM across 8 difficulty tiers. Computer.tsx, lib/engine.ts None (Client-Side Only) None None Web Worker (postMessage) Browser Web Worker Memory IMPLEMENTED Main thread UI freezing (mitigated by dedicated Web Worker isolation).

PART 2: COMPLETE ARCHITECTURE

2.1 High-Level Architecture Diagram

graph TB
    subgraph Clients["Client Layer (Browsers)"]
        BrowserA["Player A (Vercel SPA)"]
        BrowserB["Player B (Vercel SPA)"]
        BrowserSpec["Spectator Browser"]
    end

    subgraph Edge["Cloudflare Anycast Network"]
        CFDNS["Cloudflare DNS"]
        WAF["Cloudflare WAF & DDoS Shield"]
        EdgeWorker["Edge Worker Gateway (worker/index.ts)"]
        EdgeCORS["0ms CORS Preflight"]
        WSRelay["WebSocketPair Bidirectional Relay"]
    end

    subgraph Cluster["Backend Container Cluster (Node.js 24)"]
        Instance1["Server Instance 1 (Owner of Game G1)"]
        Instance2["Server Instance 2 (Owner of Game G2)"]
    end

    subgraph DataTier["State & Storage Tier"]
        RedisCluster[("Managed Redis 7 (Upstash / TLS)
        - Atomic Lua Queues
        - Distributed Leases
        - Active Snapshots
        - Cross-Instance Pub/Sub")]
        PostgresDB[("Neon Serverless PostgreSQL
        - Users & Credentials
        - Sessions & Identities
        - Finished Games (JSONB)
        - Elo Ratings")]
    end

    BrowserA -->|"HTTPS / WSS"| CFDNS
    BrowserB -->|"HTTPS / WSS"| CFDNS
    BrowserSpec -->|"HTTPS / WSS"| CFDNS
    CFDNS --> WAF
    WAF --> EdgeWorker
    EdgeWorker --> EdgeCORS
    EdgeWorker --> WSRelay

    WSRelay -->|"TCP Proxy (User A)"| Instance1
    WSRelay -->|"TCP Proxy (User B)"| Instance2
    WSRelay -->|"TCP Proxy (Spectator)"| Instance2

    Instance1 <-->|"Pub/Sub ch:user:{B}"| RedisCluster
    Instance2 <-->|"Pub/Sub ch:user:{A}"| RedisCluster
    Instance1 <-->|"Lease / Snapshot game:{G1}"| RedisCluster
    Instance2 <-->|"Pub/Sub ch:watch:{G1}"| RedisCluster

    Instance1 -->|"Idempotent SQL Transaction"| PostgresDB
    Instance2 -->|"Idempotent SQL Transaction"| PostgresDB
Loading

2.2 Component Architecture & Failure Profiles

1. Frontend SPA (frontend/)

  • Technology: React 19, TypeScript, Vite, Tailwind CSS v4, Lucide Icons, Chess.js.
  • Responsibility: User interaction, optimistic board rendering, piece drag-and-drop, sound effects, client-side clock interpolation, Web Worker UCI bridge for Stockfish.
  • Statefulness: Purely client-side state. Holds local copies of active games received via WebSocket snapshots.
  • Scaling: Static assets served globally via Vercel Edge CDN. Unlimited horizontal scalability.

2. Edge Gateway (worker/index.ts)

  • Technology: Cloudflare Workers runtime (V8 Isolate).
  • Responsibility: Global Anycast ingress, SSL/TLS termination, 0ms CORS preflight resolution (OPTIONS), transparent duplex WebSocket proxying via WebSocketPair, client IP attribution (CF-Connecting-IP).
  • Statefulness: Completely stateless.
  • Scaling: Scales automatically across Cloudflare's 330+ global edge locations.

3. Backend Core (backend1/src/)

  • Technology: Node.js 24 Alpine, TypeScript (native execution), node:http, ws WebSocketServer, ioredis, pg (node-postgres), jose, scrypt.
  • Responsibility: Authoritative chess move validation, turn clocks and flagging timers, distributed game ownership leases, matchmaking queues, session authentication, and transactional persistence.
  • Statefulness: Stateful in-memory game ownership. Each active game is managed by exactly one server instance at a time to prevent conflicting clocks. However, because snapshots are committed to Redis on every move, instance state is ephemeral.
  • Scaling: Horizontally scalable across multiple instances behind a load balancer using Redis Pub/Sub and distributed leases.

4. Distributed Broker (backend1/src/redis.ts)

  • Technology: Redis 7 (TLS rediss://).
  • Responsibility: Atomic matchmaking lists (queue:{tc}), atomic room code consumption (room:{code}), game clock ownership leases (game:{gid}:owner), active game serialized snapshots (game:{gid}), spectator live index (live ZSet), cluster pub/sub channels (ch:user:{uid}, ch:watch:{gid}, ch:instance:{iid}).
  • Statefulness: Highly stateful in-memory data store with millisecond TTLs.
  • Single Point of Failure (SPOF): YES. If Redis becomes unavailable, active games stall, matchmaking halts, and cross-instance communication collapses.

5. Relational Database (backend1/src/db.ts)

  • Technology: Neon Serverless PostgreSQL 16.
  • Responsibility: Long-term durable storage of registered users, scrypt password hashes, revoked session audit logs, federated Google identities, password reset tokens, completed game records (with full JSONB move trees), and Elo ratings.
  • Statefulness: Strongly consistent, ACID-compliant relational storage.
  • Scaling: Vertical compute scaling with serverless autoscaling and connection pooling via Cloudflare Hyperdrive or Neon connection pooler.

2.3 Deployment Architecture vs. Application Architecture

Dimension Application Architecture Design Currently Configured Deployment
Frontend Hosting Edge-hosted static Single Page Application Deployed on Vercel (https://gambitchess.vercel.app) with root vercel.json routing.
Edge Gateway Global Anycast reverse proxy and WebSocket relay Configured in Cloudflare Workers (worker/index.ts + wrangler.jsonc).
Backend Compute Multi-instance containerized Node.js cluster Containerized via backend1/Dockerfile on Railway (live fallback) and Cloudflare Containers / Container Host.
Database Serverless PostgreSQL with advisory locks Hosted on Neon PostgreSQL AWS us-east-2.
Redis Broker High-availability Redis 7 cluster Provisioned on Upstash Redis (Cloudflare target) / Railway internal Redis.

2.4 Dependency Graph & Single Points of Failure (SPOF)

graph LR
    Client["Browser Client"] --> Gateway["Edge Gateway"]
    Gateway --> Backend["Node.js Backend"]
    Backend --> Redis["Redis 7"]
    Backend --> Postgres["Neon PostgreSQL"]

    classDef spof fill:#ff4d4d,stroke:#990000,color:white;
    classDef soft fill:#ffa64d,stroke:#cc5200,color:black;
    class Redis spof;
    class Postgres soft;
Loading

Critical SPOF Analysis:

  1. Redis 7 (CRITICAL SPOF): If Redis crashes, the backend cannot enqueue players, cannot claim leases, cannot publish move updates, and cannot authenticate WebSockets (due to single-use ticket verification in Redis). The backend will reject all incoming WebSocket connections.
  2. Neon PostgreSQL (DEGRADED SPOF): If PostgreSQL is unreachable, logins and registrations fail, and completed games cannot be archived. However, already active chess games continue running in memory and Redis until they finish and attempt to persist.
  3. Edge Worker Gateway (HIGH RESILIENCE): Runs in hundreds of Cloudflare data centers; resilient to regional outages.

PART 3: FRONTEND ENGINEERING DEEP DIVE

3.1 Application Initialization & Component Tree

The application initializes in frontend/src/main.tsx by mounting App into the DOM. The routing hierarchy is defined in frontend/src/App.tsx:

graph TD
    App["App.tsx (Root Router)"] --> AuthProv["AuthContext.Provider"]
    AuthProv --> SettingsProv["SettingsContext.Provider"]
    SettingsProv --> OnlineProv["OnlineContext.Provider"]
    OnlineProv --> Layout["Header & Navigation Shell"]
    Layout --> Routes["React Router Routes"]

    Routes --> Home["/ (Home & Lobby)"]
    Routes --> LiveGame["/play/:id (Live Chess Arena)"]
    Routes --> Join["/join/:code (Private Invite Room)"]
    Routes --> Watch["/watch (Spectator Directory)"]
    Routes --> Computer["/computer (Stockfish Practice)"]
    Routes --> Analysis["/analysis (Post-Game Board Analysis)"]
    Routes --> Leaderboard["/leaderboard (Ratings Leaderboard)"]
    Routes --> Profile["/user/:username (User History)"]
    Routes --> AuthPage["/login & /signup"]
    Routes --> ResetPw["/reset-password"]
Loading

3.2 State Management Taxonomy

Gambit divides state into four distinct architectural tiers:

State Tier Examples in Gambit Storage Mechanism Lifecycle & Authority
1. Local UI State Board flip status, active chat tab, piece drag offset, sound volume, selected board theme. React useState & sessionStorage (gambit_chat_${id}) Ephemeral to the tab. Destroyed on tab close. Client-authoritative.
2. Server-Authoritative State Legal piece positions (FEN), clock milliseconds remaining, game status (active/finished), move ply count, player draw offers. Node.js Game.ts in-memory mutated state, backed by Redis snapshot Server is absolute authority. Client receives game_state or move and overrides local state.
3. Optimistic Transient State The move a player just made before the server echoes confirmation. OnlineContext.tsx:L286 (patchGame) Rendered locally for 0ms visual feedback. If server rejects (move_rejected), local move is rolled back.
4. Persistent Identity State User ID, username, rating, active JWT bearer token. localStorage.getItem('gambit.token') & Neon PostgreSQL Survives page reloads and browser restarts.

3.3 The API Client & WebSocket Transport Layer

  • REST Client (frontend/src/lib/api.ts):
    Uses native fetch with API_BASE pointing to VITE_API_URL. Every request automatically injects Authorization: Bearer <token> from localStorage (api.ts:L37).
  • WebSocket Ticket Pattern (api.ts:L49-L52):
    Before opening a WebSocket, the client makes a secure HTTP POST:
    export async function getWsTicket(): Promise<string> {
      const res = await api<{ ticket: string; expiresIn: number }>('/auth/ticket', { method: 'POST' })
      return res.ticket
    }
    The returned single-use ticket is passed in the query string: wss://.../ws?ticket=<TICKET>. This prevents long-lived JWT tokens from leaking into web server access logs or proxy headers.

3.4 Deep Trace: What Happens When a Player Refreshes During an Active Game?

sequenceDiagram
    autonumber
    participant Browser as Player Browser (Tab)
    participant Edge as Cloudflare Worker Edge
    participant Server as Node.js Backend Server
    participant Redis as Redis Cluster

    Note over Browser: Player presses F5 (Hard Refresh)
    Browser->>Browser: React tree unmounts. WebSocket connection terminates.
    Server->>Server: WebSocket 'close' event fires on backend.
    Server->>Redis: Arms 60s abandonment timer: abandonAt = Date.now() + 60000
    Server->>Redis: Publishes presence(connected: false) to opponent

    Note over Browser: Browser downloads index.html, JS chunks (from CDN)
    Browser->>Browser: main.tsx mounts App -> AuthContext reads token from localStorage
    Browser->>Server: GET /api/me (Auth verification)
    Server-->>Browser: HTTP 200 OK (User metadata restored)
    Browser->>Server: POST /api/auth/ticket
    Server->>Redis: SET ticket:uuid { userId, sessionId } EX 30
    Server-->>Browser: HTTP 200 { ticket: "uuid" }

    Browser->>Edge: WSS /ws?ticket=uuid
    Edge->>Server: WebSocket Handshake forward
    Server->>Redis: GETDEL ticket:uuid (Atomic consumption)
    Server-->>Browser: HTTP 101 Switching Protocols (WS Connected)

    Browser->>Browser: LiveGame.tsx mounts with route /play/:gameId
    Browser->>Server: WS Send: { type: "spectate", gameId: "game-123" }
    Server->>Redis: games.load("game-123") -> Deserializes GameSnapshot
    Server->>Server: Detects user is White player -> cancels abandonment timer
    Server-->>Browser: WS Emit: { type: "game_state", game: GameView }
    Browser->>Browser: OnlineContext receives game_state -> updates games state
    Browser->>Browser: Board.tsx re-renders board at exact FEN. Clocks resume ticking.
Loading

Exact Code Trace:

  1. Unmount & Disconnect (OnlineContext.tsx:L263):
    The browser unmounts OnlineProvider, triggering cleanup: wsRef.current?.close().
  2. Abandonment Armed (Game.ts:L375-L378):
    The server marks this.s.connected[c] = false, calculates abandonAt = Date.now() + config.abandonTimeoutMs, and schedules a 60-second Node.js timer:
    this.abandonTimers[c] = setTimeout(() => {
      if (this.s.status !== "active" || this.s.connected[c]) return;
      if (this.s.moves.length < 2) this.finish(null, "aborted");
      else this.finish(other(c), "abandonment");
    }, delayMs);
  3. Ticket Negotiation & Reconnection (OnlineContext.tsx:L219-L229):
    The new page instance calls getWsTicket(), receives a single-use UUID, and connects to /ws?ticket=uuid.
  4. Resubscription (LiveGame.tsx:L37 & OnlineContext.tsx:L237):
    On WebSocket open, OnlineContext iterates all active games and sends { type: "spectate", gameId: g.id }.
  5. State Restoration & Timer Disarm (GameManager.ts:L240-L243 & Game.ts:L369-L373):
    The backend recognizes the user's socket, clears the abandon timeout (clearTimeout(this.abandonTimers[c])), and emits game_state with accurate clock values: clocks: clocksOf(this.s, now).
  6. Local Clock Resumption (OnlineContext.tsx:L47):
    OnlineContext sets clocksAt: performance.now(). The frontend hook useNow(250) ticks the clock down locally on the client screen without waiting for periodic network packets.

3.5 Deep Trace: What Happens When the Same Account Opens in Two Browser Tabs?

  1. Tab 1 is already in an active game.
  2. Tab 2 opens: AuthContext reads the identical JWT token from localStorage.
  3. Tab 2 requests WS Ticket: Calls POST /api/auth/ticket, obtains a new ticket, and connects to /ws.
  4. Backend Socket Attachment (GameManager.ts:L95-L113):
    let set = this.sockets.get(userId);
    if (!set) {
      set = new Set();
      this.sockets.set(userId, set);
      ...
      await bus.on(CH.user(userId), forward);
    }
    set.add(ws);
    The backend maintains a Set<WebSocket> per user ID.
  5. Fanout Delivery: When a move or chat arrives, the backend executes:
    for (const s of set) s.send(raw);
    Result: Both Tab 1 and Tab 2 receive the exact same game events simultaneously. If the player moves a piece in Tab 1, Tab 2's chessboard moves instantly. The two tabs do not disconnect or corrupt each other.

PART 4: AUTHENTICATION AND USER LIFECYCLE

4.1 Registration & Password Hashing Architecture

Gambit implements password hashing using Node.js's native node:crypto scrypt implementation (backend1/src/auth.ts:L8-L20):

const scryptAsync = promisify(scrypt) as (pw: string, salt: Buffer, len: number) => Promise<Buffer>;
const KEY_LEN = 64;

export async function hashPassword(password: string): Promise<string> {
  const salt = randomBytes(16);
  const key = await scryptAsync(password, salt, KEY_LEN);
  return `scrypt$${salt.toString("hex")}$${key.toString("hex")}`;
}

Cryptographic Properties:

  • Salt: 16 cryptographically secure random bytes generated via randomBytes(16).
  • Key Length: 64 bytes (512 bits).
  • Format: scrypt$<salt_hex>$<derived_key_hex> stored in PostgreSQL users.password_hash.
  • Verification: Extracts salt and expected key, executes scrypt against supplied password, and evaluates timingSafeEqual(actual, expected) to prevent timing side-channel attacks (auth.ts:L19).

4.2 Token Generation, Validation & Session Lifecycle

sequenceDiagram
    autonumber
    participant Client as Frontend Client
    participant HTTP as Backend HTTP Handler
    participant DB as PostgreSQL Database
    participant Redis as Redis Cluster

    Client->>HTTP: POST /api/auth/login { username, password }
    HTTP->>DB: SELECT id, password_hash FROM users WHERE lower(username) = lower($1)
    DB-->>HTTP: User record
    HTTP->>HTTP: verifyPassword(password, hash) -> True
    HTTP->>DB: INSERT INTO sessions (id, user_id, expires_at, ip, user_agent) VALUES (...)
    DB-->>HTTP: Session record created
    HTTP->>HTTP: issueToken(userId, sessionId) -> base64url(payload).hmac_sha256
    HTTP-->>Client: HTTP 200 { user, token }

    Note over Client,HTTP: User requests WebSocket connection
    Client->>HTTP: POST /api/auth/ticket (Header: Bearer <token>)
    HTTP->>HTTP: verifyToken(token) -> Checks signature & expiration
    HTTP->>DB: isSessionActive(sessionId) -> Checks revoked_at IS NULL
    HTTP->>Redis: SET ticket:<uuid> { userId, sessionId } EX 30
    HTTP-->>Client: HTTP 200 { ticket: "<uuid>", expiresIn: 30 }

    Client->>HTTP: WSS /ws?ticket=<uuid>
    HTTP->>Redis: GETDEL ticket:<uuid>
    Redis-->>HTTP: { userId, sessionId }
    HTTP-->>Client: 101 Switching Protocols
Loading

Token Structure (auth.ts:L31-L38):

Gambit uses compact, self-contained signed tokens: $$\text{Token} = \text{Base64URL}(\text{JSON}({ \text{sub: userId}, \text{sid: sessionId}, \text{exp: timestamp} })) ,., \text{HMAC-SHA256}(\text{body}, \text{AUTH_SECRET})$$

Invalidation Mechanics:

  1. Single Session Logout (POST /api/auth/logout): Updates sessions.revoked_at = Date.now() where id = sessionId. Publishes { type: "session_revoked", sessionId } on ch:user:{userId}.
  2. All Session Logout (POST /api/auth/logout-all): Updates all sessions for userId with revoked_at = Date.now(). Disconnects all open WebSockets for that user across the cluster (GameManager.ts:L91-L93).

PART 5: MATCHMAKING AND GAME CREATION

5.1 Matchmaking Protocol & Queue Data Structures

Gambit supports 6 official time controls (protocol.ts:L10): 1+0 (Bullet), 3+0 (Blitz), 3+2 (Blitz), 5+0 (Blitz), 5+3 (Rapid), 10+0 (Rapid).

Matchmaking is managed through atomic Redis lists (backend1/src/redis.ts:L79-L87):

-- Custom Redis Lua Script: matchPop
-- KEYS[1] = queue key (e.g., gambit:queue:5+3)
-- ARGV[1] = user ID (e.g., user-123)

redis.call('LREM', KEYS[1], 0, ARGV[1])
local opp = redis.call('LPOP', KEYS[1])
if opp then
    return opp
end
redis.call('RPUSH', KEYS[1], ARGV[1])
return false

Why this Lua Script is Mathematically Race-Condition Free:

  1. Redis Single-Threaded Execution: Redis executes Lua scripts as an atomic transaction. No other command can read or write queue:{tc} while matchPop executes.
  2. Self-Deduplication: LREM KEYS[1] 0 ARGV[1] removes any pre-existing instances of the player in that queue.
  3. Atomic Pop or Enqueue: LPOP checks if an opponent is already waiting. If yes, it removes and returns the opponent. If no, RPUSH appends the player to the tail and returns false. Two players requesting a match at the exact same millisecond will never both enqueue and will never both fail.

5.2 Game Creation & Color Assignment

When matchPop returns an opponent ID (GameManager.ts:L452-L476):

  1. Queue Cleanup: Both players are removed from user:{uid}:queue.
  2. Color Assignment: randomInt(2) === 0 ? "white" : "black". White is randomly assigned to either Player A or Player B with exactly 50% probability.
  3. Game Initialization: A new Game object is created with random UUID. Initial clock milliseconds and increments are configured.
  4. Distributed Lease Claim: The creating instance acquires a Redis lease on game:{gid}:owner via SET NX PX 15000.
  5. Simultaneous Fanout: The server pushes game_state to ch:user:{whiteId} and ch:user:{blackId} simultaneously via Redis Pub/Sub.

5.3 Deep Failure Analysis: Matchmaking

sequenceDiagram
    participant P1 as Player 1
    participant P2 as Player 2
    participant S1 as Server Instance 1
    participant Redis as Redis Cluster

    P1->>S1: WS { type: "init_game", tc: "5+3" }
    S1->>Redis: EVAL matchPop(queue:5+3, P1)
    Redis-->>S1: false (P1 Enqueued at tail)
    S1-->>P1: WS { type: "lobby", queuedFor: "5+3" }

    Note over P1,S1: Failure Case 1: P1 closes laptop / loses Wi-Fi
    S1->>S1: Socket disconnect event fires
    S1->>Redis: queue.leave(P1) -> LREM queue:5+3 P1 & DEL user:P1:queue

    Note over P2,Redis: Failure Case 2: P2 enters queue before cleanup finished
    P2->>S1: WS { type: "init_game", tc: "5+3" }
    S1->>Redis: EVAL matchPop(queue:5+3, P2)
    Redis-->>S1: Returns P1
    S1->>Redis: Check presence.isOnline(P1)
    alt P1 is confirmed offline
        S1->>Redis: Match aborted. S1 re-enqueues P2 automatically.
        S1-->>P2: WS { type: "lobby", queuedFor: "5+3" }
    else P1 is online
        S1->>S1: Game created successfully!
    end
Loading

PART 6: INVITATION LINKS AND PRIVATE ROOMS

6.1 Private Room Lifecycle & Identifier Generation

Private rooms allow two users to start a game without entering public matchmaking (backend1/src/redis.ts:L225-L264).

  1. Room Creation: Player A clicks "Play a friend" and selects color and time control.
  2. Code Generation (redis.ts:L235):
    const ROOM_ALPHABET = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; // 32 chars (no ambiguous 0, O, 1, I)
    const code = Array.from({ length: 6 }, () => ROOM_ALPHABET[randomInt(ROOM_ALPHABET.length)]).join("");
    • Entropy: $32^6 = 1,073,741,824$ unique room codes.
  3. Storage: Stored in Redis under gambit:room:{code}:
    { "code": "K7N9XP", "hostId": "user-a", "tc": "5+3", "color": "white" }
    • TTL: 3,600,000 ms (1 hour) set via SET PX 3600000 NX.
  4. Atomic Join (redis.ts:L246): When Player B navigates to /join/K7N9XP, the backend executes:
    const raw = await redis.getdel(K.room(code));
    Why GETDEL is Crucial: GETDEL atomically returns the room data and deletes the key in one single operation. If two people click the same invite link simultaneously, exactly one will receive the room data; the other receives null and is shown: "This invite has expired or was already joined."

PART 7: REAL-TIME COMMUNICATION AND WEBSOCKETS

7.1 Connection Handshake & Authentication Gate

Gambit strictly enforces Pre-Upgrade Authentication in backend1/src/index.ts:L18-L80:

sequenceDiagram
    autonumber
    participant Client as Browser
    participant HTTP as Node.js HTTP Server
    participant Redis as Redis
    participant DB as PostgreSQL
    participant WSS as WebSocketServer

    Client->>HTTP: GET /ws?ticket=<ticket_uuid> (Upgrade: websocket)
    HTTP->>HTTP: Validate url.pathname === "/ws"
    
    alt Missing or Malformed Ticket
        HTTP-->>Client: HTTP/1.1 401 Unauthorized (Connection: close)
    end

    HTTP->>Redis: GETDEL ticket:<ticket_uuid>
    alt Ticket Not Found or Expired (>30s)
        Redis-->>HTTP: null
        HTTP-->>Client: HTTP/1.1 401 Unauthorized (Connection: close)
    end

    Redis-->>HTTP: { userId: "u123", sessionId: "s456" }
    HTTP->>DB: isSessionActive("s456")
    alt Session Revoked
        DB-->>HTTP: false
        HTTP-->>Client: HTTP/1.1 401 Unauthorized (Connection: close)
    end

    DB-->>HTTP: true
    HTTP->>WSS: wss.handleUpgrade(req, socket, head)
    WSS-->>Client: HTTP/1.1 101 Switching Protocols
    WSS->>WSS: Start 25s ping/pong keepalive timer
Loading

Anonymous or unauthenticated TCP connections are terminated at HTTP layer before the WebSocket protocol upgrade completes, completely shielding the game engine from socket exhaustion attacks.

7.2 WebSocket Protocol Event Catalog

The following catalog defines every wire frame exchanged over the WebSocket connection:

Client-to-Server Events (ClientMessage in protocol.ts:L54-L96)

Event Name Payload Structure Validation Rules Backend Handler State Change Broadcast Action
ping { type: "ping", t: number } Valid JSON GameManager.ts:L187 None Echoes { type: "pong", t } to sender.
init_game { type: "init_game", tc: string } Valid time control ID GameManager.ts:L189 Adds user to queue:{tc} Sends { type: "lobby", queuedFor: tc }.
cancel_queue { type: "cancel_queue" } None GameManager.ts:L193 Removes user from queue Sends { type: "lobby", queuedFor: null }.
create_room { type: "create_room", tc: string, color: Color | "random" } Valid TC and color GameManager.ts:L196 Creates room:{code} Sends { type: "lobby", room: Room }.
join_room { type: "join_room", code: string } 6 alphanumeric chars GameManager.ts:L200 Consumes room via GETDEL Starts game; emits game_state to both players.
move { type: "move", gameId: string, move: { from, to, promotion } } Valid algebraic square notation Game.ts:L244 Executes move in chess.js, decrements clock, appends increment Emits move event to players + spectators; if checkmate, finishes game.
resign { type: "resign", gameId: string } Active game & player member Game.ts:L320 Sets status: "finished", opponent wins Emits game_over to players + spectators; persists to DB.
draw_offer { type: "draw_offer", gameId: string } Active game & player member Game.ts:L333 Sets drawOfferFrom: color Emits draw_offer to opponent.
draw_respond { type: "draw_respond", gameId: string, accept: boolean } Active game & pending offer Game.ts:L340 If accept, finishes as draw; if reject, clears offer Emits game_over (draw) or draw_offer (declined).
chat { type: "chat", gameId: string, text: string } String length 1–200 Game.ts:L348 Appends line to game chat array (max 50) Emits chat to players + spectators.
spectate { type: "spectate", gameId: string } Valid UUID GameManager.ts:L205 Adds socket to watching, joins ch:watch:{gid} Sends immediate game_state snapshot to spectator.
unspectate { type: "unspectate", gameId: string } Valid UUID GameManager.ts:L208 Leaves spectator channel None.

PART 8: CHAT SYSTEM & CRITICAL BUG INVESTIGATION

8.1 Chat Pipeline & Message Ordering

  1. Submission: Player submits text via Chat.tsx.
  2. Rate Limiting: Guarded by token bucket rate limiter in GameManager.ts:L35 (12 tokens/sec, max 20 strikes before socket termination).
  3. Storage: Chat is appended to GameSnapshot.chat (Game.ts:L352). Messages are capped at the latest 50 entries using .slice(-MAX_CHAT_HISTORY).
  4. Ordering: Messages receive an authoritative server timestamp (ts: Date.now()). Ordering is guaranteed by the serialized outbox pipeline in GameManager.ts:L310-L315.

8.2 Forensic Investigation of the Reported Bug: "Private Invite Chat Causes Game Screen to Disappear"

Observed Symptom:

When Player B joins a private invited game via /join/:code and sends a chat message, the entire game screen unmounts, disappearing or redirecting back to the lobby.

Code Path Tracing & Root Cause Discovery:

flowchart TD
    JoinPage["Join.tsx (/join/:code)"] -->|"WS join_room"| Server["Backend Server"]
    Server -->|"WS game_state"| OnlineCtx["OnlineContext.tsx"]
    OnlineCtx -->|"navigate('/play/' + game.id)"| Router["React Router"]
    
    subgraph Defect1["Defect 1: History Stack Corruption"]
        Router -->|"push history instead of replace"| History["History Stack contains /join/:code"]
    end
    
    subgraph Defect2["Defect 2: Chat State Reset in LiveGame"]
        ChatSend["Player sends Chat Message"] -->|"chat event emitted"| Server
        Server -->|"WS echo chat"| OnlineCtx
        OnlineCtx -->|"patchGame updates games dictionary"| StateUpdate["games state object reference changes"]
        StateUpdate -->|"LiveGame re-evaluates"| LiveGameMount["LiveGame component re-renders"]
        LiveGameMount -->|"chatOpen reset to default false"| ChatCloses["Chat drawer vanishes!"]
    end

    subgraph Defect3["Defect 3: Missing Chat Array Unhandled TypeError"]
        MissingChat["Snapshot lacks chat property"] -->|"game.chat.map(...) in Chat.tsx"| TypeError["TypeError: Cannot read properties of undefined (reading 'map')"]
        TypeError -->|"Uncaught in React Tree"| WhiteScreen["React unmounts entire DOM tree (White Screen of Death)"]
    end
Loading

Confirmed Findings:

  1. The React White Screen Crash (TypeError):
    In frontend/src/context/OnlineContext.tsx:L46, early versions merged game_state using { ...prev, ...game }. When a game was restored from snapshots that lacked a chat array, game.chat was undefined. When Chat.tsx executed game.chat.map(...), JavaScript threw an uncaught TypeError. In React 19, an uncaught error in a component tree unmounts the entire application root, causing the screen to instantly disappear.
  2. Navigation Loop from History Stack:
    In Join.tsx, the player joined via /join/:code. When game_state arrived, navigate('/play/' + id) was called with default history push (OnlineContext.tsx:L109). Sending a chat message or suffering a micro-reconnect re-evaluated router conditions; because /join/:code remained in browser history, the component remounted /join/:code, which attempted to re-join an already-consumed room, failed, and redirected to /.

The Complete Engineering Fix Applied:

  • Added defensive array fallback: chat: Array.isArray(game.chat) ? game.chat : [] (OnlineContext.tsx:L46).
  • Changed navigation to { replace: pathRef.current.startsWith('/join/') } (OnlineContext.tsx:L109).
  • Persisted chat drawer open state in sessionStorage.getItem('gambit_chat_${game.id}') (LiveGame.tsx:L89).
  • Wrapped LiveGame in a top-level ErrorBoundary to prevent tree unmounting.

PART 9: CHESS ENGINE AND GAME LOGIC

9.1 Execution Boundary: Client vs. Server

  • Server: Absolute authority. Executes chess.js (backend1/src/Game.ts:L168). Maintains FEN string, move history, clocks, and draw claims.
  • Client: Optimistic replica. Executes chess.js locally to compute legal move highlights, drag-and-drop piece constraints, and audio triggers (soundForSan).

9.2 Stockfish 19 AI Integration

Gambit integrates Stockfish 19 Lite Single-Threaded WASM (frontend/src/lib/engine.ts):

  • Worker Isolation: Initialized as a dedicated Web Worker via new Worker('/engine/stockfish-19-lite-single.js') (engine.ts:L49). It never runs on the main JavaScript thread, preventing UI stutter or dropped frames during heavy calculations.
  • UCI Protocol Communication:
    1. this.worker.postMessage('uci') -> Waits for uciok.
    2. this.worker.postMessage('isready') -> Waits for readyok.
    3. this.worker.postMessage('setoption name Skill Level value ' + level.skill).
    4. this.worker.postMessage('position fen ' + fen).
    5. this.worker.postMessage('go ' + level.go) -> Parses bestmove <move> and info depth <d> score cp <cp> pv <moves>.
  • Backend Invocation of Stockfish: NONE. The backend never invokes Stockfish. Computer practice is 100% client-side, saving significant server CPU and operational cost.

PART 10: DATABASE AND DATA MODEL

10.1 PostgreSQL Relational Schema & ER Diagram

erDiagram
    users ||--o{ sessions : "has many"
    users ||--o{ provider_identities : "has many"
    users ||--o{ password_reset_tokens : "has many"
    users ||--o{ games : "plays as white"
    users ||--o{ games : "plays as black"

    users {
        text id PK
        text username
        text email
        text password_hash
        boolean is_guest
        integer rating
        integer games
        integer wins
        integer losses
        integer draws
        bigint created_at
    }

    sessions {
        text id PK
        text user_id FK
        bigint created_at
        bigint expires_at
        bigint revoked_at
        text ip
        text user_agent
    }

    provider_identities {
        text id PK
        text user_id FK
        text provider
        text provider_user_id
        text email
        boolean email_verified
        bigint created_at
        bigint last_login_at
    }

    password_reset_tokens {
        text id PK
        text user_id FK
        text token_hash UK
        bigint expires_at
        bigint used_at
        bigint created_at
    }

    games {
        text id PK
        text white_id FK
        text black_id FK
        integer white_rating
        integer black_rating
        integer white_delta
        integer black_delta
        text tc
        text result
        text reason
        text pgn
        jsonb moves
        bigint started_at
        bigint ended_at
    }
Loading

10.2 Concurrency & Transactional Idempotency

1. Database Schema Migrations (backend1/src/db.ts:L166-L183)

Migrations use a transaction-scoped PostgreSQL advisory lock:

BEGIN;
SELECT pg_advisory_xact_lock(727274);
-- DDL schema definitions...
COMMIT;

Guarantees that when 10 container instances boot at the same moment, exactly one executes migrations while the other 9 wait, with zero table lock deadlocks.

2. Rating Application Idempotency (db.ts:L283-L312)

BEGIN;
INSERT INTO games (id, white_id, black_id, white_rating, black_rating, white_delta, black_delta, tc, result, reason, pgn, moves, started_at, ended_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
ON CONFLICT (id) DO NOTHING RETURNING id;

-- If inserted.rowCount === 0, ROLLBACK and return false (ratings are NOT updated twice!)
UPDATE users SET rating = GREATEST(100, rating + $1), games = games + 1, ... WHERE id = $white_id;
UPDATE users SET rating = GREATEST(100, rating + $2), games = games + 1, ... WHERE id = $black_id;
COMMIT;

If an instance crashes during game over and a standby instance takes over and resaves the game, the conflict on games(id) prevents duplicate Elo inflation.


PART 11: REDIS AND DISTRIBUTED STATE

11.1 Master Redis Key Catalog

Every key in Redis is partitioned by config.redisPrefix (gambit:):

Key Pattern Redis Type Writer Reader TTL Consistency Failure Impact
gambit:instances Set Heartbeat (presence.ts) Cluster discovery None Eventual Crashed instances lingering until pruned.
gambit:instance:{iid} String Instance heartbeat Peer instances 15s (LEASE_MS) Strong Peer assumes instance died if missing.
gambit:online:{iid} Set Socket connect Stats & Presence Instance lifetime Strong Discrepancy in live player count.
gambit:queue:{tc} List matchPop Lua script Matchmaker Indefinite Strong Players stuck in queue if Redis drops.
gambit:user:{uid}:queue String queue.match Matchmaker 6 hours Strong Player unable to re-queue.
gambit:user:{uid}:room String rooms.create Invite system 1 hour Strong Host cannot cancel ghost room.
gambit:user:{uid}:game String games.commit Active reconnection Game TTL Strong Reconnecting user cannot find game ID.
gambit:room:{code} String (JSON) rooms.create rooms.take (GETDEL) 1 hour Strong Link invalidation or code collision.
gambit:game:{gid} String (JSON) games.commit games.load (Takeover) 6h active / 15m done Strong Total loss of in-flight game if Redis wiped.
gambit:game:{gid}:owner String games.claim (SET NX) Clock Engine 15s (LEASE_MS) Strong Clocks stop or duel clock split-brain.
gambit:game:{gid}:watchers Set games.watch Spectator stream 6h active / 15m done Eventual Spectator count inaccurate.
gambit:live Sorted Set games.commit (ZADD) GET /api/games/live Active game duration Eventual Game disappears from live directory.
gambit:ticket:{t} String POST /api/auth/ticket WS Upgrade (GETDEL) 30 seconds Strong WebSocket connection 401s if expired.
gambit:sess:rev:{sid} String revokeSession WS Gate check Session TTL Strong Revoked session may reconnect.
gambit:oauth:state:{s} String OAuth init OAuth callback (GETDEL) 10 minutes Strong Google login rejects state.
gambit:rl:{bucket}:{id} String (Int) rateLimited API Gate Window ms Strong Rate limit counter reset on flush.

PART 12: STATE MANAGEMENT AND CONSISTENCY

12.1 Master State Ownership Table

State Entity Browser Memory Node.js Server Memory Redis Cache / Broker PostgreSQL Database Authoritative Source
User Identity & Passwords Token only None None Full Record PostgreSQL (users)
Active Session Validity None None sess:rev:{sid} sessions table PostgreSQL (sessions)
Matchmaking Queues Optimistic None queue:{tc} List None Redis 7
Private Room Invites None None room:{code} String None Redis 7
Active Game Chessboard (FEN) Mirrored Active Game.chess game:{gid} Snapshot None Owning Node.js Instance
Active Game Clocks Extrapolated Active Timers (setInterval) turnStartedAt Epoch None Owning Node.js Instance
Game Clock Ownership None owned: Map<gid, Game> game:{gid}:owner (Lease) None Redis Distributed Lease
In-Game Chat History Rendered Array Game.chat Array game:{gid} Snapshot None Owning Node.js Instance
Completed Game History Read-only None 15-minute eviction games (JSONB moves) PostgreSQL (games)
Elo Ratings Cached Profile None None users.rating PostgreSQL (users)

PART 14: SCALABILITY AND CAPACITY ENGINEERING

Workload Scaling Projections

Metric / Resource Small (100 Active Users) Medium (1,000 Active Users) Large (10,000 Active Users) Very Large (100,000 Active Users)
Concurrent WebSockets 100 1,000 10,000 100,000
Active Games ~40 ~400 ~4,000 ~40,000
Moves / Second ~15 moves/sec ~150 moves/sec ~1,500 moves/sec ~15,000 moves/sec
Redis Commands / Sec ~100 cmd/sec ~1,000 cmd/sec ~12,000 cmd/sec ~120,000 cmd/sec
Backend Memory 150 MB (1 instance) 500 MB (1–2 instances) 3 GB (4–6 instances) 30 GB (30–40 instances)
PostgreSQL Write Load 0.2 saves/sec 2 saves/sec 20 saves/sec 200 saves/sec
Primary Bottleneck None Single-node CPU Redis Pub/Sub CPU & Socket Limits Database Connection Pool & Locks

PART 15: SECURITY AND THREAT MODEL

  • SQL Injection: Zero Risk. All database queries use parameterized SQL ($1, $2, ...) via pg.Pool. No string interpolation exists in SQL paths.
  • XSS: Mitigated. React automatically encodes strings rendered in JSX. Chat text is rendered as plain string children.
  • CSRF: Not Applicable. Authentication relies entirely on Authorization: Bearer <JWT> headers stored in localStorage, which are immune to cross-site request forgery.
  • Rate Limiting: Enforced cluster-wide in Redis via sliding window counter (KEYS.rate(bucket, id)) on auth endpoints and in-memory token bucket on WebSockets.

PART 16: OBSERVABILITY & PRODUCTION METRICS

Gambit provides structured JSON logging via logger.ts and health probes at GET /api/health and GET /health.

Recommended Production Telemetry Catalog:

  1. gambit_active_games_total: Gauge tracking concurrent live chess games.
  2. gambit_websocket_connections_total: Active WebSocket client connections.
  3. gambit_move_processing_seconds: P50, P95, P99 latency of move validation and Redis commit.
  4. gambit_redis_lease_steal_total: Counter tracking instance crash failovers.
  5. gambit_db_save_game_seconds: Latency of transactional game persistence in Neon.

PART 17: TESTING AND ENGINEERING QUALITY

  • Test Suite: Native Node.js test runner (node --test).
  • Test Results (Verified Live): 27/27 backend tests passed in 323ms. 5/5 worker gateway tests passed in 65ms.
  • Coverage: Chess rules, clock increments, draw offers, resignations, scrypt auth, single-use tickets, Google JWKS, private rooms, chat delivery, cluster failover, and rating idempotency.

PART 18: ARCHITECTURAL WEAKNESSES AND IMPROVEMENTS

Priority Area Finding / Root Cause Consequence Proposed Solution
P0 Distributed Storage Active game chat only exists in Redis snapshots; omitted from PostgreSQL games schema. If Redis is flushed after a game finishes, chat history cannot be reviewed in archives. Add chat JSONB column to PostgreSQL games table in db.ts.
P1 Database Pool Neon scales compute to zero; cold connection can take 3–5 seconds. High latency on first login after idle. Use Cloudflare Hyperdrive connection pooling or Neon warm pooler.
P2 Scalability presence.onlineCount() uses SUNION across all instance sets. CPU spike on Redis when cluster exceeds 50 instances. Switch to Redis HyperLogLog (PFADD / PFCOUNT).
P3 Observability Prometheus metrics scraping endpoint not currently exposed. Reliance on stdout log scraping. Expose /metrics endpoint with OpenTelemetry Prometheus exporter.

: FINAL ARCHITECTURAL SYNTHESIS

Engineering Maturity Assessment (1–5 Scale)

  • Correctness: 5/5 — Move validation, clock decrementing, and Elo calculations strictly match FIDE rules.
  • Consistency: 4.5/5 — Strong consistency via atomic Lua scripts and transaction-scoped advisory locks.
  • Reliability: 4/5 — Resilient multi-instance failover via Redis snapshots.
  • Security: 4.5/5 — Scrypt hashing, pre-upgrade WS ticket gates, parameterized queries, origin CORS matching.
  • Scalability: 4/5 — Stateless edge fronting container cluster; scales to ~10,000 concurrent players before Redis sharding needed.
  • Observability: 3.5/5 — Structured JSON logging present; requires Prometheus metrics exposition.

About

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages