How do transactional databases work?
Transactional databases rely on the Atomicity, Consistency, Isolation, and Durability (ACID) properties, to ensure the reliability and accuracy of the transactional data.
ACID properties
Atomicity: Atomicity simply means that a transaction is treated as a single unit (or unit of work) — either the entire transaction is included or it is not stored in the database and an error occurs if only a partial transaction is available. This helps make sure that transactional data consistency is maintained in case a transaction fails. Some of the ways that atomicity is implemented in a transactional database include:
- Undo log: In the event a transaction fails, the undo log enables the changes made by the transaction to be reversed. In this way, only database transactions that meet predefined DBMS rules are included in the transactional database.
- Redo log: When a successful transaction is being written to the transactional database, the redo log keeps track of the changes. This way, if a system failure occurs after a transaction is being ingested but before transaction changes are written, the redo log can be used to reattempt the changes.
- Two-phase commit: Two-phase commit ensures that when a database is stored across multiple machines (e.g., a distributed database), all the nodes that distributed system either accept (e.g., commit) or abort the transaction together. This not only ensures transactions are executed atomically, but also maintains data in a consistent state for a distributed transaction.
- Locking: Locking ensures that only one transaction can edit a specific piece of data at a time. This helps avoid data conflicts across database transactions while also helping maintain data integrity.
Consistency: Transactional databases maintain data consistency by enforcing predefined rules and data constraints which not only apply to ingested data but also data modifications, as well. If an ingested or modified transaction violates these rules, the transaction will not be ingested or the modification will not be saved. In addition, in the case of two users (or more) writing transactions to the database, each user cannot see the changes the other user is making until they are committed and locked.
Isolation: Each transaction is isolated from the other transactions to prevent data conflicts and maintain a consistent state of data integrity. This also helps database operations in relation to managing multiple entries and multi-level transactions. For example, if two users are trying to modify the same data (or even transaction), the DBMS lock manager will suspend other users until the changes currently being made are complete.
Durability: Durability ensures that stored transactions are preserved — even in the event of a system failure. There are a variety of ways that transactional DBMS work to ensure data durability, including:
- Data replication: Data replication software maintains multiple copies of the same data across different nodes or servers. In this way, even if there is a power failure or other type of technical issue, existing data in the database is preserved.
- Write-ahead logging: When changes are being made to a transactional database, the transaction changes are first loaded into an append-only file called a commit log. Once validated, the changes are then written to the database itself.
- Checkpointing: This technique periodically writes the database's current state to disk, ensuring that saved transaction changes are permanently stored. This reduces the amount of effort required should the database ever need to be recovered after a power failure or system failure. Checkpointing also aids in minimizing the amount of work required for database recovery after a cybersecurity attack.
- Redundant Array of Inexpensive Disks (RAID): RAID technology is used to integrate several drives into a single logical unit. This way, in case of a drive failure, there is storage redundancy and the transactional data is retained.
ACID property database example
Consider an online bookstore that tracks current inventory as well as customer orders. This information might be stored in two collections (or tables) — an inventory collection and an orders collection. As such, this example includes a multi-document ACID transaction.
Library example of ACID properties
Atomicity: When an order is placed, the inventory collection must be updated to accurately reflect the number of copies remaining in stock. If an error occurs while updating the inventory or recording the order, the entire transaction will be aborted. This is because updating the inventory without recording the order, or vice versa, would leave the transactional data in an inconsistent state.
Consistency: A book's inventory cannot drop below zero as this would indicate the book had been sold when it was out of stock. For this reason, a database constraint is applied to prevent a book's inventory from going below zero. This means that any order causing a book's inventory to drop below zero would be aborted.
Isolation: Consider a case where two users place the same order at the same time. The final result of running the transactions simultaneously should be the same as if they were run sequentially providing all inventory is available (e.g., if only one book is available, one order would go through and one would be aborted).
Durability: What if a system failure occurs right after the database has confirmed a transaction has been committed? Because the database still has the updated inventory and order details from the committed transaction in the redo log (despite the unexpected server crash), the transactional data would be preserved. The illustration below depicts the flow of events as two users attempt to purchase copies of a book when an outage occurs.
