Acid Databases
Acid Databases
INTRODUCTION
ACID stands for Atomicity, Consistency, Isolation and
Durability. These are four key properties that most
database management systems (DBMS) offer as
guarantees when handling transactions.
Most popular DBMS like MySQL, PostgresSQL and
Oracle have ACID guarantees out of the box. Others
have partial ACID guarantees like Redis, DynamoDB,
and Cassandra. The trend, however, seems to be that
more and more DBMS are offering ACID compliance.
What are Transactions?
Transactions serve a single purpose: they make sure a
system is fault tolerant. If a failure in a system occurs, can
the system continue to operate without complete
catastrophe? Phrased differently, can the system tolerate
faults? An answer of ‘yes’ to this question means that
such a system is fault tolerant.
A transaction is an abstraction. It is a collection of
operations (reads and writes) that are treated as a single
logical operation.
A transaction ensures that all operations related to the
purchase are treated as a single operation. If any part of
the transaction fails, the entire transaction is rolled back,
leaving the database in a state as if the customer had
never attempted the purchase, thus maintaining the
integrity of the data.
The transaction is committed when all the operations
within the transaction are successfully completed and
their results are permanently recorded. This permanence
is typically achieved by writing the changes to the
database's storage
What Does Atomicity Mean?
Atomicity simply means that all queries in a transaction must
succeed for the transaction to succeed. If one query fails, the
entire transaction fails.
For example:-
We want to transfer $100 from account 1 to account 2. This
involves two operations:
1. Deduct $100 from account 1.
2. Add $100 to account 2.
Both operations must succeed for the transaction to be
considered successful. If either operation fails, the
transaction should be rolled back to maintain atomicity.
What Does Consistency Mean?
Consistency can mean different things in
cloud/software engineering, depending on the context.
In the case of ACID, the “C” was most likely added to
make the acronym work.
Consistency in the context of ACID means consistency
in data, which is defined by the creator of the
database.
The technical term for consistency in data is called
referential integrity. Referential integrity is a method of
ensuring that relationships between tables remain
consistent. It's usually enforced through the use of
foreign keys.
What Does Isolation Mean?
Isolation is a guarantee that concurrently running
transactions should not interfere with each other.
Concurrency here refers to two or more transactions trying
to modify or read the same database record(s) at the same
time.
Read Commited:-
No Dirty Reads: Reading data from another transaction that
has not yet been committed is called a dirty read. With the
read committed isolation level, you will only see data that
has been committed by another transaction.
No Dirty Writes: Overwriting data that has already been
written by another transaction but not yet committed is
called a dirty write.
Phantom read:-A phantom read is a specific type of
concurrency problem that can occur in database systems when
transactions run in an isolation level lower than SERIALIZABLE.
It happens when a transaction reads a set of rows that satisfy a
certain condition, but another transaction concurrently inserts,
updates, or deletes rows that would affect the result set of the
first transaction if the same query were run again.
Repeatable Read:-The repeatable read is a more strict isolation
level, in that it has the same guarantees as read committed
isolation – plus it guarantees that reads are repeatable.
A repeatable read guarantees that if a transaction reads a row
of data, any subsequent reads of that same row of data within
the same transaction will yield the same result, regardless of
changes made by other transactions. This consistency is
maintained throughout the duration of the transaction.
When a transaction reads the same data twice, but sees a
different value in each read because a committed transaction
has updated the value between the two reads, this is called a
fuzzy read. The repeatable read isolation level prevents fuzzy
reads.
What Does Durability Mean?
Durability is a guarantee that changes made by a
committed transaction must not be lost. All committed
transactions must be persisted on durable, non-volatile
storage, that is on disk. This ensures that any committed
transactions are protected even if the database crashes.
Naturally, durability cannot protect against destruction of
the disk which stores the data. Additional redundancy can
be added by having backups of your database stored
separately from the original.
Thanks for
reading