Listen to this Post
Scaling up a cluster of database servers is easier with read replicas. However, the more replicas you use, the more you must deal with replication lag. Here are the 3 approaches every backend engineer should know:
1. Tight Consistency
All the replicas are up to date with the primary server before any other operation is allowed. This approach is rarely used in practice since it cancels out the performance benefits of using replicas. A less rigid approach is to execute specific read requests requiring strict consistency only on the primary server. Instead, all other read requests that allow more freedom are sent to the replicas.
2. Causal Consistency
Each replica is guaranteed to be updated to at least a specific point in time. Read requests dependent on a write can be directed to a replica that has seen that write. A way to implement this is by using global transaction identifiers (GTID). Each write transaction in the primary database has a GTID associated with it. The GTIDs associated with those changes are also sent to the replicas. Read requests specifying a certain GTID are only routed to replicas having at least seen changes up to that GTID.
3. Monotonic Read Consistency
This approach allows multiple sequential reads to follow a consistent timeline. Each subsequent read request goes to a replica that’s at least as up-to-date as the one that served the last read. A way to implement this is using UUIDs. The approach assumes a proxy layer exists between the application and the replicas. Each read request is sent to this proxy layer with a UUID. Read requests with the same UUID are routed to the identical read replica.
You Should Know: Practical Implementation with Commands and Codes
Tight Consistency Implementation
To implement tight consistency, you can use the following SQL command to ensure that all replicas are synchronized before proceeding with a read operation:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; BEGIN TRANSACTION; -- Your read query here COMMIT;
Causal Consistency with GTID
To implement causal consistency using GTID, you can use the following MySQL commands:
-- Enable GTID SET @@GLOBAL.ENFORCE_GTID_CONSISTENCY = ON; SET @@GLOBAL.GTID_MODE = ON; -- Check GTID status SHOW VARIABLES LIKE 'gtid_mode';
Monotonic Read Consistency with UUID
To implement monotonic read consistency, you can use a proxy layer like ProxySQL. Here’s an example of how to configure ProxySQL to route requests based on UUID:
-- Add a query rule in ProxySQL INSERT INTO mysql_query_rules (rule_id, active, match_pattern, destination_hostgroup, apply) VALUES (1, 1, '^SELECT.<em>UUID=.</em>', 1, 1); -- Load the rules to runtime LOAD MYSQL QUERY RULES TO RUNTIME;
Linux Commands for Database Management
Here are some Linux commands to manage your database clusters:
1. Check Replication Status:
mysql -u root -p -e "SHOW SLAVE STATUS\G"
2. Restart MySQL Service:
sudo systemctl restart mysql
3. Monitor Database Performance:
sudo apt-get install sysstat sar -u 1 5
Windows Commands for Database Management
For Windows users, here are some useful commands:
1. Check MySQL Service Status:
sc query mysql
2. Start MySQL Service:
net start mysql
3. Monitor Network Traffic:
netstat -an | find "3306"
What Undercode Say
Scaling database clusters with read replicas is a critical skill for backend engineers. By understanding and implementing tight consistency, causal consistency, and monotonic read consistency, you can effectively manage replication lag and improve database performance. Practical implementation using GTID, UUID, and proxy layers like ProxySQL can significantly enhance your system’s reliability and scalability. Always monitor your database performance using Linux or Windows commands to ensure optimal operation.
Expected Output:
- Tight Consistency: Ensures all replicas are synchronized before reads.
- Causal Consistency: Uses GTID to route reads to updated replicas.
- Monotonic Read Consistency: Uses UUIDs to maintain a consistent timeline for sequential reads.
By following these strategies and commands, you can effectively scale your database clusters while minimizing replication lag.
References:
Reported By: Fernando Franco – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅



