Case study

BattleRoom.

A real-time multiplayer quiz-battle game, and everything that builds, ships, watches and alerts on it. The app is Node.js, Express, Socket.IO and MongoDB with JWT auth and Gemini-generated questions. This page is about how it runs.

AWS resources in Terraform
23
pipeline jobs on every push
5
automated tests on the real app
26
Prometheus alert rules
8
outside uptime check
15 min

01

Architecture

BattleRoom architecture on AWS Players connect over HTTPS and WebSockets to Nginx on the app server. GitHub Actions pushes images to Docker Hub and deploys the exact commit SHA with Ansible over SSH. Inside one custom VPC and public subnet, a separate monitoring server scrapes node_exporter on port 9100 and app metrics on port 9101, which only it can reach. The app uses MongoDB Atlas and the Gemini API. Alertmanager and the outside uptime check post to Discord. Players browser GitHub Actions 5-job pipeline on push uptime check every 15 min rollback by old SHA Docker Hub :SHA and :latest AWS · custom VPC · public subnet internet gateway App server EC2 · Elastic IP · IMDSv2 Nginx · Let’s Encrypt TLS · WebSocket proxy App container · Node.js · Socket.IO node_exporter :9100 · app metrics :9101 Monitoring server EC2 t3.micro · Elastic IP Nginx · the one public door Prometheus · Grafana · Alertmanager 127.0.0.1 Blackbox exporter · HTTPS + cert expiry MongoDB Atlas database Gemini API question generation Discord #alerts Alertmanager + uptime check HTTPS · WSS deploy :SHA Ansible · SSH push image scrape :9100 :9101 security group: monitoring only alerts
Two EC2 instances in one custom VPC, all defined in Terraform (23 resources). The monitoring server is the only thing that can reach the metrics ports.

02

Infrastructure and configuration

Terraform defines 23 AWS resources: a custom VPC, a public subnet, an internet gateway, routing, and two EC2 instances with Elastic IPs, one for the app and one for monitoring.

  • Metrics ports (9100 node_exporter, 9101 app metrics) are reachable only from the monitoring server, through security-group references.
  • IMDSv2 is enforced on every instance.
  • On the monitoring box, Prometheus and Grafana listen only on 127.0.0.1. Nginx is the one public door.

Ansible takes a fresh server to a running app with two roles. base installs Docker, Compose, certbot and node_exporter, grows the disk and adds 1 GB of swap. certbot gets the first certificate and installs the renewal hook. Handlers restart a service only when its config actually changed.

The test of all this: rebuild the app instance from scratch, run the playbook, and it comes back with a new certificate. No hand steps. Nginx proxies WebSockets with Let’s Encrypt TLS, and renewals reload Nginx without dropping live games.

03

Pipeline

Five jobs in GitHub Actions, on every push to main:

  1. 1 test ESLint, then 26 automated tests against the real app (about 20 seconds).
  2. 2 infra-lint ansible-lint on the production profile, terraform fmt, terraform validate.
  3. 3 docker Build the image, stamp the commit SHA inside it, push :SHA and :latest, with a layer cache.
  4. 4 deploy Check the image exists, then a pinned Ansible version runs the playbook for that exact SHA, never :latest.
  5. 5 smoke tests The live /healthz must return ok and report the new SHA. Grafana, Prometheus and Alertmanager must answer.
git push → green
A real run, sped up: test, infra-lint and docker go green, Ansible deploys, /healthz reports ok with the new SHA.

Safety rails

  • One deploy at a time.
  • Rollback: enter an old SHA in “Run workflow”. Tests and build are skipped and the old image is deployed.
  • Tests and lint also run on pull requests, but nothing deploys from them.
  • All versions are pinned.

The check from outside

A second workflow runs every 15 minutes from GitHub, outside AWS. It checks the game, Grafana, Prometheus and Alertmanager, and posts to Discord only when something breaks or recovers. It catches the one case the monitoring server can’t: the monitoring server itself dying.

04

Tests

The real app.js runs as its own process. Only two things are swapped: the database, for a throwaway in-memory MongoDB, and Gemini, for a fake that can be made slow or broken. The 26 tests cover:

  • Malformed socket messages can’t crash the server.
  • Message floods get cut off.
  • Forged and unsigned tokens are refused.
  • Outsiders can’t join rooms.
  • A full two-player game, with scoring.
  • Reconnecting mid-game.
  • Gemini busy (retried) and Gemini down (players are told).
  • Auth rules and rate limits.

05

Monitoring and alerts

The monitoring server runs Prometheus, Grafana, Alertmanager, a blackbox exporter and Nginx. Dashboards and the data source are provisioned from files.

  • Hosts: node_exporter on both servers.
  • App: metrics on port 9101. Requests by route and status, response time, live players, active rooms, event-loop lag, Gemini successes and failures: the four golden signals.
  • From outside: the blackbox exporter checks each public URL over HTTPS, plus certificate expiry. It catches what the app can’t see about itself. If MongoDB is down, /healthz returns 503 and the check fails.
Grafana
The Grafana dashboard. It’s live, but behind a login on purpose; I’m happy to walk through it in an interview.

Eight alert rules, to Discord

Prometheus evaluates the rules, and Alertmanager routes them to a Discord channel.

  • TargetDown
  • EndpointDown
  • HighErrorRate
  • SlowResponses
  • QuestionGenerationFailing
  • CertificateExpiringSoon
  • DiskAlmostFull
  • MemoryAlmostFull
Alert
TargetDown fires, then resolves.

06

Hardening

  • Every socket message is validated, and a wrapper catches errors, so one bad message can’t crash the server.
  • Rate limits: 50 socket messages per 10 seconds per connection, 16 KB max per message; 100 web requests per 15 minutes per IP; 10 failed logins per 15 minutes per IP (successful logins don’t count).
  • An unknown email and a wrong password cost the same bcrypt work, so timing doesn’t reveal which emails exist.
  • JWT is HS256 only, and unsigned tokens are refused. NoSQL injection is blocked by checking that inputs are plain strings. XSS is blocked with textContent and a Helmet CSP.
  • Anti-cheat: the right answer is never sent before you answer, only the first answer counts, and scores are computed on the server.
  • Six secrets live in GitHub Actions. On the servers, .env files are root-only.

07

Decisions and trade-offs

Chose Over Because
GitHub Actions Jenkins The code already lives on GitHub. No CI server to run and patch.
Deploy by commit SHA :latest Every deploy traces back to a commit, and rolling back is just entering an older SHA.
Tests against the real app Fully mocked unit tests Only the database and Gemini are swapped, so the tests hit the real server, sockets and all.
Monitoring on its own t3.micro The same box as the app The 1 GB app server can’t hold Prometheus and Grafana too, and monitoring shouldn’t die with the thing it watches.
Uptime check in GitHub Actions A hosted checker like UptimeRobot It runs outside AWS, so it still reports when the monitoring server is down, and it lives in the repo next to the pipeline.
App metrics on internal port 9101 A public /metrics route Only the monitoring server can reach it, by security-group rule. Nothing about the app’s internals is public.
Two Nginx configs, HTTP first One config with HTTPS from the start A fresh server can’t start Nginx with an HTTPS block that points at a certificate that doesn’t exist yet.
Certbot on the host Switching Nginx for Caddy Keeps the more common setup I’ll meet at work.
.env over SSH from a GitHub secret AWS Secrets Manager Right-sized for one server and one operator. Secrets Manager is the answer at larger scale.

Questions about any of it? utkarshtyagi9050@gmail.com, or read the code on GitHub.