Snake Rush

Production-Grade Multiplayer Game Infrastructure

Overview

Snake Rush is a real-time multiplayer snake game built with modern cloud-native technologies and deployed on production Kubernetes infrastructure. This project demonstrates end-to-end DevOps practices, from containerization and CI/CD pipelines to GitOps deployment and observability.

The platform leverages a microservices architecture with a Go backend handling WebSocket connections for real-time gameplay, Redis for distributed state management across multiple pods, and a React frontend for the user interface. The entire infrastructure is deployed on Civo Cloud, a Kubernetes-native cloud provider, showcasing scalable and resilient system design.

System Architecture Diagram

┌─────────────────────────────────────────────────────────────────────────────────────┐
│                              CI/CD Pipeline & GitOps                                │
├─────────────────────────────────────────────────────────────────────────────────────┤
│                                                                                     │
│  ┌──────────┐    ┌──────────────┐    ┌─────────────┐    ┌──────────────────────┐    │
│  │  GitHub  │───▶│ Docker Build │───▶│   Harbor    │───▶│   ArgoCD (GitOps)    │    │
│  │   Repo   │    │ Multi-stage  │    │  Registry   │    │   Automated Sync     │    │
│  └──────────┘    └──────────────┘    └─────────────┘    └──────────┬───────────┘    │
│                                                                      │              │
└──────────────────────────────────────────────────────────────────────┼──────────────┘
                                                                       │
                                                                       ▼
┌─────────────────────────────────────────────────────────────────────────────────────┐
│                          Civo Cloud - Kubernetes Cluster (K3s)                      │
├─────────────────────────────────────────────────────────────────────────────────────┤
│                                                                                     │
│  ┌────────────────────────────────────────────────────────────────────────────┐     │
│  │                      Traefik Ingress Controller                            │     │
│  │                    (SSL/TLS Termination & Routing)                         │     │
│  └────────────┬──────────────────────────────────────┬────────────────────────┘     │
│               │                                      │                              │
│  ┌────────────▼──────────────┐         ┌─────────────▼──────────────┐               │
│  │   Frontend Namespace      │         │    Backend Namespace       │               │
│  ├───────────────────────────┤         ├────────────────────────────┤               │
│  │                           │         │                            │               │
│  │  ┌─────────────────────┐  │         │  ┌──────────────────────┐  │               │
│  │  │  React + Vite       │  │         │  │   Go WebSocket       │  │               │
│  │  │  (TypeScript)       │  │         │  │   Server (x2)        │  │               │
│  │  │  NGINX Server       │  │         │  │   + Gorilla Mux      │  │               │
│  │  │  Replicas: 2        │  │         │  │   Replicas: 2        │  │               │
│  │  └─────────────────────┘  │         │  └──────────┬───────────┘  │               │
│  │                           │         │             │              │               │
│  │  Service: ClusterIP       │         │  Service: ClusterIP        │               │
│  └───────────┬───────────────┘         └─────────────┼──────────────┘               │
│              │                                       │                              │
│              │  WebSocket                            │                              │
│              └───────────────────────────────────────┤                              │
│                                                      │                              │
│                                         ┌────────────▼──────────────┐               │
│                                         │   Redis Namespace         │               │
│                                         ├───────────────────────────┤               │
│                                         │                           │               │
│                                         │  ┌──────────────────────┐│                │
│                                         │  │  Redis Master        ││                │
│                                         │  │  (State Store)       ││                │
│                                         │  └──────────┬───────────┘│                │
│                                         │             │             │               │
│                                         │  ┌──────────▼───────────┐│                │
│                                         │  │  Redis Replica       ││                │
│                                         │  │  (HA Replication)    ││                │
│                                         │  └──────────────────────┘│                │
│                                         │                           │               │
│                                         │  Persistent Volume (PV)   │               │
│                                         │  Civo Block Storage       │               │
│                                         └───────────────────────────┘               │
│                                                                                     │
│  ┌────────────────────────────────────────────────────────────────────────────┐     │
│  │                          ArgoCD Namespace                                  │     │
│  │  ┌──────────────────────────────────────────────────────────────────────┐  │     │
│  │  │  ArgoCD Server - Continuous Deployment & Health Monitoring           │  │     │
│  │  │  Auto-sync from Git | Rolling Updates | Health Checks                │  │     │
│  │  └──────────────────────────────────────────────────────────────────────┘  │     │
│  └────────────────────────────────────────────────────────────────────────────┘     │
│                                                                                     │
└─────────────────────────────────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────────────────────────────────┐
│                              Data Flow & Communication                              │
├─────────────────────────────────────────────────────────────────────────────────────┤
│                                                                                     │
│  Players ──HTTPS──▶ Traefik Ingress ──▶ Frontend (React)                            │
│                                                │                                    │
│                                                ▼                                    │
│  Players ──WSS────▶ Traefik Ingress ──▶ Backend (Go) ◀──▶ Redis (State)             │
│                                                │                                    │
│                                                ▼                                    │
│                                         Redis Pub/Sub                               │
│                                       (Multi-pod Sync)                              │
│                                                                                     │
└─────────────────────────────────────────────────────────────────────────────────────┘

          

Infrastructure Architecture

Cloud Platform

The application is hosted on Civo Cloud, a cloud-native Kubernetes platform that provides fast cluster provisioning and excellent performance. Civo's K3s-based infrastructure offers production-ready Kubernetes clusters with integrated networking, storage, and load balancing capabilities. The platform's simplicity and developer-friendly approach make it ideal for deploying containerized applications at scale.

Kubernetes Architecture

The application follows a multi-namespace architecture for logical separation and resource isolation:

  • Frontend Namespace - Hosts the React application served through NGINX with 2 replicas for high availability
  • Backend Namespace - Runs the Go WebSocket server with 2 replicas behind a service for load distribution
  • Redis Namespace - Contains Redis master-replica setup for distributed state management
  • ArgoCD Namespace - GitOps controller managing continuous deployment

Network Architecture

  • Traefik Ingress Controller - Routes external traffic with SSL/TLS termination
  • Service Mesh - Internal communication between microservices using ClusterIP services
  • WebSocket Support - Configured ingress rules for persistent WebSocket connections
  • CORS Configuration - Environment-based origin validation for security

Storage & State Management

  • Redis Cluster - Master-replica configuration for room state, player connections, and game scores
  • Persistent Volumes - Civo block storage for Redis data persistence and application logs
  • ConfigMaps - Environment-specific configuration management
  • Secrets - Secure storage for Redis credentials and sensitive data

CI/CD Pipeline

Continuous Integration

  • Docker Multi-stage Builds - Optimized image builds with build cache and minimal production layers
  • Harbor Registry - Private container registry for secure image storage and vulnerability scanning
  • Image Tagging Strategy - Semantic versioning with automated latest tag updates
  • Platform Targeting - Cross-platform builds for linux/amd64 architecture

GitOps with ArgoCD

ArgoCD provides declarative GitOps continuous delivery for Kubernetes. The deployment workflow follows:

  • Git as Single Source of Truth - All Kubernetes manifests stored in version control
  • Automated Sync - ArgoCD monitors Git repository and automatically applies changes
  • Rolling Updates - Zero-downtime deployments with pod replacement strategy
  • Health Monitoring - Continuous health checks and automatic rollback on failures
  • Multi-environment Support - Separate ArgoCD applications for dev and production

Deployment Process

  1. Code changes pushed to GitHub repository
  2. Docker images built with version tags (e.g., v1.4.0-json-logs)
  3. Images pushed to Harbor private registry
  4. Kubernetes deployment manifests updated with new image tags
  5. ArgoCD detects manifest changes and triggers sync
  6. Rolling update deployed across pods with health checks
  7. Traffic gradually shifted to new pods while old pods drain

Technology Stack

Backend

  • Go (Golang) - High-performance WebSocket server with goroutine-based concurrency
  • Gorilla WebSocket - Production-ready WebSocket implementation
  • Gorilla Mux - HTTP router and URL matcher
  • Redis Client - Go Redis library for distributed state
  • Logrus - Structured JSON logging for observability

Frontend

  • React 18 - Modern UI framework with hooks
  • TypeScript - Type-safe development
  • Vite - Fast build tool and dev server
  • React Router - Client-side routing
  • WebSocket API - Real-time bidirectional communication

Infrastructure

  • Kubernetes (K3s) - Container orchestration on Civo Cloud
  • ArgoCD - GitOps continuous delivery
  • Docker - Containerization with multi-stage builds
  • Harbor - Private container registry
  • Traefik - Ingress controller and reverse proxy
  • Redis - In-memory data store for distributed state
  • NGINX - Static file serving for frontend

Distributed Systems Design

Scalability Patterns

  • Horizontal Pod Autoscaling - Ready for automatic scaling based on CPU/memory metrics
  • Redis Pub/Sub - Broadcast messages across all backend pods for real-time sync
  • Stateless Backend - All game state stored in Redis for pod interchangeability
  • Connection Pooling - Efficient Redis connection management across pods

High Availability

  • Multi-replica Deployments - 2 replicas for both frontend and backend
  • Redis Replication - Master-replica setup for data redundancy
  • Health Checks - Liveness and readiness probes for automatic recovery
  • Rolling Updates - Zero-downtime deployments with gradual traffic shift

Distributed State Challenges Solved

  • Room Capacity Enforcement - Redis-based global player count prevents race conditions across pods
  • Player Connection Tracking - Unique connection IDs with Redis hash maps for distributed tracking
  • Game State Synchronization - Redis as single source of truth for player positions and scores
  • WebSocket Stickiness - Ingress configuration maintains WebSocket session affinity

Observability & Monitoring

Structured Logging

  • JSON Log Format - Structured logs optimized for log aggregation platforms
  • Standardized Fields - Service name, version, environment, and timestamp on every log
  • Event Taxonomy - Consistent event naming (room.player.joined, websocket.connection.established)
  • Contextual Logging - Room ID, player ID, connection ID for distributed tracing
  • SigNoz Integration - Ready for SigNoz dashboard creation with indexed attributes

Metrics & Traces

  • Request Logging Middleware - HTTP request/response tracking with duration and status codes
  • Visitor Analytics - Client IP, user agent, and referer tracking
  • Performance Metrics - Request duration, bytes transferred, error rates
  • Log Rotation - Lumberjack library for automatic log file management

Security Implementation

  • CORS Protection - Environment-based origin whitelisting
  • Non-root Containers - All containers run as unprivileged user (uid 1000)
  • Secret Management - Kubernetes secrets for sensitive credentials
  • Network Policies - Namespace isolation with controlled ingress/egress
  • TLS Encryption - End-to-end encryption with SSL/TLS termination at ingress
  • Private Registry - Harbor with role-based access control
  • Resource Limits - CPU and memory limits to prevent resource exhaustion

Key Features

  • Real-time multiplayer gameplay with WebSocket connections
  • Room-based matchmaking with 2-player limit enforcement
  • Distributed state management across multiple backend pods
  • Zero-downtime deployments with rolling updates
  • GitOps-driven continuous delivery with ArgoCD
  • Production-grade structured logging for observability
  • High availability with multi-replica architecture
  • Cloud-native deployment on Civo Kubernetes

Technical Achievements

  • Designed and implemented production-ready Kubernetes infrastructure from scratch
  • Solved distributed systems challenges including race conditions and state synchronization
  • Built CI/CD pipeline with Docker, Harbor, and ArgoCD for automated deployments
  • Implemented WebSocket-based real-time communication at scale
  • Configured multi-namespace Kubernetes architecture with proper network isolation
  • Integrated structured logging with JSON format for modern observability platforms
  • Deployed and managed Redis cluster for distributed state management
  • Implemented security best practices including CORS, secrets management, and non-root containers