CSE Roadmap

Common tier · about 4 hours · 8 parts

Part 5 of 8 · 2 min read

Storage and deployment

Memory, rebuild and serverless.

Sizing. 650 MB is the worst case, and 2,500 updates a second is far below what one Redis server can do. So a single server is enough at this scale.

Failure. Redis can persist to disk, but a large instance restarts slowly from disk. The usual setup is a read replica that is promoted when the main node fails, with a new replica attached.

Rebuild path. A relational database keeps two supporting tables. The user table has the id and display name. The point table has the user id, the score gained and the time of each win. That history serves play history and other features, and it can rebuild a lost leaderboard by summing points per user for the month.

A worked example (made up). Redis loses its data. A job reads the point table for February, sums points per user, and loads each total with ZADD. For 25 million users, the job takes minutes to hours, so the replica is the first line of defence and the rebuild is the second.

Small cache. User names and pictures of the top 10 are read most often, so a small cache for them removes most database reads.

Two ways to deploy.

  1. On your own servers: API servers talk to Redis and MySQL.
  2. Serverless on a cloud: an API gateway maps each HTTP route to a function (for example AWS Lambda). A function runs only when called and scales by itself. It calls Redis and the database. The book recommends this for a new game, since there are no servers to size or patch.

Serverless function

A serverless function is code that a cloud runs on demand and scales automatically, with no servers for you to manage.

Example: a function that adds one point is called 2,500 times a second at peak, and the cloud starts as many copies as needed.

Check yourself: 1. How is a lost leaderboard rebuilt? 2. What does a read replica add? 3. What does serverless save?