Skip to Content
BackendDeployment Guide

Deployment Guide

Last Updated: April 2026


Prerequisites

ToolVersionPurpose
Go1.26+Compile the service binary
Docker24+Container images
kind0.20+Local Kubernetes cluster
ctlptl0.8+Declarative cluster management
Tilt0.33+Live development environment
AtlaslatestDatabase migration tooling

Environment Variables Reference

All configuration uses the BATTLESBIT_ prefix. Nested keys use double underscores (__) as separators.

HTTP / API

VariableDefaultDescription
BATTLESBIT_HTTP__HOST0.0.0.0Listen address
BATTLESBIT_HTTP__PORT8888Listen port
BATTLESBIT_HTTP__ALLOWED_ORIGINS*CORS allowed origins (comma-separated). Restrict in production.
BATTLESBIT_HTTP__RATE_LIMIT20Max requests per window
BATTLESBIT_HTTP__RATE_LIMIT_WINDOW1sRate limit window
BATTLESBIT_HTTP__DEBUGfalseEnable debug mode
BATTLESBIT_HTTP__BASE_URL""Public base URL
BATTLESBIT_HTTP__GQL__PLAYGROUNDtrueEnable GraphQL playground
BATTLESBIT_HTTP__GQL__INTROSPECTIONtrueEnable introspection
BATTLESBIT_HTTP__GQL__COMPLEXITY_LIMIT100Max query complexity

Database

VariableDefaultDescription
BATTLESBIT_DATABASE__URIpostgresql://postgres:postgres@localhost:5432/postgres?sslmode=disablePostgreSQL connection string
BATTLESBIT_DATABASE__DEBUGfalseLog all SQL queries
BATTLESBIT_DATABASE__AUTO_MIGRATEtrueRun migrations on startup
BATTLESBIT_DATABASE__MAX_OPEN_CONNS10Max open connections
BATTLESBIT_DATABASE__MAX_IDLE_CONNS5Max idle connections

Authentication / JWT

VariableDefaultDescription
BATTLESBIT_JWT__SIGNING_KEYberyberysecretJWT HMAC signing key. Must change in production.
BATTLESBIT_JWT__ACCESS_DURATION168hToken validity period
BATTLESBIT_JWT__DEV_LOGINfalseAllow passwordless login (dev only)

Cache (Redis)

VariableDefaultDescription
BATTLESBIT_CACHES__REDIS__ADDRSlocalhost:6379Redis addresses
BATTLESBIT_CACHES__REDIS__PASSWORDredisRedis password

NATS JetStream

VariableDefaultDescription
BATTLESBIT_NATS__URInats://nats:nats@localhost:4222NATS server URI

Monitoring

VariableDefaultDescription
BATTLESBIT_MONITORING__PROMETHEUS_ENABLEDtrueExpose Prometheus metrics
BATTLESBIT_MONITORING__METRICS_PATH/metricsMetrics endpoint path
BATTLESBIT_MONITORING__SENTRY_DSN""Sentry DSN for error tracking
BATTLESBIT_MONITORING__SENTRY_TRACINGtrueEnable Sentry performance tracing

Market Data (Binance)

VariableDefaultDescription
BATTLESBIT_MARKET__BINANCE__API_URLhttps://api.binance.com/api/v3Binance REST API URL
BATTLESBIT_MARKET__BINANCE__WS_URLwss://fstream.binance.comBinance WebSocket URL
BATTLESBIT_MARKET__BINANCE__TIMEOUT10sHTTP timeout
BATTLESBIT_MARKET__BINANCE__ASSETSBTCUSDT,ETHUSDT,...Subscribed trading pairs

Solana / HD Wallet

VariableDefaultDescription
BATTLESBIT_SOLANA__ENDPOINThttps://api.testnet.solana.comSolana RPC endpoint
BATTLESBIT_HDWALLET__MASTER_SEED_PHRASE(dev seed)CRITICAL: Store in KMS/Vault in production.

Email (SMTP)

VariableDefaultDescription
BATTLESBIT_EMAIL__HOSTsmtp.hostinger.comSMTP host
BATTLESBIT_EMAIL__PORT465SMTP port
BATTLESBIT_EMAIL__USERNAMEnoreply@battlesbit.comSMTP username
BATTLESBIT_EMAIL__PASSWORD""SMTP password

