Does the row locking on slave database also apply to master database?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In the realm of database management systems, understanding the intricacies of data replication mechanisms like master-slave architecture is crucial. One of the common questions that arise is whether row locking on a slave database also applies to the master database. This article delves into the technical aspects of row locking in master-slave databases, providing examples and exploring relevant subtopics.
Master-Slave Database Architecture
Master-slave architecture is a common replication model where the master database handles all the write (INSERT, UPDATE, DELETE) operations and the slave database(s) handle read operations. This setup helps in load balancing, reducing the load on the master, and maintaining data redundancy for reliability purposes.
Row Locking Explained
Row locking is a concurrency control mechanism that prevents multiple transactions from simultaneously modifying the same row in a database:
- In the master database: Row locking is crucial during write operations to ensure data consistency. When a transaction modifies a row, it locks the row to prevent other transactions from making conflicting changes.
- In the slave database: Since it primarily handles read operations, row locking may not be as critical because reads do not alter data. However, row-level locking may still occur in some operations such as during transaction replication or when read consistency (isolation) is enforced.
Does Row Locking on a Slave Apply to the Master?
The direct answer is no; row locking on a slave database does not apply to the master database. However, the underlying operations and replication processes can create scenarios where both databases experience locks for different reasons.
Key Points of Consideration
| Aspect | Master Database | Slave Database |
| Write Operations | Handles all writes; row locks are essential to maintain data consistency. | Does not handle writes; row locks typically arise during replication or specific isolation needs. |
| Read Operations | Supports both reads and writes, generally locking is managed through the transaction isolation level. | Only handles reads, potentially using locks to ensure read consistency under high isolation levels. |
| Replication Delays | Rarely affects master directly, but write operations can block replication. | May experience locks during replication to ensure data consistency with the master. |
| Lock Impact | Directly impacts database writes and transaction performance. | Mostly impacts read performance and can cause replication lag. |
Technical Example
Let's examine a simple scenario with row locking using SQL commands.
Master Database:
Slave Database:
In this example, while the lock on the slave does not impact the master, the complexity arises if replication needs to maintain transactional integrity with read-isolation needs on the slave.
Row Locking and Replication Delays
Replication delay, often called "lag," can impact how locks are perceived on the slave. If a transaction is not promptly replicated, the slave might display stale data, necessitating focused read consistency strategies that may involve additional locking akin to those on the master.
Challenges and Considerations
- Performance: Heavy locking, especially on the slave during replication catch-up, can impede read performance.
- Data Consistency: Strategies like eventual consistency may involve additional complexities in locking, demanding rigorous transaction management and conflict resolution.
- Isolation Levels: Adjusting isolation levels can mitigate unintended locks but at a potential cost to data consistency and transaction performance.
Conclusion
In conclusion, while row locking on a slave does not apply to the master directly, interactions between their operations can influence how locks manifest and impact performance. Understanding these nuances is essential for database administrators aiming to optimize a master-slave architecture for both performance and data integrity across the system. Through strategic management of locks, isolation levels, and replication, organizations can maintain efficient, secure, and reliable database environments.
Related reading
- Does this cause a real problem when I adopt the Raft's never commits log entries from previous terms by counting replicas rule in this situation?
- Doesn't Paxos end up with the same instructions in the exact same order?
- Domain Events and Commands in Distributed System (DDD)
- DTO and DAO concepts and MVC
- Does the SQL Server JDBC driver support asynchronous operations?
- Does YugaByte DB support online schema changes, especially the ability to add new columns to an existing table
- Duplicate delete query in binary log of MySql Master
- Dynamic Topic Name / Quarkus SmallRye Reactive Messaging Kafka

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.