"C" in ACID - Consistency


In this short essay, we dive deep and understand the "C" in ACID - Consistency.

In this quick read, we will take a detailed look into Consistency, understand its importance, functioning, and how the database implements it.

What is Consistency?

In the context of databases, Consistency is Correctness, which means that under no circumstance will the data lose its correctness.

Database systems allow us to define rules that the data residing in our database are mandated to adhere to. Few handy rules could be

  • balance of an account should never be negative
  • no orphan mapping: there should not be any mapping of a person whose entry from the database is deleted.
  • no orphan comment: there should not be any comment in the database that does not belong to an existing blog.

These rules can be defined on a database using Constraints, Cascades, and Triggers; for example, Foreign Key constraints, Check constraints, On Delete Cascades, On Update Cascades, etc.

Consistency ACID Database

Role of the database engine in ensuring Consistency

An ACID-compliant database engine has to ensure that the data residing in the database continues to adhere to all the configured rules. Thus, even while executing thousands of concurrent transactions, the database always moves from one consistent state to another.

What happens when the database discovers a violation?

Database Engine rollbacks the changes, which ensures that the database is reverted to a previous consistent state.

What happens when the database does not find any violation?

Database Engine will continue to apply the changes, and once the transaction is marked successful, this state of the database becomes the newer consistent state.

Why is consistency important?

The answer is very relatable. Would you ever want your account to have a negative balance? No. This is thus defined as a rule that the database engine would have to enforce while applying any change to the data.

How does the database ensure Consistency?

Integrity constraints are checked when the changes are being applied to the data.

Cascade operations are performed synchronously along with the transaction. This means that the transaction is not complete until the primary set of queries, along with all the eligible cascades, are applied. Most database engines also provide a way to make them asynchronous, allowing us to keep our transactions leaner.

✨ Next up is "I" in ACID - Isolation. Stay tuned.

References


Arpit Bhayani

Arpit's Newsletter

CS newsletter for the curious engineers

❤️ by 38000+ readers

If you like what you read subscribe you can always subscribe to my newsletter and get the post delivered straight to your inbox. I write essays on various engineering topics and share it through my weekly newsletter.




Other essays that you might like



Be a better engineer

A set of courses designed to make you a better engineer and excel at your career; no-fluff, pure engineering.


Paid Courses

System Design for Beginners

A masterclass that helps early engineers and product managers become great at designing scalable systems.

300+ learners

Details →

System Design Masterclass

A masterclass that helps you become great at designing scalable, fault-tolerant, and highly available systems.

1000+ learners

Details →

Redis Internals

Learn internals of Redis by re-implementing some of the core features in Golang.

98+ learners

Details →

Free Courses

Designing Microservices

A free playlist to help you understand Microservices and their high-level patterns in depth.

823+ learners

Details →

GitHub Outage Dissections

A free playlist to help you learn core engineering from outages that happened at GitHub.

651+ learners

Details →

Hash Table Internals

A free playlist to help you understand the internal workings and construction of Hash Tables.

1027+ learners

Details →

BitTorrent Internals

A free playlist to help you understand the algorithms and strategies that power P2P networks and BitTorrent.

692+ learners

Details →