MariaDB replication to a server behind NAT
A standby copy of a database does not have to live in a second data centre. An office server or a machine at home will do, if the two can talk. Usually they cannot: one of them sits behind a router that lets nothing in.
Why plain replication stops at the router
MariaDB replication is a connection the replica opens to the master on port 3306. That works when the master is reachable. It also means a database port open to the internet, or a VPN that somebody has to build and keep alive.
The harder case is the day the roles turn. After a switch the old master has to follow the new one, so the connection must work in the other direction too. A setup that only works one way is a standby you cannot switch to and back from.
What ZeroBehind does instead
Each server runs one agent. The agents build an encrypted WireGuard tunnel between the two servers, and replication, the control channel and the heartbeat all travel inside it.
- Only one of the two servers has to accept incoming packets, on a single UDP port. The server behind the router calls out.
- Replication connects through the tunnel, so port 3306 does not have to be opened to the internet on either side.
- Inside the tunnel both servers reach each other, so a role switch works in both directions.
- Pairing is a one-time code. Each agent has its own certificate authority and pins the identity of the other side.
There is no port forwarding to configure on the router and no separate VPN to maintain. Your data travels directly between your two servers; it does not pass through us.
What the link needs
Replication is asynchronous, so a slow or lossy link does not slow the master down. It only decides how far the replica can fall behind at a busy moment. The dashboard shows that gap in transactions, counted from GTID positions on both servers, together with the round trip time of the tunnel.
When the link drops, the replica reconnects by itself and continues from the transaction it stopped at. Nothing is copied again.
Measured, not promised
We run this ourselves: a public server and a home server behind NAT. Under continuous writes a planned role switch took 3.5 seconds from start to finish, the application saw a pause of under half a second, and no acknowledged write was lost. Copying 1.2 GiB to a new replica over the internet took about 30 seconds. Your numbers depend on your data and your link.
Limits worth knowing
- One of the two servers needs a public address, or at least one UDP port forwarded to it. Two servers that are both hidden behind routers cannot find each other.
- It is one master and one replica per pair, not a cluster with several writable nodes.
- After a role switch you point your applications to the new master yourself.
Want to see it on your servers?
Tell us what you run. We answer with what is covered and what is not.
Talk to us