How we moved Clover off a 128 GB server and onto half a CPU core
We moved Clover's API, Discord bot, and databases from a 128 GB server to Azure. It now costs $41 to $55 a month, and each switch took about three minutes of downtime.
I work on Clover, a Minecraft client and launcher. Behind it sit one API, one Discord bot, and a database for each. Until this month, all of that ran on a dedicated server with 8 cores and 128 GB of RAM, and used less than 10% of it.
Over two days, we moved everything to Azure. The two apps now share half a vCPU and 1 GiB of memory, next to a small managed database. The whole thing costs between $41 and $55 a month, and each switch took the service down for about three minutes.
This is how it went, including the parts that did not go well.
By the numbers#
- 2 apps and 2 databases moved: the API, the Discord bot, and Postgres for both.
- 59 tables and 2,151 rows. We are not big. That is the point of this post.
- About 3 minutes offline for each of the two switches.
- $41 to $55 a month for all of it.
- 2 to 4 minutes from
git pushto the new version being live. - 17 lines to fix a websocket bug that turned out to be older than the migration.
Where we started#
Clover ran on a small Kubernetes cluster on one very large server, with Caddy and Cloudflare in front, and our own CI runner doing the deploys. It worked. It was also a 128 GB machine doing the job of a Raspberry Pi.
The server is being retired, so we had to move anyway. The obvious answer was a smaller server. We wanted two things that a smaller server does not give you:
- No more machines to look after. Something managed, that can grow later without us rebuilding it.
- As cheap as possible for about 100 players.
The new setup#
- Azure Container Apps runs the API and the bot in one small container each, with a quarter of a vCPU and half a gigabyte of memory.
- Azure Database for PostgreSQL holds both databases on one small server (1 vCore, 2 GiB).
- Azure Container Registry stores the images.
- Cloudflare still sits in front of the API.
- Our own CI runner still builds everything. A deploy is a push to main.
- Redis, which the bot uses for cooldowns and caches, runs as a second container inside the bot's app.
Before
- Players
- Cloudflare
- Caddy
- One dedicated server8 cores, 128 GB of RAM, under 10% used
- API
- Discord bot
- Postgres for each
Deploys: our own CI runner
After
- Players
- Cloudflare
- Azure Container AppsA quarter of a vCPU and half a gigabyte per app
- API
- Discord bot + Redis
- Azure Database for PostgreSQL1 vCore, 2 GiB, both databases
Deploys: our own CI runner, pushing images to Azure Container Registry
What it costs, and why the bot has a price range#
We did not guess the prices. Azure has a public price list you can query without an account, so we pulled the real per-second rates for our region and did the arithmetic.
Two things about Container Apps billing took a while to sink in:
- The free allowance is per subscription, not per app. One always-on app uses all of it. The second app pays full price from the first second.
- There is an idle rate, and for CPU it is eight times cheaper. A container is billed as idle only while it handles no web requests, uses less than 1% of a core, and receives fewer than 1,000 bytes a second.
A Discord bot for 100 people is idle most of the day. Whether Azure agrees is something only the first bill can tell us, which is why the bot is listed at $5.91 to $19.71.
We also looked at the alternatives. The same bot on Azure's plainer container service would be a flat $9.02. A managed Redis starts at $11.68 a month, which is more than the bot itself might cost. So Redis moved in with the bot.
How a deploy works now#
One file describes each app. The image, the secrets, the size, the domain, and its certificate all live in a single template, and the pipeline applies it on every deploy. The catch is that anything clicked in the Azure portal is undone by the next push. We found this out halfway through adding the domain by hand.
The config file is a secret. Both apps read one config file at startup. The whole file is stored as a CI secret, handed to Azure as a secret, and mounted into the container as a file. One trap: changing a secret does not restart the app, so the bot gets a fresh copy on every pipeline run to make sure it reads the new one.
The Azure command line never gets installed. Installing it on our runner failed, because the installer tries to replace a certificate file the runner has mounted from its host. The job now borrows Microsoft's own container image for that one step.
Moving the data without losing a row#
We had two rules.
Rehearse with production still running. The first copy of each database happened while everything was live. Nothing was interrupted, and we learned how long the real one would take.
Count every table on both sides. After each copy we compared row counts table by table. Not a total. Every table.
Then we did the real thing, twice: stop the old app, copy the database (29 seconds for the API, 4 seconds for the bot), start the new app on the copied data, and point the traffic at it.
The API was answering again 3 minutes and 6 seconds after we stopped it. The bot took 3 minutes and 4 seconds.
The one surprise was that the managed database refuses Postgres extensions until you allow each one in a server setting. Our very first migration needs one.
A Discord bot on a platform built for websites#
Container Apps is designed around things that answer web requests. A bot answers nothing. It dials out to Discord and stays connected. That mostly works, with three quirks.
- Redis rides along. A second container in the same app shares the bot's network, so the bot talks to Redis on localhost. Both containers have to fit inside one of the allowed sizes, so we measured first: the bot peaked at 182 MiB.
- It forgets on every deploy. The Redis container has no disk and starts empty each time. That is fine for cooldowns, but not for anything you want to keep.
- Zero-downtime deploys are wrong for a bot. Azure starts the new copy before stopping the old one. For a website, that is the whole idea. For a bot, it means two bots are online for a moment, both answering.
Two things we found on the way#
The move fixed a bug we did not know we had. Requests now pass through Cloudflare and Azure before reaching our code, so we had to tell the API which proxies to trust. While checking that, we found the old setup had been saving Cloudflare's address instead of the player's for the 68 most recent sessions.
"It can autoscale" is not the same as "we can autoscale." We wanted autoscaling. Then we read our own code. The API keeps its live connections and its rate limits in memory, so two copies would each see half the players. It runs as exactly one copy until that changes. The platform was ready before we were.
What broke#
The first region said no. The cheapest database size was refused in the first region we picked, and again in the second, after other pieces had already been built there. Start with the piece most likely to refuse.
Then the bot kept hanging up. After the move, we noticed the bot's connection to the API dropping and reconnecting every two minutes. The natural assumption was that the migration had broken it.
So we measured the gaps: 125, 125, 126 seconds. Then we counted: 460 reconnects in about 17 hours.
The cause was older than the migration. That connection carries nothing unless a player comes online, and Cloudflare closes websockets that stay silent. Player connections got a ping every 30 seconds. The bot's connection got none. The fix was 17 lines, and the old server sat behind Cloudflare too, so this had very likely been going on long before we moved.
The lesson: a number that steady is a timeout, not a crash.
Six connections in a row, each dropped after 125 or 126 seconds
Still connected nine minutes later, when we stopped watching
03 min6 min9 min12 min
And our own check lied to us. For a few minutes, we believed the bot was down, while it was happily answering commands. Our "is it talking to the database" check ran inside one transaction, and Postgres shows a transaction a frozen picture of who is connected. We were polling a photograph.
What we gave up#
Moving the apps turned out not to be the whole job.
- Metrics. Our dashboards read from both apps directly. On Azure, there is nothing for them to reach, so they have nothing new to show.
- Backups. The nightly dumps kept running, against the old database that no longer changes. The live data had only Azure's built-in seven days.
- Logs. They are in Azure now, not next to everything else we run.
None of it is hard to fix. All of it was invisible until after the switch.
A migration is not finished when the app answers. It is finished when the monitoring and the backups have moved too.
What's next#
- Shared state for the API, so it can run more than one copy and the autoscaling we came for can be switched on.
- Metrics and dashboards for the two apps in their new home.
- Off-site database dumps again.
- A working uptime ping for the bot. The old one points at a service we replaced.
- Turning the big server off.
Thanks to everyone who plays Clover and hangs out in the Discord, including during the three minutes the bot went missing. If something feels off since the move, tell us there.