Consistency of ACID Properties | System Design | SDE Interview
Consistency is one of the four properties of the ACID (Atomicity, Consistency, Isolation, Durability) model, which is used to ensure the reliability and consistency of database transactions in a system design.
The consistency property ensures that a transaction brings the database from one valid state to another. This means the database must meet certain integrity constraints, such as unique key constraints, referential integrity constraints, and check constraints. For example, a transaction that transfers money between two bank accounts must ensure that the sum of the balances of the two accounts remains the same before and after the transaction.
To achieve consistency, database systems use a technique called “transaction isolation”. This ensures that the changes made by one transaction are not visible to other transactions until the first transaction is committed. This is important to prevent “dirty reads”, where a transaction reads data that has been modified by another transaction that has not yet been committed.
There are several levels of isolation levels which can be used to achieve consistency:
- Read Uncommitted: At this level, a transaction can read data that has been modified by another transaction that has not yet been committed. This can lead to dirty reads and can cause inconsistent results.
- Read Committed: At this level, a transaction can only read data that another transaction has committed. This eliminates the possibility of dirty reads, but it does not guarantee that the data read by the transaction will remain unchanged until the end of the transaction.
- Repeatable Read: At this level, a transaction can only read data that another transaction has committed, and it also ensures that the data read by the transaction will remain unchanged until the end of the transaction. This eliminates the possibility of dirty reads and non-repeatable reads.
- Serializable: This level provides the highest degree of isolation, ensuring that transactions are executed as if they were executed serially, one after the other. This eliminates the possibility of dirty, non-repeatable, and phantom reads.
Real-World Case of Consistency
To illustrate the consistency property, consider a banking system that allows customers to transfer money between accounts. In this system, a transaction might involve debiting one account and crediting another. To ensure consistency, the transaction must first check that the account has sufficient funds to make the transfer.
If the account does not have sufficient funds, the transaction is rolled back, and the database remains consistent. The transaction is committed if the account has sufficient funds, and the database is updated to reflect the new balances.
In addition to transaction isolation, database systems also use a “concurrency control” technique to ensure consistency. This involves controlling access to the database by multiple transactions, to prevent conflicts and ensure that the database remains consistent. There are two main concurrency control methods:
- Optimistic concurrency control: In this method, transactions are allowed to proceed without any locks or restrictions, and conflicts are detected and resolved only when a transaction attempts to commit.
- Pessimistic concurrency control: In this method, transactions are required to acquire locks on the data they access, to prevent conflicts. This can lead to lower performance, as transactions may have to wait for locks to be released.
In summary, consistency is an essential aspect of the ACID model and ensures the reliability and consistency of database transactions in a system design. It ensures that the database meets certain integrity constraints and that the changes made by one transaction are not visible to other transactions until the first transaction is committed.
Database systems use techniques like transaction isolation and concurrency control to achieve consistency. It is essential to choose the right isolation level for a system, as it can significantly impact the system's performance and reliability. The isolation level chosen should balance the need for consistency with the need for performance and scalability.
In addition, consistency can also be achieved through the use of database constraints and triggers. Constraints enforce rules on the data in the database, such as unique key and referential integrity constraints. Triggers are used to automatically enforce rules or perform actions when certain events occur, such as when data is inserted, updated, or deleted.
It’s also worth noting that, consistency can also be achieved by implementing a two-phase commit protocol. This protocol ensures that all the participants in a distributed transaction agree to commit roll back the changes.
Conclusion
In conclusion, consistency is a crucial aspect of the ACID model and is essential for maintaining the integrity and reliability of a system’s data. It ensures that the data is consistent throughout a transaction and that any changes made to the data are valid and conform to integrity constraints.
Consistency can be achieved through the use of transaction isolation, concurrency control, database constraints, triggers, and a two-phase commit protocol. Choosing the right isolation level, and concurrency control method, and implementing database constraints and triggers can all help to ensure consistency in a system design.
That’s all 👍🏼.
Thanks 🤗.
Want to Hire/Connect? LinkedIn
P.S.: If you like this uninterrupted reading experience on this beautiful platform of Medium.com, consider supporting the writers of this community by signing up for a membership HERE. It only costs $5 per month and helps all the writers.
A clap would be highly appreciated if you liked what you just read. You can be generous in clapping; it shows me how much you enjoyed this story. And if you didn’t like it? Please do comment😋!