Rebuilding the infrastructure behind vAMSYS
Between 20 and 22 November I rebuilt the infrastructure behind vamsys.io so that no single server can take it down.
Between 20 and 22 November I rebuilt the infrastructure behind vamsys.io so that no single server can take the site down any more. Most of you won't have noticed, and that was the point: the work happened one piece at a time, with the site running throughout.
This post explains what vAMSYS used to run on, what changed and why. It also covers the honest downside: the server count went down, but the software running on those servers is far more involved, and that means more things that can break. I'll explain why I think the trade is worth it, and what I did to keep the extra complexity under control.
Why this mattered
vAMSYS has no quiet hours. Virtual Airlines and their pilots are spread across every time zone, so someone is always flying, booking or filing. A long-haul flight can take twelve hours, and when it ends the pilot expects the PIREP to go through that minute, not "once the server is back". Pegasus also reports each flight's position throughout the flight. An outage at the wrong moment doesn't just show an error page: it can interrupt flights in progress, delay PIREPs and leave staff unable to run their airline.
As vAMSYS grows, more airlines depend on it, and the cost of a single machine failing grows with them. I wanted to fix that before a hardware failure forced the issue, not after.
Before: a few machines that everything depended on
Until 20 November, vAMSYS ran on about ten servers in a data centre in Amsterdam. The web servers were already doubled up, but three other jobs each lived on exactly one machine.
In plain terms:
- The load balancer is the front door. Every visit to vamsys.io came through it and was handed to a web server. If it stopped, the site stopped, even with five healthy web servers behind it.
- The database is the filing cabinet: every PIREP, booking, pilot and airline. One server held the live copy. If it failed, the fix was to rebuild it from backups, which needed a person and took hours.
- The worker does everything that isn't a page load: processing PIREPs after you land, sending emails, scheduled tasks and live updates. It also held everyone's login sessions. If it stopped, PIREPs piled up unprocessed, and everyone could be logged out.
None of these failed often, but each one was a single point of failure: a part whose loss takes the whole service down with it. They also made maintenance awkward. Security updates that need a reboot meant a choice between a short outage and putting the update off.
After: everything in pairs or more
The new layout has the same rule everywhere: every job that vamsys.io needs runs on at least two machines, and the spare can take over by itself.
- No more single front door. Each of the four app servers now has its own front door. Cloudflare, which already sat in front of vamsys.io, spreads visitors across them, and the brain takes any server that stops answering out of that list. Each front door also watches how busy its neighbours are and sends work to the least loaded one.
- The database is a pair. hold-alpha holds the live copy; hold-bravo keeps an exact second copy, updated as each change happens. A change only counts as saved once both have it, so a failure can't lose a PIREP you just filed. If the leader fails, hold-bravo takes over on its own, typically within a minute.
- The background work is a pair. Two "tugs" now share the job queue: PIREP processing, emails, scheduled tasks and live updates. If one stops, the other keeps going.
- A shared cache and login sessions on every app server. Login sessions and frequently read data (think airport details and aircraft types) are kept in fast memory with a copy on each app server, so a page can read them locally instead of crossing the network, and losing one server no longer logs anyone out.
- A watchtower. tower-alpha monitors everything and alerts me. It also casts the deciding vote whenever a pair has to agree on who leads, so the two halves of a pair can never both think they're in charge.
Tying it together is what I call the brain: a small program on every server. It checks each server's health every few seconds, moves visitors away from a struggling machine, and schedules the nightly reboots for security updates, one server at a time.
The honest part: more moving parts
Redundancy isn't free, even with fewer servers: from about ten to nine, almost all of them machines I already had. The cost is in the software running on them. Before, "the database" was one program on one server. Now it is two database servers, a referee that decides which one leads, a vote spread over three machines, and a front door on every app server that has to keep up with which database is in charge.
A few things keep that manageable:
- One pattern everywhere. Every part follows the same shape: two or more copies, a vote across three machines to decide which one leads, and a local signpost on each app server that follows the leader. Learn it once and you understand all of it.
- Standard tools, not home-made ones. The failover itself is done by well-known open-source software that many others run in production (listed at the end). The only part I wrote is the brain, and it coordinates; it doesn't replace any of them.
- Routine work is automated. Health checks, nightly reboots and the status page notices for them happen without anyone typing a command, so they happen the same way every time.
- Everything is written down. Each procedure, from replacing a server to restoring the database, has written steps, so fixing something at 3 am doesn't depend on remembering how it was built.
There is also a small, permanent cost. Because every change is saved on both database servers before it counts, saving is a little slower: PIREP processing takes a few percent longer than before. I think that is the right price for never losing a filed flight.
How the move was done
The move took three nights, one piece at a time:
It followed a few rules:
- A written plan for every step, including how to undo it.
- Rehearse first. Each risky change was tried on spare servers, and the database restore was timed, before doing it for real.
- One change at a time, with the riskiest steps at night and the status page updated as the work went on.
- Test the failure, not just the success. I deliberately switched the database, the cache and the front doors over while the site was live, to prove the takeover works and measure what users would notice.
What went wrong
Not everything went to plan. Three things reached users during the move:
- A permission set wrong. On 20 November, a file-permission mistake during work on the database server made about half of all requests to vAMSYS fail between 21:21 and 21:24 UTC. No data was lost, and the procedure was changed so the same mistake can't happen again.
- A dependency nobody checked. Hangar signs in and syncs airlines through vAMSYS. One of the changes altered how those requests reached vAMSYS, and Hangar's sign-in and airline sync failed from 21 November 17:34 to 22 November 11:58 UTC. Nothing was watching Hangar, so it went unnoticed overnight. Missed syncs were re-run, and Hangar now has its own check on the status page.
- A planned pause. On 21 November at about 02:23 UTC, vAMSYS showed its maintenance page for about half a minute while the database changed hands.
All three are on the status page, and they count against our uptime there. Downtime is downtime, planned or not.
Why it's better anyway
The difference is what happens when something does break. Before, losing one of the key machines meant an outage until a person woke up, worked out what happened and rebuilt it. Now the partner takes over by itself, in seconds to about a minute.
| If this breaks | Before | Now |
|---|---|---|
| The front door | Site down until fixed by hand | Taken out of rotation within about 20 seconds; in a live test, traffic moved in 16 |
| A web server | Fine: the others carried on | Fine: the other three carry on |
| The database | Site down; rebuild from backups, hours | hold-bravo takes over automatically, typically within a minute, with no saved data lost |
| Background work | PIREP processing, emails and live updates stop until fixed | The other tug keeps going |
| The cache and logins | Part of the worker, so they failed along with it, logging everyone out | Another app server takes the lead; a planned switch caused zero errors |
| The watchtower | Monitoring stops | Monitoring stops |
The second win is maintenance. Servers need security updates, and some updates need a restart. Before, every restart of a key machine meant either a short outage or postponing the update. Now the brain does it every night between 02:00 and 05:00 UTC, one server at a time. Before each restart it moves visitors or database leadership to a partner, then restarts, checks the server is healthy and moves on. Updates get applied within days instead of being put off for weeks, and nobody has to stay up for them.
And because no single server is special any more, I can take one out for an upgrade, or swap it entirely, during the day without a maintenance window.
Roads not taken
There were other ways to build this. These are the ones I seriously weighed and decided against:
- Cloudflare's load-balancing add-on. It was the first plan. Instead, the brain keeps one Cloudflare record per healthy app server and Cloudflare spreads visitors across them, which has the same effect without the add-on.
- A separate pair of load balancers. Keeping the old load balancer and adding a second would have kept a whole tier of servers that could fail. Putting a front door on each app server removes that tier and a network hop.
- Cloudflare straight to the app, with no front door on the servers. Simpler, but it would lose the one-second health checks and the queueing that stops a burst of traffic from overwhelming one server.
- Database traffic through central proxies. Every query would take an extra hop, and the database would depend on those proxies staying up. Instead, each server has its own small signpost that follows the database leader, and switches within about 5 to 10 seconds of a change.
- A floating IP address for the database. It only works within one site, its switch time isn't documented, and it wouldn't replace the local signposts, which also route reads. Not worth it.
- Several Redis servers all accepting writes. Not an option: the open-source version doesn't do it, and the job queue vAMSYS uses doesn't support Redis's cluster mode. So it's one leader with copies, and Sentinel to pick a new leader.
- Servers in the Americas or Asia. About 71% of real traffic comes from Europe, regional servers would need paid geographic routing, and all vAMSYS data is kept in the EU. Parked for now; making the app itself faster comes first.
- A second provider running live. Losing our whole hosting provider is a risk I accept. Instead there's the written plan to rebuild elsewhere from EU backups in 3 to 5 hours, described below.
- Kubernetes. It shines when servers are added and removed as traffic rises and falls, and to do that well it belongs on one of the big cloud providers. vAMSYS runs around the clock at a steady load on servers we already have, and moving to a big cloud provider would cost far more than our infrastructure budget allows for no real gain.
- Three app servers instead of four. Three would handle peak traffic even with one down. Four means at least two keep serving while one restarts for updates and another has failed at the same time.
- Copying database changes later instead of straight away. Faster saves, but a takeover could lose the last moments of data. I chose to save every change on both servers first; the cost is the few percent slower PIREP processing mentioned above.
Tighter security along the way
Rebuilding the servers was also a chance to close doors that didn't need to be open.
- Only Cloudflare gets in. The app servers now refuse web traffic that doesn't come through Cloudflare, which proves itself with a certificate on every connection. Attacks aimed straight at a server's address bounce off, and Cloudflare's protection can't be bypassed.
- Encrypted between our own servers. Traffic between the app servers, the tugs and the coordination layer is encrypted, even though it never leaves the data centre. The job queue and session storage have always needed a password; now they also accept connections only from our own servers.
- Every server only talks to the servers it needs. Each machine has its own firewall that allows only its known partners. The database accepts connections from our own servers and nothing else.
- Least privilege. Each application now logs in to the database with its own account, allowed only what that application needs. Old shared accounts are gone, and the monitoring tools can read statistics but not change data. In the coordination layer, each server has its own login and can only change its own entries.
- Encrypted backups, kept in the EU. Backups are encrypted before they leave our servers. The keys to unlock them are kept separately, also encrypted.
Together with the automatic security updates described further down, this means fixes reach every server within days, and a single weak spot exposes far less than it used to.
A database a third of the size
Before building the second database server, I slimmed the database down from about 650 GB to 233 GB, roughly a third of its old size, without deleting anything pilots or airlines rely on.
- Flight tracking data, packed tightly. The position reports and telemetry Pegasus sends during every flight are by far the biggest data we hold. They now live in storage built for time-series data, which compresses older flights heavily while keeping them readable.
- Request logs, restructured. The log of API requests went from 44 GB to 2.9 GB by storing the same information more efficiently.
- Old weather tables removed. About 22 GB of weather data that nothing used any more.
- Empty space reclaimed. Tables that change constantly leave gaps behind. I rebuilt the worst ones while the site stayed up, and removed duplicate indexes that cost disk space and slowed every save.
This made everything after it easier. A smaller database copies to a second server faster, backs up faster and restores in minutes rather than hours. It also needs less disk, which let it run on servers I already had.
Backups, and a plan for the worst day
A second copy of the database protects against a broken server, not against everything. A mistake on our side can be copied to both servers instantly, and a whole data centre can go offline. That is what backups are for, and they live somewhere else entirely.
- What: the full vAMSYS database, about 233 GB.
- How often: a complete copy every Sunday, the day's changes every night, and every individual change streamed out within about a minute. The whole database can be rewound to almost any moment of the past two weeks.
- Where: encrypted, in Cloudflare's storage in the EU, outside the data centre that runs vAMSYS.
- Proven: a backup you have never restored is only a hope. These backups are restored regularly; it is how the second database server was built. A full restore takes 10 to 15 minutes.
These backups are for disaster recovery, not for undoing changes to one airline. A restore rewinds the entire database, for every Virtual Airline at once, back to a moment before something went wrong on our side. It can't bring back one airline's data on its own. If an import or an API integration changes or deletes data in your airline, we can't restore just that airline from these backups, so check imports before running them, and keep your own copy of anything you can't afford to lose.
For the worst case, the data centre itself being gone, there is now a written plan to rebuild vAMSYS at a different provider from those backups. I estimate 3 to 5 hours. The keys needed to unlock the backups are kept in an encrypted kit, updated weekly and stored away from the servers, so a disaster can't take the keys down with the data.
Beyond vamsys.io: updates and reboots everywhere
The same machinery now looks after our other servers too: the one behind vamsys.co.uk, the status page and the feedback board, the support desk, Hangar and the Discord bot, MyNextAirline, SigWX, and a few more.
- Automatic security updates. Every server installs security updates by itself each day.
- Rolling reboots. Some updates only take effect after a restart. The brain keeps a queue and restarts servers one at a time, each in its own slot between 02:00 and 05:00 UTC, and never two parts of the same pair together.
- Ready first, restart second. Before each restart the brain does whatever that server needs: it hands database leadership to the partner, lets running jobs finish, or moves visitors elsewhere. If a server isn't safe to restart that night, it waits for the next one.
- Checked afterwards. After the restart the brain works through the server's own health list, such as its websites answering and its background workers running, before moving on.
- Status page and alerts handled. For services that run on a single server, the brain posts a short maintenance notice on the status page ahead of time and silences the expected alerts for that slot.
The result: updates land within days on every server, nobody stays up at night to apply them, and the status page says what is happening.
Under the hood, for the curious
All of this runs on 24 rented servers at one hosting provider, netcup, in two data centres in Amsterdam and Nuremberg, all in the EU:
| Servers | vCPU | Memory | Disk | |
|---|---|---|---|---|
| Serving vamsys.io directly (app servers, tugs, holds, search) | 9 | 104 | 248 GB | 8.3 TB |
| The rest of the vAMSYS family (homepage, status page, feedback, Hangar, Discord bot, support desk, monitoring) | 4 | 26 | 44 GB | 1.8 TB |
| Our other projects, error tracking, Pegasus build servers and spares | 11 | 88 | 192 GB | 6.4 TB |
| Total | 24 | 218 | 484 GB | 16.5 TB |
14 of them are ARM machines and 10 are x86. Disk is the space allocated, not the space used.
For readers who want the names, these are the main building blocks. All are widely used open-source tools, apart from the brain, which I wrote myself.
| Building block | What it does here |
|---|---|
| PostgreSQL + Patroni | The database, and the referee that keeps the two copies in step and promotes the second copy if the leader fails |
| etcd | The shared notebook across three servers where Patroni and the brain record who leads what; a majority must agree, which is why there are three |
| HAProxy | The front door on each app server, and the local signpost that always points the app at the current database and cache leader |
| Redis + Sentinel | Fast in-memory storage for queues, sessions and the cache, with Sentinel watching and switching leaders when needed |
| pgBackRest | Database backups: weekly full copies, nightly changes and a continuous stream of every change, encrypted, to Cloudflare R2 in the EU |
| Netdata + All Quiet | Monitoring of every server, and the alerts that reach a person when something needs one |
| The brain | Our own coordinator: health checks, load-aware traffic, rolling reboots, status page notices |
Did we do what we said?
When the work started on 20 November, I posted the goals on Discord: no single server should be able to take vAMSYS down, and every server we pay for should pull its weight. Here is how that turned out.
- A new way of balancing traffic across our servers. Done. Every app server has its own front door, and the brain keeps Cloudflare's list of healthy servers up to date.
- Automatic failover for the database. Done, and tested on the live site.
- Redundant queue and live-update servers. Done: two tugs share the queues, background work and live updates.
- Rearranging what runs where. Done. vAMSYS now runs on fewer servers than before, and the old fifth web server became the watchtower. Three servers are spare for now; they'll be put to use as vAMSYS grows and we take on more.
- Fresh operating systems and updates across the board. Mostly done. Nearly every server runs the latest Ubuntu LTS, and all of them install security updates automatically. Four servers, including search and the one that runs Hangar, are still on the previous LTS. That version is supported for about two more years, so they'll be upgraded in good time rather than in a rush.
- No single server can take vAMSYS down. True for vamsys.io itself. Search and the server that runs Hangar and the Discord bot are still single servers, so losing one would take out that feature, not the whole site. And everything is with one hosting provider; the plan to rebuild elsewhere covers the day that matters.
What's next
Over the coming weeks I'll watch how the new setup behaves under real traffic, fine-tune the checks that decide when something counts as down, and rehearse the full disaster plan on servers outside our usual data centre.
For pilots and Virtual Airlines, nothing changes in how you use vAMSYS. If a server fails, you should at worst see a few seconds of errors, not an evening of downtime. As always, live status is on status.vamsys.co.uk, and the new status page post explains what you'll find there.