MySQL High Availability: A Hands-On Guide to Galera Multi-Master Clusters

Database tutorial - IT technology blog
Database tutorial - IT technology blog

The 3 AM Database Emergency

Few things kill a weekend like a 3 AM page reporting that your primary database node has crashed. In a standard single-master setup, a failure means your application stays dark until you manually promote a slave or restore the service. After managing MySQL, PostgreSQL, and MongoDB across various production environments, I’ve found that Galera Cluster offers one of the most reliable ways to keep data accessible without manual intervention.

Setting up a Galera Cluster is more than just running scripts. It requires a solid grasp of the architecture that maintains uptime even if an entire data center goes offline. This guide walks through a battle-tested configuration for high availability.

Why Multi-Master Beats Traditional Replication

Why choose Galera over standard MySQL replication? Most traditional setups use asynchronous replication. In that model, the master writes data and then sends logs to the slave. If the master hardware fails before those logs transfer, you lose data. For a fintech app or an e-commerce platform processing $10,000 in sales per hour, that risk is unacceptable.

Galera Cluster utilizes Synchronous Multi-Master Replication. Every node in your cluster is an identical peer. When Node A receives a write, it immediately broadcasts it to Node B and Node C. The transaction only commits once the entire cluster reaches a consensus. This means every node always has the most recent data, regardless of where the write originated.

Evaluating the Trade-offs

Galera is powerful, but it isn’t a one-size-fits-all fix. You must understand the performance costs before migrating your production workloads.

The Advantages

  • True High Availability: There is no single point of failure. If one server drops, the others continue serving traffic without a hiccup.
  • Guaranteed Data Integrity: Synchronous commits mean zero data loss during a node failure.
  • Scalable Reads: You can distribute read queries across all nodes. In a 3-node cluster, this effectively triples your read throughput.
  • Self-Healing: New nodes join and sync via State Snapshot Transfers (SST) automatically.

The Challenges

  • Increased Write Latency: Expect a latency penalty of 5ms to 15ms per write. Every node must acknowledge the transaction before it completes.
  • The Weakest Link Effect: If Node C has a slow CPU or disk, it will throttle the performance of Node A and Node B.
  • Rollback Conflicts: If two users update the exact same row on different nodes at the same millisecond, the cluster will force one to roll back.

The 3-Node Production Standard

Always aim for a 3-node minimum in production. This avoids the dreaded “split-brain” scenario. If a 2-node cluster loses its interconnect, both nodes might assume the other is dead and try to take control. With three nodes, the two that can still communicate form a “quorum” (a majority). They stay online while the isolated node safely shuts itself down to prevent data corruption.

For this setup, we will use Ubuntu 22.04 and MariaDB. Before starting, ensure your security groups allow traffic on these specific ports:

  • 3306: Standard MySQL traffic
  • 4567: Galera Cluster replication
  • 4568: Incremental State Transfer (IST)
  • 4444: State Snapshot Transfer (SST)

Step-by-Step Implementation

1. Deploy MariaDB

Update your package manager and install the MariaDB server on all three nodes simultaneously.

sudo apt update
sudo apt install mariadb-server -y

2. Define the Galera Configuration

Create a custom configuration file at /etc/mysql/mariadb.conf.d/60-galera.cnf. While the cluster address remains the same for all nodes, you must customize the wsrep_node_address for each server.

[galera]
# Core Cluster Settings
wsrep_on                 = ON
wsrep_provider           = /usr/lib/galera/libgalera_smm.so
wsrep_cluster_name       = "prod_cluster_01"
wsrep_cluster_address    = "gcomm://10.0.0.1,10.0.0.2,10.0.0.3"
binlog_format            = row
default_storage_engine   = InnoDB
innodb_autoinc_lock_mode = 2

# Node Identity
wsrep_node_address       = "10.0.0.1" # Use the local IP of the node
wsrep_node_name          = "node-01"    

3. Initialize the Cluster

You cannot start all nodes at once. The first node needs to “bootstrap” the cluster because there are no peers to join yet. On Node 1, stop the service and run the bootstrap command:

sudo systemctl stop mariadb
sudo galera_new_cluster

Node 1 is now the primary component of your new network.

4. Connect the Peers

On Node 2 and Node 3, simply restart the MariaDB service. They will see the IPs in the configuration, reach out to Node 1, and begin the synchronization process.

sudo systemctl restart mariadb

5. Confirm Cluster Health

Verify the setup by logging into the MariaDB shell. Check the cluster size to ensure all nodes are visible.

mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size';"

If the output returns 3, your multi-master cluster is officially live.

Lessons from the Field

Building the cluster is the first step, but maintaining it requires specific operational habits. Here are three lessons I’ve learned from managing large-scale deployments.

Optimize for IST over SST

When a node restarts, it performs a State Transfer. SST is a full data wipe and copy, which can saturate your network if you have 500GB of data. IST only sends missing transactions. To favor IST, increase your gcache.size to something substantial, like 1G or 2G, depending on your write volume. This allows nodes to recover from short outages without a full resync.

Implement a Load Balancer

Galera synchronizes the data, but it doesn’t manage your traffic. If your app points to a single IP, you still have a single point of failure. Use ProxySQL or HAProxy to sit in front of the nodes. These tools monitor node health and automatically reroute traffic if one server becomes unresponsive.

Manage Large Write Operations

Galera excels at small, rapid transactions. However, running a DELETE on 5 million rows in one go can cause the entire cluster to stall. This happens because every node must process that massive write set at the same time. Always batch your large operations into smaller chunks of 2,000 to 5,000 rows to keep the cluster responsive.

Moving to a high-availability model requires a different operational approach than managing a single server. While Galera adds some initial complexity, the security of knowing your data is replicated in real-time across multiple failure domains is worth the investment.

Share: