Last modified: December 03, 2024

This article is written in: 🇺🇸

Multi-Master Replication

Multi-master replication is a database replication model where multiple database nodes, referred to as masters, can perform read and write operations concurrently. Each master node propagates its data changes to every other master node, ensuring consistency across the entire system. This approach enhances scalability, availability, and performance but introduces complexities like conflict resolution and increased configuration overhead.

Understanding the Architecture

To visualize how multi-master replication works, consider the following ASCII diagram:

#
               +---------------------+
               |                     |
               |      Clients        |
               | (Write & Read Ops)  |
               |                     |
               +----------+----------+
                          |
           +--------------+---------------------+----------------------------------+
           |                                    |                                  |
 Write & Read Operations                 Write & Read Operations           Write & Read Operations  
           |                                    |                                  |
           v                                    v                                  v
+----------+-----------+              +---------+----------+             +---------+----------+
|                      | Replication  |                    | Replication |                    |
|    Master Server     | ---------->  |   Master Server    | ----------> |   Master Server    |
|                      | <----------  |                    | <---------- |                    |
+----------+-----------+              +---------+----------+             +---------+----------+

In this setup, each master node is connected to every other master node through replication links. Data changes made on any node are replicated to all other nodes, and clients can connect to any master node for both read and write operations. This interconnected architecture allows for a highly available and scalable system.

Key Characteristics

Multi-master replication has several defining features:

Purpose of Multi-Master Replication

The primary goals of multi-master replication include:

  1. By distributing the write load across multiple nodes, the system can handle higher transaction volumes without a single point becoming a bottleneck.
  2. With multiple masters, the system continues to operate even if one master fails, offering redundancy and resilience.
  3. In geographically dispersed systems, allowing writes to the nearest master reduces latency and improves performance for users in different locations.
  4. Balancing both read and write operations across multiple nodes optimizes resource utilization and prevents any single node from becoming overwhelmed.

Advantages

Implementing multi-master replication offers several benefits:

Challenges

Despite its advantages, multi-master replication introduces several challenges:

Conflict Resolution Strategies

Handling conflicts is a critical aspect of multi-master replication. Various strategies can be employed:

Implementing Multi-Master Replication with MySQL and Galera Cluster

Galera Cluster is a synchronous multi-master replication plugin for MySQL and MariaDB databases. It ensures that transactions are committed on all nodes simultaneously, providing strong data consistency across the cluster.

Prerequisites

To set up a Galera Cluster, you'll need:

Setting Up the Cluster

Installation

Install the database server and Galera Cluster on each master node.

For Ubuntu/Debian systems:

sudo apt-get update
sudo apt-get install mariadb-server mariadb-client galera-3 rsync

For CentOS/RHEL systems:

sudo yum install mariadb-server mariadb-client galera rsync

Note: Replace mariadb with mysql if you prefer MySQL over MariaDB.

Configuring the Database Server

Edit the configuration file, typically located at /etc/mysql/my.cnf or /etc/my.cnf, and add or modify the following settings:

[mysqld]
# Basic Settings
bind-address = 0.0.0.0
default_storage_engine = innodb
binlog_format = ROW
innodb_autoinc_lock_mode = 2

# Galera Provider Configuration
wsrep_on = ON
wsrep_provider = /usr/lib/galera/libgalera_smm.so

# Galera Cluster Configuration
wsrep_cluster_name = "my_galera_cluster"
wsrep_cluster_address = "gcomm://node1_ip,node2_ip,node3_ip"

# Node Specific Configuration
wsrep_node_name = "nodeX"
wsrep_node_address = "nodeX_ip"

# State Snapshot Transfer Method
wsrep_sst_method = rsync

Replace node1_ip, node2_ip, node3_ip with the IP addresses of your master nodes. Also, set nodeX and nodeX_ip to the hostname and IP address of the current node you're configuring.

Securing the Database

Run the following command to secure your database installation:

sudo mysql_secure_installation

Follow the prompts to set a root password and remove anonymous users, enhancing the security of your database servers.

Initializing the Cluster

On the first node only, bootstrap the cluster:

sudo systemctl stop mysql
sudo galera_new_cluster

For older systems, you might use:

sudo /etc/init.d/mysql stop
sudo service mysql bootstrap

Starting MySQL on Other Nodes

On the remaining nodes, start the MySQL service normally:

sudo systemctl start mysql

Or for older systems:

sudo /etc/init.d/mysql start

Verifying Cluster Status

To check the cluster size and ensure all nodes are connected, run the following command on any node:

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

The result should display the total number of nodes in the cluster, confirming that the setup is successful.

Understanding Conflict Resolution in Galera Cluster

How Conflicts Are Detected
Handling Conflicts in Applications

Testing the Cluster

Best Practices

To maximize the benefits of multi-master replication and ensure a stable environment, consider the following best practices:

Table of Contents

    Multi-Master Replication
    1. Understanding the Architecture
      1. Key Characteristics
      2. Purpose of Multi-Master Replication
    2. Advantages
    3. Challenges
    4. Conflict Resolution Strategies
    5. Implementing Multi-Master Replication with MySQL and Galera Cluster
      1. Prerequisites
      2. Setting Up the Cluster
      3. Understanding Conflict Resolution in Galera Cluster
      4. Testing the Cluster
    6. Best Practices