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
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 (
9100node_exporter,9101app 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
testESLint, then 26 automated tests against the real app (about 20 seconds). -
2
infra-lintansible-lint on the production profile, terraform fmt, terraform validate. -
3
dockerBuild the image, stamp the commit SHA inside it, push :SHA and :latest, with a layer cache. -
4
deployCheck the image exists, then a pinned Ansible version runs the playbook for that exact SHA, never :latest. -
5
smoke testsThe live /healthz must return ok and report the new SHA. Grafana, Prometheus and Alertmanager must answer.
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,
/healthzreturns 503 and the check fails.
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
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
textContentand 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,
.envfiles 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.