Keep your app and database running when one VM dies
Many apps run on a single VM: the web app and its database side by side. It's simple, but when that VM goes down, your customers stop too. This guide moves such an app onto two VMs that take over for each other, without changing the app's code.
Test results
A simple point-of-sale (POS) app writes one transaction per second. The main VM holds the web app and the database writer and is powered off hard while transactions are running. The two VMs are in different regions.
| Test | Gap until the second VM serves again | Lost transactions |
|---|---|---|
| Ruby 2.5 + ActiveRecord 5.2 + mysql2, data from MySQL 8, MariaDB 10.11 cluster (Singapore + Bangalore), 1 Oct 2026 | about 13 seconds | 0 |
| Node.js, PostgreSQL 18 cluster in synchronous mode, 30 Sep 2026 | about 21 seconds | 0 |
When the main VM comes back it rejoins by itself, catches up on missed data, and visitors return to it without data loss. During the gap the cashier sees an error and has to retry; a transaction reported as successful is never lost.
Layout: 2 VMs per customer + 1 shared witness
| VM | Runs | Size |
|---|---|---|
| A (region 1) | Web app + database | Like your current VM |
| B (region 2) | Copy of the web app + database, always in sync | Same as A |
| Witness (region 3) | No data. Only votes, so there are never two writers. | Small (1 vCPU, 1 GB) |
- A and B must be in different regions or providers: that is what keeps the app running when one region has trouble.
- One witness VM can be shared by many customers. Each cluster gets its own network and port on that witness. For 50 customers: 50 extra VMs in the second region + 1 small witness (preferably 2 witnesses splitting the load, so one witness going down never touches every customer).
- A data VM serves one cluster (one customer) only.
- A and B swap roles automatically. When A comes back it catches up on its own; nothing needs copying by hand.
Choose the database
| Your app uses | Saka cluster |
|---|---|
| MySQL 8 / MariaDB, older app (e.g. Ruby 2.5, PHP 7, Docker images based on Debian 10) | MariaDB 10.11 (LTS until 2028) |
| MySQL 8 / MariaDB, newer app | MariaDB 12.3 |
| PostgreSQL | PostgreSQL 18 (or 17) |
Old MySQL client libraries (libmariadb 3.1, the default in Ruby 2.5 images) read every text value as the number 0 from MariaDB 11.4 and newer. We tested it: 10.11 is correct, 11.4, 11.8, and 12.3 are not. Older apps must use 10.11.
Steps
1. Connect the VMs to Saka
Prepare VMs A and B (and a witness if you don't have one yet), then install the Saka agent on each, including the old VM. See Getting started.
2. Create a database cluster
- Open Databases, press + Create database cluster, choose the engine and version from the table above.
- Data servers: A and B. Witness: the shared witness VM.
- PostgreSQL: mode Zero data loss (synchronous). MariaDB is always synchronous.
The cluster is usually ready in 1 to 5 minutes; you're told on Telegram.
3. Move the old database
- Stop the old app so no new transactions happen during the copy (e.g. after closing time).
- On the cluster page, card Move an existing database into the cluster: choose the old VM, paste the old database address as in the app's config, e.g.
mysql://kasir:[email protected]:3306/kasirorpostgresql://kasir:[email protected]:5432/kasir, and press Copy to cluster. - Saka copies tables, indexes, views, triggers, and routines, then compares the row count of every table. Test result from MySQL 8: 100,302 rows in 30 seconds, counts identical.
- From MySQL 8 to MariaDB:
utf8mb4_0900_*collations are mapped to their equivalents, MyISAM tables become InnoDB (MyISAM isn't replicated to the second location), and old DEFINERs are dropped. Tables without a primary key are reported. - If it fails, the target database it created is removed again and the old database is never modified. The error names the cause (wrong password, old database not answering, and so on).
- The old database password is only passed to the old VM for the copy and is not stored by Saka.
- Via API:
POST /api/v1/database/<id>/impor {"server_id", "sumber", "database"}. Via an AI agent:database_pindahkan_lama.
4. Deploy the app on A
- Open Sites, Create site, App from Git, choose server A. A repo with a
Dockerfileis used as is (e.g.FROM ruby:2.5); see Apps from Git. - On the cluster page, card Connect app servers: install the proxy on A. It always leads to a healthy database node and the same single writer, whichever VM that is.
- Set the app's
DATABASE_URLto the proxy address for containers (the port is shown on the card; press Show password for the password):Rails (mysql2)DATABASE_URL=mysql2://root:[email protected]:13306/kasir?ssl_mode=required - Deploy, open the app, and check that the old data is there.
The app must reconnect to the database when its connection drops. Rails, Laravel, Django, and Prisma do this by themselves: the connection is checked each time it is borrowed from the pool. The test above used exactly this Rails behavior, with no extra settings.
5. Add the second location on B
- On the app page, card Two locations, press Add a second location and choose B. B builds the same commit with the same variables; the database proxy on B is installed automatically on the same port.
- Change the app domain's DNS to a CNAME pointing at the load balancer address shown on the card (e.g.
lbxxxx.lb.saka.work). A bare domain (e.g. shop.com) needs a DNS provider with CNAME flattening, such as Cloudflare; or useapp.shop.com. - HTTPS is set up on both VMs. Every deploy, rollback, and variable change on A is applied to B too.
6. Test it yourself (recommended)
Outside busy hours, power off VM A from your cloud provider's panel. Within about 30 seconds the app opens again from B with all data. Power A back on; it rejoins by itself. You're told on Telegram at each step.
One-click DNS switch & failover drill
When an app server dies, visitors need to be pointed at the standby. In Settings, One-click DNS switch stores one Cloudflare A record with two addresses (primary and standby): one click switches, one click switches back. The Cloudflare token is limited to one zone (Zone Read and DNS Edit), stored encrypted, and never shown again.
- Monitor the standby: an external monitor can connect to the standby IP while sending the site's Host and SNI, so the standby and its certificate are watched without changing DNS.
- Failover drill: really switches the record to the standby, measures how many seconds 1.1.1.1 and 8.8.8.8 take to follow, checks the standby serves, holds, switches the record back and measures again. The report goes to Telegram. The record is always switched back, and the drill does not start if the standby is unhealthy. Real visitors move during the drill.
- Readiness: the Readiness section on the Servers page summarizes replicas, backups and their restore tests, DNS TTL and standby health in one view.
One thing you still prepare yourself: scheduled jobs (cron) must run on only one server at a time, so in a real failover enable the scheduler on the standby following your app's runbook.
With an AI agent
Connect Saka to Claude, ChatGPT, or another AI agent (see Connect AI), add the VMs in the panel, then simply ask, e.g. "Make the POS for customer Toko Maju survive a region outage: VM toko-maju-sg as main, toko-maju-blr as backup, witness-1 as witness. Ruby 2.5 app, MySQL 8 database on the old VM toko-maju." The AI calls panduan_ha, drafts a plan, asks for your approval, then runs every step above and reports the results. Steps only you can do are still asked of you: installing the agent on new VMs, stopping the old app, and changing DNS.
Good to know
- Many customers, one app: group the app on every VM into a fleet for staged deploys (canary first, auto-stop on failure) and shared variables.
- Surviving a VM outage is not a backup. Data deleted by mistake is copied to B too. Also turn on backups & point-in-time restore to your S3 bucket, so data can be brought back to any second.
- IDs skip numbers on a MariaDB cluster (e.g. 65536, 65539, 65542): Galera spreads numbers across nodes so they never clash. IDs stay unique and increasing. If the app prints receipt numbers from the ID and customers expect gapless numbers, generate receipt numbers separately.
- Tables without a primary key should get one: on a MariaDB cluster, UPDATE/DELETE on such tables is slower and very large transactions can be rejected.
- Uploaded files (product photos, attachments) on the VM's disk are not copied for Git apps. Store them in S3 storage (e.g. Cloudflare R2, DigitalOcean Spaces) or in the database.
- Workers and cron run on A. If A is unreachable for 3 minutes, B takes over, and hands them back once A recovers, without ever running both at once.
- Visitors whose DNS answer is still cached (~30 seconds) may briefly be sent to the dead A before moving to B.
- Each write waits for the second location to agree: about one network round trip between regions extra (tens of milliseconds across countries).
Technical details: Managed databases and Sites in two locations.