DSC4
DSC4
● Dirty reads
● Lost updates
● Inconsistent data
Concurrency control ensures that even with many users, transactions execute safely and
correctly, just like if they were run one after another (serially).
⚙️ Common Techniques:
● Locking protocols (e.g., Two-phase locking)
● Timestamp ordering
🧠 Example:
In a banking app:
Example:
Transferring ₹100 from A to B:
2. Add to B
Both steps must succeed together.
Nested Transactions
A nested transaction is a transaction within a transaction.
Key Points:
Example:
Booking a holiday package:
● Lost updates
● Dirty reads
● Inconsistent states
🔐 Types of Locks:
1. Binary Lock (Mutex Lock):
2. Shrinking Phase: Locks are released; no new locks can be acquired.
⚠️ Drawbacks:
● Can cause deadlocks (circular wait).
⚙️ How It Works:
OCC works in three phases:
○ Before committing, the system checks if this transaction conflicts with others.
✅ Advantages:
● No locks → High concurrency
⚠️ Disadvantages:
● Costly rollbacks if conflicts are frequent
🧠 Example:
Two users editing the same profile:
● At commit, the system validates and allows the first, but rejects the second if data
was modified.
⚙️ Basic Idea:
● The system enforces a serial order: older transactions (with smaller timestamps)
appear to execute before newer ones.
● Every read and write is allowed only if it doesn't violate this order.
📘 Key Rules:
For a data item X:
✅ Advantages:
● Ensures serializability
● No locks → no deadlocks
⚠️ Disadvantages:
UNIT 4
● More aborts if conflicts are common
🧠 Example:
● T1 (TS=5) reads X
● Different servers
● Different locations
● Different databases
A distributed transaction ensures that all parts either commit or roll back together —
maintaining global consistency.
🧱 Example:
Transferring money:
● Node crashes
✅ Handled By:
● Two-Phase Commit Protocol (2PC)
● It starts, performs all operations, and then commits or aborts as one unit.
Example:
Booking a flight and hotel together from different servers in a single transaction.
● Sub-transactions can commit independently, but the parent commits only if all
children succeed.
Example:
Online order system:
🔸 Phase 1: Prepare
● Coordinator sends "prepare to commit" to all participants.
🔸 Phase 2: Commit/Abort
● If all reply "Yes", the coordinator sends commit.
Drawback: If the coordinator crashes during phase 2, participants may be stuck waiting →
can cause blocking.
Phases:
⚙️ Challenges:
● Transactions span multiple systems
🔐 Common Techniques:
🔸 1. Distributed Two-Phase Locking (2PL)
● Locks are acquired across all nodes before access
🔸 2. Timestamp Ordering
● Each transaction gets a global timestamp
🧠 Example:
Two users updating the same bank account across servers:
● With concurrency control, only one update succeeds or they are ordered correctly
Distributed Deadlocks
✅ What is a Deadlock?
A deadlock occurs when two or more transactions are waiting indefinitely for each other to
release resources, and none of them can proceed.
In a distributed system, resources and transactions are spread across multiple nodes,
making deadlock more complex and harder to detect.
1. Mutual Exclusion: Only one process can use a resource at a time.
2. Hold and Wait: A process holding one resource is waiting for another.
4. Circular Wait: A closed chain of processes exists where each holds a resource the
next wants.
This cycle is not visible from a single node, so special techniques are needed.
🔸 1. Centralized Detection
● A central coordinator collects the wait-for graphs from all nodes.
🔸 2. Distributed Detection
● Each node monitors local waits.
🔸 3. Timeout-Based Detection
● If a transaction waits too long, it is assumed to be deadlocked.
Pros: Simple
Cons: May abort non-deadlocked transactions (false positives)
● Random selection
🔹 1. Wait-Die Scheme
● Older transactions can wait for younger ones.
● Younger transactions trying to lock a resource held by an older one are aborted
(die).
🔹 2. Wound-Wait Scheme
● Older transactions wound (abort) younger ones holding the lock.
Transaction Recovery
✅ What is Transaction Recovery?
Transaction recovery is the process of restoring a system to a consistent state after a
failure occurs (e.g., system crash, network failure, transaction abort).
● Or abort together
🔁 Recovery Mechanisms
🔸 1. Logging (Write-Ahead Log - WAL)
● Every transaction's changes and status (BEGIN, COMMIT, ABORT) are written to a
log file before being applied to the database.
csharp
CopyEdit
🔸 2. Checkpoints
● A snapshot of the current system state is saved periodically.
● During recovery, the system restores the last checkpoint and applies log records
after that.
🧠 Example:
Suppose a transaction T1 involves 3 servers:
2. Coordinator sends COMMIT to 2 servers, but crashes before sending to the 3rd.
3. On recovery:
○ It may contact other servers to know the outcome and act accordingly.
UNIT 4