Console Login

Zero-Downtime Database Migration: A Survival Guide for Norwegian Infrastructure

Zero-Downtime Database Migration: A Survival Guide for Norwegian Infrastructure

There are two types of systems engineers: those who have accidentally corrupted a production database during a migration, and those who are lying. Moving data is stressful. Moving active, transactional data while your CEO is refreshing the dashboard is distinctively terrifying.

The standard "maintenance window" approach—shutting down the app, dumping the data, transferring it, and restoring it—is dead. In 2024, users expect 99.99% availability. If you are running a high-traffic e-commerce site targeting Oslo or a SaaS platform serving Europe, a four-hour downtime window is a resignation letter.

I have overseen migrations moving terabytes of data from legacy bare-metal in Frankfurt to virtualized environments in Norway. The physics of latency and I/O throughput are immutable. Here is how you handle a migration without melting your servers or your reputation.

The Pre-Flight Check: IOPS and Latency

Before you even touch a config file, look at the hardware. A common failure mode is attempting to replicate data to a target server that has slower disk I/O than the source. If your source is a dedicated server with standard SSDs, and your target is a cheap VPS with noisy neighbors, the replica will never catch up. The replication lag will grow infinitely until the disk fills up.

We see this constantly at CoolVDS. Engineers try to migrate high-churn databases to instances with capped IOPS. For a smooth migration, the target drive must be NVMe. We standardize on enterprise-grade NVMe storage for this exact reason—write speeds need to exceed the rate of incoming binary logs.

Network Latency: The Silent Killer

If you are moving data into Norway to benefit from local data residency laws and green energy, you need to check the path. If your source is in the US and your target is in Oslo, the 90ms+ latency will affect synchronous replication. Always opt for asynchronous replication for cross-border moves, or ensure you are utilizing a direct peering path.

Strategy: The Master-Slave Replication Swap

Forget `dump` and `restore` for anything larger than 5GB. The only professional way to migrate a live database is to set up the new server as a replica (slave), let it sync, and then promote it to master.

1. MySQL / MariaDB Configuration (GTID)

If you are still using binary log positions (filename + offset), stop. It is 2024. Use Global Transaction Identifiers (GTID). It makes failover and tracking consistency significantly easier.

On your Source Server (my.cnf):

[mysqld]
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
gtid_mode = ON
enforce_gtid_consistency = ON
binlog_format = ROW

On your Target CoolVDS Server:

[mysqld]
server-id = 2
gtid_mode = ON
enforce_gtid_consistency = ON
log_replica_updates = ON
read_only = 1

Critical Security Note: Do not expose port 3306 to the public internet. If you do not have a private VLAN between providers (which is rare), use an SSH tunnel. It adds negligible overhead compared to the security risk of an open DB port.

# Establish the tunnel from Target to Source
ssh -N -L 3307:127.0.0.1:3306 user@source-ip -i ~/.ssh/id_rsa

2. Taking the Snapshot

Use Percona XtraBackup for MySQL or `pg_basebackup` for PostgreSQL. These tools allow you to take a consistent snapshot without locking the database for writes.

# MySQL: Streaming backup directly to the new server via netcat (unsafe but fast) or SSH
xtrabackup --backup --stream=xbstream --target-dir=./ | ssh user@target-ip "xbstream -x -C /var/lib/mysql/"

The PostgreSQL Approach

PostgreSQL handles streaming replication robustly. Ensure your `postgresql.conf` on the source allows enough replication slots.

# postgresql.conf (Source)
wal_level = replica
max_wal_senders = 10
max_replication_slots = 10

# pg_hba.conf (Source)
# Allow the CoolVDS IP to connect
host    replication     rep_user        185.x.x.x/32          scram-sha-256

Initiate the base backup on the CoolVDS target:

pg_basebackup -h source-ip -D /var/lib/postgresql/16/main -U rep_user -P -v -X stream -C -S slot_migration
Pro Tip: Always set `synchronous_commit = off` on the source during the initial sync if you are paranoid about latency affecting the master's write performance. You can tighten consistency requirements once the catch-up phase is done.

Compliance: The Norwegian Context

Moving data to Norway isn't just about latency; it's about sovereignty. Since the Schrems II ruling, EU and EEA companies are hyper-sensitive about data leaving the EEA. Hosting in Norway (which is part of the EEA framework) provides strong legal protection.

However, the Datatilsynet (Norwegian Data Protection Authority) is strict. When migrating:

  • Ensure encryption in transit (SSL/TLS) is mandatory.
  • Verify that your hosting provider (like CoolVDS) does not silently backup data to non-EEA locations.
  • Update your Record of Processing Activities (ROPA) to reflect the new physical location of the data.

The Cutover: Minimal Downtime

Once your replication lag is at 0 seconds, you are ready.

  1. Lower TTL: Drop your DNS TTL to 60 seconds a day before.
  2. Stop Writes: Put your application in maintenance mode or set the source DB to `read_only = 1`.
  3. Verify Sync: Run `SHOW MASTER STATUS` on source and `SHOW REPLICA STATUS` on target. Ensure coordinates match.
  4. Promote Target:
    # MySQL
    STOP REPLICA;
    RESET REPLICA ALL;
    SET GLOBAL read_only = 0;
  5. Switch Traffic: Update your application config or switch the Floating IP.

Why Infrastructure Matters

You can script the perfect migration, but if the underlying host chokes on I/O during the catch-up phase, you will fail. Database replication is essentially a massive sequential write operation followed by random write operations.

We designed the CoolVDS stack with this specific workload in mind. We don't oversubscribe our CPU cores, meaning when your database needs to process a backlog of binary logs, the cycles are there. Combined with local NVMe storage, we reduce the "catch-up" time significantly.

Don't let a slow disk be the reason you explain downtime to your stakeholders. Test your migration strategy on a high-performance instance first.

Ready to secure your data in Norway? Deploy a CoolVDS instance in under 55 seconds and benchmark the difference yourself.