Master-master database replication for web application

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Our competencies:
Development stages
Latest works
  • image_website-b2b-advance_0.png
    B2B ADVANCE company website development
    1212
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_websites_belfingroup_462_0.webp
    Website development for BELFINGROUP
    852
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    822
  • image_bitrix-bitrix-24-1c_fixper_448_0.png
    Website development for FIXPER company
    815

Setting Up Master-Master Replication for Web Application Databases

Master-Master (Multi-Primary) replication — scheme where writes are possible on multiple nodes simultaneously. More complex than Master-Slave, solves geographically distributed write tasks and high availability with immediate failover.

When Master-Master is justified

Most web applications work fine on Master-Slave. Master-Master is needed when:

  • Applications in different regions must write to local DB with subsequent sync
  • Zero switch time is required if one master fails
  • Write cannot be directed through single point without unacceptable delays

MySQL: Galera Cluster

Galera — synchronous multi-primary replication for MySQL/MariaDB. All nodes accept writes, transactions apply on all nodes before commit.

# /etc/mysql/conf.d/galera.cnf (on each node)
[mysqld]
binlog_format = ROW
default_storage_engine = InnoDB
innodb_autoinc_lock_mode = 2
bind-address = 0.0.0.0

# Galera Provider
wsrep_on = ON
wsrep_provider = /usr/lib/galera/libgalera_smm.so
wsrep_cluster_name = "production_cluster"
wsrep_cluster_address = "gcomm://192.168.1.10,192.168.1.11,192.168.1.12"
wsrep_sst_method = rsync

# Unique for each node
wsrep_node_address = "192.168.1.10"
wsrep_node_name = "node1"

Initialize cluster on first node:

galera_new_cluster
# On other nodes — normal start
systemctl start mysql

Check status:

SHOW STATUS LIKE 'wsrep_%';
-- wsrep_cluster_size should show node count
-- wsrep_ready should be ON

PostgreSQL: BDR (Bi-Directional Replication)

BDR — extension from pgEdge, supports async multi-master for PostgreSQL:

-- Connect extension
CREATE EXTENSION bdr;

-- Initialize first node
SELECT bdr.bdr_group_create(
  local_node_name := 'node1',
  node_external_dsn := 'host=192.168.1.10 port=5432 dbname=myapp'
);

-- Join second node
SELECT bdr.bdr_group_join(
  local_node_name := 'node2',
  node_external_dsn := 'host=192.168.1.11 port=5432 dbname=myapp',
  join_using_dsn := 'host=192.168.1.10 port=5432 dbname=myapp'
);

Alternative — Patroni + Postgres in synchronous replication mode. This is not true multi-master, but provides automatic failover with replica promotion to master in seconds.

Resolving write conflicts

Main difficulty of Master-Master — conflicts when two nodes simultaneously change same record.

Resolution strategies:

Strategy Approach Risk
Last Write Wins Latest by timestamp wins Data loss
Origin wins Source node wins Predictable, but not always right
Custom resolver Merge business logic Complex to implement
Application-level Application prevents conflicts Requires architectural changes

For most cases, better to prevent conflicts: each region writes own data (different tables or row-level partitioning by region_id).

Nginx/HAProxy for write balancing

upstream mysql_masters {
    server 192.168.1.10:3306 weight=1;
    server 192.168.1.11:3306 weight=1;
}

For Galera: all three nodes accept writes, ProxySQL handles distribution:

INSERT INTO mysql_servers(hostgroup_id, hostname, port, weight)
VALUES (10, '192.168.1.10', 3306, 1),
       (10, '192.168.1.11', 3306, 1),
       (10, '192.168.1.12', 3306, 1);

Monitor Galera

-- Queue of incoming transactions to apply (should be small)
SHOW STATUS LIKE 'wsrep_local_recv_queue_avg';

-- Certification conflicts (should be near zero)
SHOW STATUS LIKE 'wsrep_local_cert_failures';

-- Node status
SHOW STATUS LIKE 'wsrep_cluster_size';

Limitations

  • Galera doesn't support MyISAM and MEMORY tables
  • AUTO_INCREMENT requires innodb_autoinc_lock_mode=2 + wsrep_auto_increment_control=ON
  • DDL operations (ALTER TABLE) block cluster — use pt-online-schema-change or gh-ost
  • Latency between nodes directly affects write performance in Galera

Timeline

Setup of Galera Cluster on three nodes with ProxySQL — 3–4 business days. Including load testing and monitoring setup.