> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Transaction Integrity

> Explains transaction integrity and ACID properties in databases using a banking transfer example, showing how transactions ensure atomicity, consistency, isolation, and durability.

Up until now we've covered structured data and how to model it, discussed relational databases (systems built to store structured data), and the language used to query and update that data: SQL.

Now we'll focus on transaction integrity (also called transactional integrity or transaction management). This concept is essential when multiple related updates must be treated as a single logical operation. We'll use a classic banking example to illustrate.

Imagine you have two bank accounts: a savings account and a checking account. The bank reports a total of $70: $20 in savings and \$50 in checking. In a relational database, those amounts would typically be stored in two separate rows.

If you transfer \$10 from savings to checking, that logical transfer requires two separate updates at the database level:

* Subtract \$10 from the savings account row.
* Add \$10 to the checking account row.

If both updates succeed, the final balances are $10 in savings and $60 in checking, and the total still equals \$70. But because the transfer maps to two update statements, things can go wrong if only one of the updates runs or if another process observes the intermediate state.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/7fDr5YdUM5MRud_L/images/DP-900-Microsoft-Azure-Data-Fundamentals/Structured-Data/Transaction-Integrity/moving-money-step1-70-before-after.jpg?fit=max&auto=format&n=7fDr5YdUM5MRud_L&q=85&s=649fc55c52123e8ff07b71167f57b90d" alt="A slide titled &#x22;Moving Money: Step 1&#x22; showing a total amount of 70. It has a before-and-after bar chart where 20 (green) and 50 (purple) on the left become 10 (green) and $60 (purple) on the right, with an arrow between them." width="1920" height="1080" data-path="images/DP-900-Microsoft-Azure-Data-Fundamentals/Structured-Data/Transaction-Integrity/moving-money-step1-70-before-after.jpg" />
</Frame>

Common failure scenarios:

* The second update never happens (we remove \$10 from savings but never add it to checking).
* A concurrent process inspects the accounts between the two updates and sees an inconsistent intermediate state (for example: $10 in savings and $50 in checking), which does not add up to the \$70 the bank owes you.

To avoid these issues, databases use four transaction properties—commonly known as ACID—which guarantee correct behavior for multi-step updates.

* Atomicity: All updates in the transaction are treated as a single unit. Either every update succeeds, or none are applied.
* Consistency: The database moves from one valid state to another valid state. Constraints and business rules are preserved.
* Isolation: Concurrent transactions do not see each other's intermediate states. Observers see either the state before the transaction or the state after it completes.
* Durability: Once a transaction commits, its effects survive system failures.

ACID at a glance:

| Property | What it ensures | Example effect |
| - | - | - |
| Atomicity | Transaction is all-or-nothing | If adding \$10 fails after subtraction, both updates are rolled back. |
| Consistency | Data stays valid before/after commit | Balance constraints (e.g., non-negative) are preserved. |
| Isolation | No partial states visible to others | Other sessions cannot read the intermediate $10/$50 state. |
| Durability | Committed changes survive crashes | After commit, the new balances persist even if the server restarts. |

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/7fDr5YdUM5MRud_L/images/DP-900-Microsoft-Azure-Data-Fundamentals/Structured-Data/Transaction-Integrity/acid-properties-atomic-consistent-isolated-durable.jpg?fit=max&auto=format&n=7fDr5YdUM5MRud_L&q=85&s=e48a4400aaaa02523cd3d7e5d804ec5e" alt="A presentation slide titled &#x22;Transaction Criteria: ACID&#x22; showing the four properties—Atomic, Consistent, Isolated, Durable—in colored boxes with brief explanations. It notes atomicity as all updates seen as one step, consistency as data remaining correct, isolation hiding intermediary steps, and durability surviving failures." width="1920" height="1080" data-path="images/DP-900-Microsoft-Azure-Data-Fundamentals/Structured-Data/Transaction-Integrity/acid-properties-atomic-consistent-isolated-durable.jpg" />
</Frame>

In SQL you express a multi-step operation as an explicit transaction so you can commit or roll back as a unit. A simple transfer looks like this:

```sql theme={null}
BEGIN TRANSACTION;

UPDATE accounts
SET balance = balance - 10
WHERE account_type = 'savings' AND account_id = 123;

UPDATE accounts
SET balance = balance + 10
WHERE account_type = 'checking' AND account_id = 456;

-- If both updates succeed:
COMMIT;

-- If an error occurs before commit:
-- ROLLBACK;
```

With ACID guarantees in place:

* If both updates succeed, the database commits the changes (final state: $10 savings, $60 checking).
* If any step fails, the database rolls everything back to the original consistent state (original: $20 savings, $50 checking).

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/7fDr5YdUM5MRud_L/images/DP-900-Microsoft-Azure-Data-Fundamentals/Structured-Data/Transaction-Integrity/transaction-management-results-updates-succeed-fail.jpg?fit=max&auto=format&n=7fDr5YdUM5MRud_L&q=85&s=6198ade5c5eb957d8f97b2b7e16ee4fe" alt="A presentation slide titled &#x22;Result of Transaction Management&#x22; showing an &#x22;All updates succeed&#x22; section with listed balances (70 in the bank, 10 in savings, 60 in checking) and a bar chart. Below it is an &#x22;All updates fail&#x22; section with different balances (70 bank, 20 savings, 50 checking)." width="1920" height="1080" data-path="images/DP-900-Microsoft-Azure-Data-Fundamentals/Structured-Data/Transaction-Integrity/transaction-management-results-updates-succeed-fail.jpg" />
</Frame>

<Callout icon="lightbulb" color="#1CB2FE">
  Different database systems implement ACID with varying default isolation levels and concurrency mechanisms. Be aware of your DBMS’s settings (for example: `READ COMMITTED`, `REPEATABLE READ`, `SERIALIZABLE`) because they affect visibility, performance, and concurrency.
</Callout>

Practical tips:

* Keep transactions short to reduce locking and contention.
* Use appropriate isolation levels for your workload: higher isolation reduces anomalies but can decrease concurrency.
* Validate constraints inside the transaction (for example, check for sufficient funds) to maintain consistency.
* Test failure paths (e.g., simulate errors or crashes) to ensure rollbacks work as expected.

Summary

* Transaction integrity ensures multi-step operations behave as single, indivisible actions.
* Use transaction control statements (`BEGIN`, `COMMIT`, `ROLLBACK`) and understand ACID to preserve correctness under errors and concurrency.
* When designing systems, balance isolation needs against performance and concurrency requirements.

Links and References

* [Kubernetes Documentation](https://kubernetes.io/docs/)
* [Docker Hub](https://hub.docker.com/)
* [Transaction Isolation Levels — PostgreSQL](https://www.postgresql.org/docs/current/transaction-iso.html)
* [ACID Properties — Wikipedia](https://en.wikipedia.org/wiki/ACID)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/dp-900-microsoft-azure-data-fundamentals/module/ab06c95a-37f6-40d4-9dd8-b5a6961866b5/lesson/5e88823d-ac4e-4664-b09a-45af7f13f183" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.