Putting it on the internet
What hosting is, why many copies of a server need one shared memory, and how Tour of the Day is chosen.
In the workspace, the game runs on one computer. To let anyone play it, it can be published to Vercel, a service that runs web apps on the internet. This chapter is about what changes when it moves there.
Concept: "hosting". Hosting means running your program on a computer that's always on and reachable from the internet, so anyone can visit it. Vercel is a hosting service.
Many copies, one memory
On Vercel, the server isn't one program on one computer. Vercel starts copies of it as players arrive, and stops them when it's quiet. Each copy can only write to its own temporary folder, which is wiped when it stops.
That's fine for caches, since the archive always has the originals:
61_DEFAULT_DATA_DIR = "/tmp/terrapin-trail" if os.environ.get("VERCEL") else "data/.apps/terrapin-trail" 62DATA_DIR = Path(os.environ.get("TERRAPIN_TRAIL_DATA_DIR", _DEFAULT_DATA_DIR))
It's not fine for anything players share. If each copy kept its own leaderboard, two friends could see different boards. So shared things live in a separate database, Redis, that every copy talks to.
Concept: "stateless". A stateless server keeps nothing important in itself between requests; anything that matters lives in a database. That's what lets a hosting service start and stop copies freely without losing anything.
Tour of the Day
The first visitor of each day triggers the choice: the date picks one of the 40 most-played bands, the show closest to today's date picks the year, and that show's tour becomes the day's tour. The result is saved, so everyone that day gets the same tour.
- Hosting on Vercel runs many short-lived copies of the server.
- Caches can be temporary; shared data must live in a database every copy can reach (stateless servers).
- Tour of the Day is chosen once per date and saved for everyone.