Object Storage (S3)

VariableDefaultDescription
BATTLESBIT_S3__ENDPOINTs3.ir-thr-at1.arvanstorage.irS3-compatible endpoint
BATTLESBIT_S3__BUCKETbattlesbitBucket name
BATTLESBIT_S3__ACCESS_KEY(placeholder)S3 access key
BATTLESBIT_S3__SECRET_KEY(placeholder)S3 secret key

Firebase

VariableDefaultDescription
BATTLESBIT_FIREBASE__CREDENTIALS_FILEpath_to_file.jsonFirebase service account credentials

Database Migration Strategy

Migrations are managed via Ent + Atlas.

Before deploying a new version

# Run migrations against the target database go run . migrate

The service also supports auto_migrate: true (default), which runs migrations on startup. For production, it is safer to run migrations as a separate pre-deploy step:

  1. Build the binary
  2. Run ./service migrate as a Kubernetes Job before rolling out the new Deployment
  3. Set BATTLESBIT_DATABASE__AUTO_MIGRATE=false on the running service pods

Rolling back a migration

# Atlas CLI rollback atlas migrate down --env production # If Atlas is unavailable, apply manual SQL psql $DATABASE_URL < rollback.sql

Service Scaling Guidance

Stateless services (scale horizontally)

ServiceNotes
cmd/web (GraphQL API)Standard HTTP service. Scale with HPA on CPU/request latency.
Arena WorkerProcesses positions via NATS pull consumers. Multiple replicas safe with same durable name.
Withdrawal WorkerProcesses withdrawals via NATS pull consumers. Constrained by Solana RPC rate limits.
Achievement WorkerProcesses achievement events via NATS. Multiple replicas with same durable name.

Stateful services (scale with care)

ServiceNotes
PostgreSQLUse managed Postgres (RDS, CloudSQL). Scaling requires read replicas or sharding.
NATSRun as a NATS cluster (3+ nodes recommended).
RedisRun as a Redis cluster or managed service.

NATS JetStream Consumer Scaling

Multiple replicas of the same worker can consume from the same stream using the same durable consumer name. NATS will distribute messages across consumers automatically (pull-based load balancing).

Worker-1 ─┐ Worker-2 ─┤── Pull from "POSITIONS" stream, consumer "arena-worker" Worker-3 ─┘

Secret Management

Production secrets that MUST be secured

SecretRecommended Storage
HDWALLET__MASTER_SEED_PHRASEAWS KMS, HashiCorp Vault, or GCP Secret Manager
JWT__SIGNING_KEYKubernetes Secret or Vault
DATABASE__URIKubernetes Secret or Vault
EMAIL__PASSWORDKubernetes Secret
S3__SECRET_KEYKubernetes Secret
FIREBASE__CREDENTIALS_FILEMounted secret volume

The Solana HD wallet seed phrase is the most sensitive secret. In V2, migrate to a proper KMS/Vault integration for key signing operations.


Kubernetes Deployment

Manifest structure

Kubernetes manifests should include:

  • Deployment for each service (web, arena, withdrawal, achievement)
  • Service (ClusterIP) for internal routing
  • Ingress for external HTTPS access
  • ConfigMap for non-sensitive configuration
  • Secret for credentials
  • Job for pre-deploy database migrations

Health checks

The web service exposes:

  • Readiness: GET /healthz
  • Liveness: GET /healthz
  • Metrics: GET /metrics (Prometheus format)

Monitoring

Prometheus

Metrics are exposed at /metrics when BATTLESBIT_MONITORING__PROMETHEUS_ENABLED=true.

Key metrics to monitor:

  • GraphQL operation duration (p50, p95, p99)
  • Active WebSocket connections
  • NATS consumer lag (messages pending)
  • Database connection pool utilization

Sentry

Error tracking is enabled by setting BATTLESBIT_MONITORING__SENTRY_DSN. Performance tracing captures request spans when SENTRY_TRACING=true.


CORS Configuration for Production

The default allowed_origins: ["*"] is permissive for development. In production:

BATTLESBIT_HTTP__ALLOWED_ORIGINS="https://app.battlesbit.com,https://admin.battlesbit.com"

Set this to the exact list of frontend origins that should be allowed.

Last updated on