Console Login

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

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

I still wake up in a cold sweat remembering a migration I botched in 2018. We were moving a high-traffic payment gateway from a legacy provider in Germany to a local setup. I relied on mysqldump, underestimated the restore time by three hours, and cost the client five figures in lost transactions. I learned the hard way: Maintenance windows are a myth.

If you are shutting down your application to move your database, you have already failed. In 2021, users expect 100% availability. Furthermore, with the recent Schrems II ruling from the CJEU effectively invalidating the Privacy Shield, moving data back to sovereign European (and specifically Norwegian) infrastructure is no longer just a performance upgrade—it is a legal necessity for many of us handling PII.

This is not a tutorial for hobbyists. This is how battle-hardened engineers move terabytes of data across borders while the engine is still running.

The Architecture of Fearless Migration

Forget offline dumps. The only viable strategy for production systems is Replication-Based Migration. You treat your new CoolVDS instance as a read-replica (slave) of your current production master. Once the replica catches up, you promote it to master.

This requires three things:

  1. Low Latency: If the network lag between your old provider and Oslo is too high, replication lag will never hit zero.
  2. High I/O: Catching up requires massive disk write speeds. Spinning rust or cheap SSDs will choke.
  3. Strict Configuration: Default kernel settings are not ready for this data firehose.

Step 1: The Network Tunnel

Never send database traffic over the public internet without encryption. It is slow and reckless. Establish a secure tunnel. If you are using SSH (which you should), disable the encryption overhead for the data stream if you are already inside a WireGuard VPN, or use a high-performance cipher.

ssh -L 3307:127.0.0.1:3306 user@target-coolvds-ip -N -f

However, for massive transfers, standard TCP settings in Linux are often too conservative. Before you start the sync, tune the TCP stack on your CoolVDS target server to handle the influx from the NIX (Norwegian Internet Exchange) or continental Europe.

Edit /etc/sysctl.conf:

# Increase TCP window sizes for high-bandwidth/high-latency links
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216

# Protect against SYN flood during the vulnerable switch-over phase
net.ipv4.tcp_syncookies = 1

# Enable fast recycling of TIME_WAIT sockets
net.ipv4.tcp_tw_reuse = 1

Run sysctl -p to apply. This ensures your network buffer doesn't become the bottleneck before your disk does.

Step 2: Preparing the Target (The CoolVDS Advantage)

Here is where most generic VPS providers fail. When you start pulling data, your Disk I/O Usage will spike. If you are on a noisy neighbor platform, your iowait will skyrocket, and the replication will lag indefinitely. We use KVM at CoolVDS specifically to isolate resources, but you still need to configure your database engine to utilize the NVMe storage effectively.

Pro Tip: During the initial import (the "Catch-up" phase), you can relax durability requirements to speed up writes by 300%. Just remember to revert this before going live.

MySQL / MariaDB Optimization

If you are running MySQL 8.0, create a dedicated configuration file /etc/mysql/conf.d/migration.cnf for the import process:

[mysqld]
# DANGEROUS: Only for initial sync. Reduces disk flush frequency.
innodb_flush_log_at_trx_commit = 2

# Disable binary logging on the target until it becomes master
skip-log-bin

# Maximize memory usage for bulk inserts (assuming 16GB RAM instance)
innodb_buffer_pool_size = 10G
innodb_log_buffer_size = 256M
innodb_write_io_threads = 16
innodb_io_capacity = 2000 # Crank this up for NVMe

Restart the service. If you don't set innodb_io_capacity high enough, MySQL will treat your lightning-fast NVMe drive like a spinning hard disk from 2010.

Step 3: The Data Sync

For PostgreSQL, use pg_basebackup. It allows you to stream the data directory without stopping the master.

pg_basebackup -h source_ip -D /var/lib/postgresql/13/main -U replicator -P -v --wal-method=stream

For MySQL, use Percona XtraBackup. It creates a hot backup without locking tables. Do not use mysqldump for anything over 5GB; the table locking will kill your production application's performance.

Once the base data is on the CoolVDS instance, configure it as a replica. You will need the Master Log File and Position from the source.

CHANGE MASTER TO MASTER_HOST='10.8.0.1', MASTER_USER='repl', MASTER_PASSWORD='secure_pass', MASTER_LOG_FILE='mysql-bin.000342', MASTER_LOG_POS=482910;

Start the slave:

START SLAVE;

Now, watch the gap close. Monitor it with:

watch -n 1 "mysql -e 'SHOW SLAVE STATUS\G' | grep Seconds_Behind_Master"

On a standard SATA VPS, this number often hovers around 300-400 seconds and never drops because the disk cannot write the relay logs fast enough. On our NVMe infrastructure, you should see this drop to 0 within minutes of the base import finishing.

Step 4: The Cutover (The Moment of Truth)

When Seconds_Behind_Master is 0, you are ready. The goal is to switch writes to the new Norway-based server with sub-second downtime.

  1. Lower TTLs: 24 hours before migration, drop DNS TTL to 60 seconds.
  2. Block Writes on Source: This prevents "split brain" scenarios.
FLUSH TABLES WITH READ LOCK; SET GLOBAL read_only = ON;

3. Verify Sync: Ensure the CoolVDS instance has executed the final transaction.

4. Promote Target: Stop the slave and make it writable.

STOP SLAVE;
RESET SLAVE ALL;
SET GLOBAL read_only = OFF;
# Revert the "dangerous" my.cnf settings now!
SET GLOBAL innodb_flush_log_at_trx_commit = 1;

5. Switch App Config: Point your web servers to the new IP. If your web servers are also migrating to CoolVDS (recommended for that internal latency), this is just an internal IP change.

Why Infrastructure Matters

I have seen scripts perfect in theory fail in practice because the target hardware had high Steal Time (CPU contention). When you are parsing binary logs for replication, a 5% CPU steal caused by a noisy neighbor can delay a transaction just enough to cause a replication error.

At CoolVDS, we guarantee dedicated resource allocation. When you are migrating under the scrutiny of GDPR/Schrems II requirements, you need to know exactly where your data lives and that the hardware beneath it isn't oversubscribed.

Final Checklist

  • Check latency from your app servers to the new DB: ping -c 100 new_db_ip
  • Ensure max_connections matches your application pool.
  • Verify backups are running on the NEW server immediately.

Don't let slow I/O kill your SEO or your uptime. If you are planning a migration, deploy a test instance on CoolVDS in 55 seconds and benchmark the difference yourself.