Enhance data consistency with semisynchronous replication. Ensure that the primary waits for at least one replica to acknowledge receipt of a transaction before committing.
Standard MariaDB replication is asynchronous, but MariaDB also provides a semisynchronous replication option. The feature is built into the server and is always available. In versions prior to , it was a separate plugin that needed to be installed.
With regular asynchronous replication, replicas request events from the primary's binary log whenever the replicas are ready. The primary does not wait for a replica to confirm that an event has been received.
With fully synchronous replication, all replicas are required to respond that they have received the events. See Galera Cluster.
Semisynchronous replication waits for just one replica to acknowledge that it has received and logged the events.
Semisynchronous replication therefore comes with some negative performance impact, but increased data integrity. Since the delay is based on the roundtrip time to the replica and back, this delay is minimized for servers in close proximity over fast networks.
Semisynchronous replication is built into the server. See for more information.
Semisynchronous replication can be enabled by setting the relevant system variables on the primary and the replica.
If a server needs to be able to switch between acting as a primary and a replica, then you can enable both the primary and replica system variables on the server. For example, you might need to do this if is being used to enable auto-failover or switchover with MariaDB Monitor.
Semisynchronous replication can be enabled on the primary by setting the system variable to ON. It can be set dynamically with . For example:
It can also be set in a server in an prior to starting up the server. For example:
Semisynchronous replication can be enabled on the replica by setting the system variable to ON. It can be set dynamically with . For example:
It can also be set in a server in an prior to starting up the server. For example:
When switching between semisynchronous replication and asynchronous replication on a replica with already running, the replica I/O thread will need to be restarted. For example:
If this is not done, then the replica IO thread will continue to use the previous setting.
In semisynchronous replication, only after the events have been written to the relay log and flushed does the replica acknowledge receipt of a transaction's events. If the replica does not acknowledge the transaction before a certain amount of time has passed, then a timeout occurs and the primary switches to asynchronous replication. This will be reflected in the primary's with messages like the following:
When this occurs, the status variable will be switched to OFF.
When at least one semisynchronous replica catches up, semisynchronous replication is resumed. This will be reflected in the primary's with messages like the following:
When this occurs, the status variable will be switched to ON.
The number of times that semisynchronous replication has been switched off can be checked by looking at the value of the status variable.
If you see a lot of timeouts like this in your environment, then you may want to change the timeout period. The timeout period can be changed by setting the system variable. It can be set dynamically with . For example:
It can also be set in a server in an prior to starting up the server. For example:
To determine a good value for the system variable, you may want to look at the values of the and status variables.
In semisynchronous replication, there are two potential points at which the primary can wait for the replica acknowledge the receipt of a transaction's events. These two wait points have different advantages and disadvantages.
The wait point is configured by the system variable. The supported values are:
AFTER_SYNC
AFTER_COMMIT
It can be set dynamically with . For example:
It can also be set in a server in an prior to starting up the server. For example:
When this variable is set to AFTER_SYNC, the primary performs the following steps:
Prepares the transaction in the storage engine.
Syncs the transaction to the .
Waits for acknowledgement from the replica.
Commits the transaction to the storage engine.
The effects of the AFTER_SYNC wait point are:
All clients see the same data on the primary at the same time; after acknowledgement by the replica and after being committed to the storage engine on the primary.
If the primary crashes, then failover should be lossless, because all transactions committed on the primary would have been replicated to the replica.
However, if the primary crashes, then its may also contain events for transactions that were prepared by the storage engine and written to the binary log, but that were never actually committed by the storage engine. As part of the server's process, the server may recover these prepared transactions when the server is restarted. This could cause the "old" crashed primary to become inconsistent with its former replicas when they have
been reconfigured to replace the old primary with a new one.
The old primary in such a scenario can be re-introduced only as a .
The server post-crash recovery of the server configured with rpl_semi_sync_slave_enabled = ON
When this variable is set to AFTER_COMMIT, the primary performs the following steps:
Prepares the transaction in the storage engine.
Syncs the transaction to the .
Commits the transaction to the storage engine.
Waits for acknowledgement from the replica.
The effects of the AFTER_COMMIT wait point are:
Other clients may see the committed transaction before the committing client.
If the primary crashes, then failover may involve some data loss, because the primary may have committed transactions that had not yet been acknowledged by the replicas.
rpl_semi_sync_master_enabledDescription: Set to ON to enable semi-synchronous replication primary. Disabled by default.
Command line: --rpl-semi-sync-master-enabled[={0|1}]
Scope: Global
Dynamic: Yes
rpl_semi_sync_master_timeoutDescription: The timeout value, in milliseconds, for semi-synchronous replication in the primary. If this timeout is exceeded in waiting on a commit for acknowledgement from a replica, the primary will revert to asynchronous replication.
When a timeout occurs, the status variable will also be switched to OFF.
See for more information.
rpl_semi_sync_master_trace_levelDescription: The tracing level for semi-sync replication. Four levels are defined:
1: General level, including for example time function failures.
16: More detailed level, with more verbose information.
rpl_semi_sync_master_wait_no_slaveDescription: If all replicas have disconnected from the primary (i.e. is 0), this variable controls whether or not the primary will still suspend the next transaction's (and any others that commit within duration) commit phase to wait for a replica to connect or reconnect, and send an ACK. If set to ON, the default, the replica count may drop to zero, and the primary will still wait for the timeout period for the next transaction, and any more that commit after this transaction within the semi-sync timeout duration. If no ACK is received in this time, the primary will revert to asynchronous replication. If set to OFF, the primary will revert to asynchronous replication as soon as the replica count drops to zero.
Command line: --rpl-semi-sync-master-wait-no-slave[={0|1}]
rpl_semi_sync_master_wait_pointDescription: Whether the transaction should wait for semi-sync acknowledgement after having synced the binlog (AFTER_SYNC), or after having committed in storage engine (AFTER_COMMIT, the default).
When this variable is set to AFTER_SYNC, the primary performs the following steps:
Prepares the transaction in the storage engine.
rpl_semi_sync_slave_delay_masterDescription: Only write primary info file when ack is needed.
Command line: --rpl-semi-sync-slave-delay-master[={0|1}]
Scope: Global
Dynamic: Yes
rpl_semi_sync_slave_enabledDescription: Set to ON to enable semi-synchronous replication replica. Disabled by default.
Command line: --rpl-semi-sync-slave-enabled[={0|1}]
Scope: Global
Dynamic: Yes
rpl_semi_sync_slave_kill_conn_timeoutDescription: Timeout for the mysql connection used to kill the replica io_thread's connection on primary. This timeout comes into play when stop slave is executed.
Command line: --rpl-semi-sync-slave-kill-conn-timeout[={0|1}]
Scope: Global
Dynamic: Yes
rpl_semi_sync_slave_trace_levelDescription: The tracing level for semi-sync replication. The levels are the same as for .
Command line: --rpl-semi-sync-slave-trace_level[=#]
Scope: Global
Dynamic: Yes
init-rpl-roleFrom , , , , and , changes the condition for semi-sync recovery to truncate the to instead use this option, when set to SLAVE. This avoids a possible error state where the replica’s state is ahead of the primaries. See .
rpl-semi-sync_masterDescription: Controls how the server should treat the plugin when the server starts up.
Valid values are:
OFF - Disables the plugin without removing it from the table.
rpl-semi-sync_slaveDescription: Controls how the server should treat the plugin when the server starts up.
Valid values are:
OFF - Disables the plugin without removing it from the table.
For a list of status variables added when the plugin is installed, see .
This page is licensed: CC BY-SA / Gnu FDL
1.0
N/A
Data Type: boolean
Default Value: OFF
--rpl-semi-sync-master-timeout[=#]Scope: Global
Dynamic: Yes
Data Type: numeric
Default Value: 10000 (10 seconds)
Range: 0 to 18446744073709551615
32: Net wait level, including more information about network waits.64: Function level, including information about function entries and exits.
Command line: --rpl-semi-sync-master-trace-level[=#]
Scope: Global
Dynamic: Yes
Data Type: numeric
Default Value: 32
Range: 0 to 18446744073709551615
Scope: Global
Dynamic: Yes
Data Type: boolean
Default Value: ON
Syncs the transaction to the binary log.
Waits for acknowledgement from the replica.
Commits the transaction to the storage engine.
Returns an acknowledgement to the client.
When this variable is set to AFTER_COMMIT, the primary performs the following steps:
Prepares the transaction in the storage engine.
Syncs the transaction to the binary log.
Commits the transaction to the storage engine.
Waits for acknowledgement from the replica.
Returns an acknowledgement to the client.
In and before, this system variable does not exist. However, in those versions, the primary waits for the acknowledgement from replicas at a point that is equivalent to AFTER_COMMIT.
See Configuring the Primary Wait Point for more information.
Command line: --rpl-semi-sync-master-wait-point=value
Scope: Global
Dynamic: Yes
Data Type: enum
Default Value: AFTER_COMMIT
Valid Values: AFTER_SYNC, AFTER_COMMIT
Data Type: boolean
Default Value: OFF
Data Type: boolean
Default Value: OFF
Data Type: numeric
Default Value: 5
Range: 0 to 4294967295
Data Type: numeric
Default Value: 32
Range: 0 to 18446744073709551615
ON - Enables the plugin. If the plugin cannot be initialized, then the server will still continue starting up, but the plugin will be disabled.FORCE - Enables the plugin. If the plugin cannot be initialized, then the server will fail to start with an error.
FORCE_PLUS_PERMANENT - Enables the plugin. If the plugin cannot be initialized, then the server will fail to start with an error. In addition, the plugin cannot be uninstalled with UNINSTALL SONAME or UNINSTALL PLUGIN while the server is running.
See Plugin Overview: Configuring Plugin Activation at Server Startup for more information.
Command line: --rpl-semi-sync-master=value
Data Type: enumerated
Default Value: ON
Valid Values: OFF, ON, FORCE, FORCE_PLUS_PERMANENT
Removed:
ON - Enables the plugin. If the plugin cannot be initialized, then the server will still continue starting up, but the plugin will be disabled.FORCE - Enables the plugin. If the plugin cannot be initialized, then the server will fail to start with an error.
FORCE_PLUS_PERMANENT - Enables the plugin. If the plugin cannot be initialized, then the server will fail to start with an error. In addition, the plugin cannot be uninstalled with UNINSTALL SONAME or UNINSTALL PLUGIN while the server is running.
See Plugin Overview: Configuring Plugin Activation at Server Startup for more information.
Command line: --rpl-semi-sync-slave=value
Data Type: enumerated
Default Value: ON
Valid Values: OFF, ON, FORCE, FORCE_PLUS_PERMANENT
Removed:
N/A
N/A
(feature is built-in, no longer available as a separate plugin)
1.0
Stable
1.0
Gamma
1.0
Unknown
SET GLOBAL rpl_semi_sync_master_enabled=ON;[mariadb]
...
rpl_semi_sync_master_enabled=ONSET GLOBAL rpl_semi_sync_slave_enabled=ON;[mariadb]
...
rpl_semi_sync_slave_enabled=ONSTOP SLAVE IO_THREAD;
START SLAVE IO_THREAD;[Warning] Timeout waiting for reply of binlog (file: mariadb-1-bin.000002, pos: 538), semi-sync up to file , position 0.
[Note] Semi-sync replication switched OFF.[Note] Semi-sync replication switched ON with replica (server_id: 184137206) at (mariadb-1-bin.000002, 215076)SET GLOBAL rpl_semi_sync_master_timeout=20000;[mariadb]
...
rpl_semi_sync_master_timeout=20000SET GLOBAL rpl_semi_sync_master_wait_point='AFTER_SYNC';[mariadb]
...
rpl_semi_sync_master_wait_point=AFTER_SYNC