A community rhythm game's server:
live through traffic surges on update days
On a community rhythm game made by two people, I handled the server, database and security; the other member built the game client. In the published server code, load, retries and input checks are covered by tests.
OperationLive serviceA game server with traffic surges on update days
CITestedAutomatically, on SQLite · MySQL 8.0 · 8.4
Architecture
The game client reports the play count of each chart of a song, and the server receives the reports and stores them in the database.
Built by me
Others
- 01Client
-
- Others
- The game client was built by the other member of the team.
- Traffic
- Surged on update days.
- Reports
- Sends the play count of each chart of a song to the server.
-
- 02Edge
-
- What I did
- Set up a Cloudflare proxy and WAF in front of the server, so abuse of the unauthenticated reports is stopped before it reaches the server.
- Stack
- Cloudflare
- WAF
-
- 03API server
-
- What I did
- Ran the FastAPI play-log API in a Docker container on EC2. The public version deals with connection-pool exhaustion, duplicate storage of retried reports, and input checks.
- Stack
- FastAPI
- Docker
- AWS EC2
-
- 04Storage
-
- What I did
- Stored the play reports in RDS MySQL. The public version compares song titles exactly as written, so songs that differ only in case or in hiragana vs katakana stay separate.
- Stack
- MySQL
- AWS RDS
-
ResultRan as a live service through traffic surges on update daysThe public repository is a cleaned-up version (with tests)
Problems handled in the public version
The request the game client sends stays as it was; the server-side problems were pinned down one by one with tests.
Requests failing when many players send at once
- Problem
- The first draft of the public code took its database connection too early, so in a test with many clients sending at once, many of the requests ended up waiting on each other and failed.
- Fix
- The connection is now taken, used and returned inside one worker thread, and there are as many connections as worker threads. CI runs the concurrent-request test and checks that every request is answered.
Retried reports stored twice
- Problem
- When a connection drops and the same report arrives again, its play counts can be added twice.
- Fix
- A report that carries a number (a report id) is stored only once per number. Two copies arriving at the same moment are still stored once, and the same number with different contents is refused. The released game sends no number, though, so its retries are still stored as separate reports.
Song titles merging
- Problem
- By default MySQL does not tell upper from lower case or hiragana from katakana, so it treats “Glory” and “GLORY”, or “ゆめ” and “ユメ”, as the same song.
- Fix
- Titles and ids are compared exactly as written, character for character, and CI checks it.
Input anyone can send
- Problem
- The released game sends its reports without any login, so anyone who knows the server's address can send one.
- Fix
- The server checks the format before it reads the contents and turns away malformed input and oversized requests right away; Cloudflare in front also helps hold off abuse. The README also states the limit: a report does not prove that a play actually happened.
GitHub Actions runs the tests on SQLite and on MySQL 8.0 and 8.4, and checks that the database definition (schema.sql) matches the code.
- Python
- FastAPI
- Docker
- AWS EC2
- AWS RDS
- MySQL
- Cloudflare
- GitHub Actions