Junior
What is replication in the context of databases?
sobes.tech AI
Answer from AI
Replication is the process of copying data from one database (the master) to one or more other databases (replicas) in real-time or with a slight delay.
Main goals of replication:
- Increased availability. In case of master failure, a replica can take over the load.
- Faster read speeds. Read requests are distributed between the master and replicas.
- Backup. A replica can be used as an up-to-date copy of data.
- Analytics. Analytical queries can be performed on replicas without burdening the master.
Types of replication:
- Asynchronous. The master does not wait for confirmation from the replica about applying changes. There may be a slight delay in data.
- Synchronous. The master waits for confirmation from the replica. Ensures data consistency but may slow down writing on the master.
- Semi-synchronous. A compromise between asynchronous and synchronous. The master waits for confirmation from at least one replica.
Example of MySQL configuration:
// my.cnf on the master
[mysqld]
server-id = 1
log_bin = mysql-bin
// my.cnf on the replica
[mysqld]
server-id = 2
relay_log = mysql-relay-bin
read_only = 1 // The replica can be set to read-only
Replica setup:
// On the replica
CHANGE MASTER TO
MASTER_HOST='<Master_IP>',
MASTER_USER='<repl_user>',
MASTER_PASSWORD='<repl_password>',
MASTER_LOG_FILE='<binlog_file>', // Obtained from show master status on the master
MASTER_LOG_POS=<binlog_position>; // Obtained from show master status on the master
START SLAVE;