# Unit 4

### **Unit 4: Distributed Transaction Processing - Complete Notes**

***

#### **1. Transactions**

**Definition:**

* A **transaction** is a sequence of operations performed as a single logical unit of work.
* It ensures that a system remains in a consistent state, even in the event of failures.

**ACID Properties:**

1. **Atomicity:**
   * All operations in a transaction are completed successfully, or none are.
   * Example: Money transfer between two accounts (both debit and credit must succeed).
2. **Consistency:**
   * A transaction brings the system from one valid state to another.
   * Example: Ensuring account balances are never negative.
3. **Isolation:**
   * Transactions are executed independently without interference.
   * Example: Two users transferring money from the same account simultaneously.
4. **Durability:**
   * Once a transaction is committed, its effects are permanent.
   * Example: After a successful transaction, the changes are saved even if the system crashes.

***

#### **Mind Map for Transactions:**

```
Transactions  
├── Definition  
│   └── Sequence of operations as a single unit  
├── ACID Properties  
│   ├── Atomicity (Example: Money transfer)  
│   ├── Consistency (Example: Valid account balances)  
│   ├── Isolation (Example: Concurrent transactions)  
│   └── Durability (Example: Permanent changes after commit)
```

***

#### **2. Nested Transactions**

**Definition:**

* A **nested transaction** is a transaction that contains other transactions (subtransactions).
* It allows for modular and hierarchical transaction management.

**How It Works:**

1. **Parent and Child Transactions:**
   * A parent transaction can have multiple child transactions.
   * Example: Booking a flight (parent) and reserving a seat (child).
2. **Commit and Rollback:**
   * If a child transaction fails, only that part is rolled back, not the entire parent transaction.
   * Example: If seat reservation fails, the flight booking can still proceed.

**Advantages:**

1. **Modularity:**
   * Breaks down complex transactions into smaller, manageable parts.
2. **Partial Rollback:**
   * Only the failed subtransaction is rolled back, reducing the impact of failures.

***

#### **Mind Map for Nested Transactions:**

```
Nested Transactions  
├── Definition  
│   └── Transactions within transactions  
├── How It Works  
│   ├── Parent and Child Transactions (Example: Flight booking)  
│   └── Commit and Rollback (Example: Seat reservation)  
└── Advantages  
    ├── Modularity  
    └── Partial Rollback
```

***

#### **3. Locks**

**Definition:**

* **Locks** are mechanisms used to control access to shared resources in a transaction.
* They ensure that only one transaction can access a resource at a time.

**Types of Locks:**

1. **Shared Lock (Read Lock):**
   * Multiple transactions can read a resource simultaneously.
   * Example: Multiple users reading a file.
2. **Exclusive Lock (Write Lock):**
   * Only one transaction can write to a resource at a time.
   * Example: A single user editing a file.

**Challenges:**

1. **Deadlocks:**
   * Two transactions waiting for each other to release locks.
   * Example: Transaction A locks Resource 1 and waits for Resource 2, while Transaction B locks Resource 2 and waits for Resource 1.
2. **Performance Overhead:**
   * Lock management can introduce latency.
   * Example: Delays in accessing heavily locked resources.

***

#### **Mind Map for Locks:**

```
Locks  
├── Definition  
│   └── Control access to shared resources  
├── Types  
│   ├── Shared Lock (Example: Reading a file)  
│   └── Exclusive Lock (Example: Editing a file)  
└── Challenges  
    ├── Deadlocks (Example: Circular waiting)  
    └── Performance Overhead (Example: Latency)
```

***

#### **4. Optimistic Concurrency Control**

**Definition:**

* **Optimistic Concurrency Control (OCC)** assumes that conflicts are rare and allows transactions to proceed without locking resources.
* Conflicts are detected and resolved at the end of the transaction.

**How It Works:**

1. **Read Phase:**
   * Transactions read data without locking.
2. **Validation Phase:**
   * Before committing, the system checks for conflicts.
3. **Write Phase:**
   * If no conflicts are found, the transaction is committed; otherwise, it is rolled back.

**Advantages:**

1. **High Concurrency:**
   * Transactions can proceed without waiting for locks.
   * Example: Collaborative editing tools like Google Docs.
2. **Low Overhead:**
   * No need to manage locks, reducing system overhead.

**Disadvantages:**

1. **Rollback Overhead:**
   * If conflicts are frequent, rollbacks can degrade performance.
   * Example: High-contention systems like stock trading platforms.

***

#### **Mind Map for Optimistic Concurrency Control:**

```
Optimistic Concurrency Control (OCC)  
├── Definition  
│   └── Assumes conflicts are rare  
├── How It Works  
│   ├── Read Phase  
│   ├── Validation Phase  
│   └── Write Phase  
├── Advantages  
│   ├── High Concurrency (Example: Google Docs)  
│   └── Low Overhead  
└── Disadvantages  
    └── Rollback Overhead (Example: Stock trading platforms)
```

***

#### **5. Timestamp Ordering**

**Definition:**

* **Timestamp Ordering** assigns a unique timestamp to each transaction and ensures that transactions are executed in timestamp order.

**How It Works:**

1. **Timestamp Assignment:**
   * Each transaction is assigned a timestamp when it starts.
2. **Conflict Resolution:**
   * If a transaction tries to access data modified by a newer transaction, it is rolled back.
   * Example: Transaction A (timestamp 1) tries to read data updated by Transaction B (timestamp 2).

**Advantages:**

1. **No Deadlocks:**
   * Timestamps ensure a strict order, preventing deadlocks.
2. **Simple Implementation:**
   * Easy to implement compared to locking mechanisms.

**Disadvantages:**

1. **Starvation:**
   * Older transactions may be repeatedly rolled back.
   * Example: A low-priority transaction constantly delayed by high-priority transactions.

***

#### **Mind Map for Timestamp Ordering:**

```
Timestamp Ordering  
├── Definition  
│   └── Transactions executed in timestamp order  
├── How It Works  
│   ├── Timestamp Assignment  
│   └── Conflict Resolution (Example: Rollback)  
├── Advantages  
│   ├── No Deadlocks  
│   └── Simple Implementation  
└── Disadvantages  
    └── Starvation (Example: Low-priority transactions)
```

***

#### **6. Flat vs. Nested Distributed Transactions**

**Flat Transactions:**

* A single-level transaction with no subtransactions.
* **Example:** Transferring money between two accounts.

**Nested Transactions:**

* A transaction containing subtransactions.
* **Example:** Booking a flight (parent) and reserving a seat (child).

**Comparison:**

| **Feature**    | **Flat Transactions**          | **Nested Transactions**                |
| -------------- | ------------------------------ | -------------------------------------- |
| **Structure**  | Single-level                   | Hierarchical (parent-child)            |
| **Complexity** | Simple                         | Complex                                |
| **Rollback**   | Entire transaction rolled back | Only failed subtransaction rolled back |
| **Use Case**   | Simple operations              | Complex, modular operations            |

***

#### **Mind Map for Flat vs. Nested Transactions:**

```
Flat vs. Nested Transactions  
├── Flat Transactions  
│   ├── Single-level  
│   └── Example: Money transfer  
├── Nested Transactions  
│   ├── Hierarchical  
│   └── Example: Flight booking  
└── Comparison  
    ├── Structure, Complexity, Rollback, Use Case
```

***

#### **7. Atomic Commit Protocols**

**Definition:**

* **Atomic Commit Protocols** ensure that a distributed transaction is either fully committed or fully rolled back across all nodes.

**Types:**

1. **Two-Phase Commit (2PC):**
   * A coordinator ensures all participants agree to commit or abort.
   * Example: Distributed databases like MySQL Cluster.
2. **Three-Phase Commit (3PC):**
   * Adds a pre-commit phase to reduce blocking in case of failures.
   * Example: High-availability systems like Google Spanner.

***

#### **Mind Map for Atomic Commit Protocols:**

```
Atomic Commit Protocols  
├── Two-Phase Commit (2PC)  
│   └── Example: MySQL Cluster  
└── Three-Phase Commit (3PC)  
    └── Example: Google Spanner
```

***

#### **8. Concurrency Control in Distributed Transactions**

**Definition:**

* **Concurrency Control** ensures that multiple transactions can execute simultaneously without causing inconsistencies.

**Techniques:**

1. **Lock-Based Protocols:**
   * Use locks to control access to resources.
   * Example: Shared and exclusive locks.
2. **Timestamp Ordering:**
   * Use timestamps to order transactions.
   * Example: Conflict resolution using timestamps.
3. **Optimistic Concurrency Control:**
   * Assume conflicts are rare and resolve them at commit time.
   * Example: Collaborative editing tools.

***

#### **Mind Map for Concurrency Control:**

```
Concurrency Control  
├── Lock-Based Protocols (Example: Shared/Exclusive locks)  
├── Timestamp Ordering (Example: Conflict resolution)  
└── Optimistic Concurrency Control (Example: Collaborative editing)
```

***

#### **9. Distributed Deadlocks**

**Definition:**

* **Distributed Deadlocks** occur when transactions in a distributed system are waiting for resources held by each other.

**Detection and Prevention:**

1. **Detection Algorithms:**
   * Detect deadlocks by analyzing wait-for graphs.
   * Example: Chandy-Misra-Haas algorithm.
2. **Prevention Techniques:**
   * Avoid deadlocks by resource allocation strategies.
   * Example: Timeout-based resource allocation.

***

#### **Mind Map for Distributed Deadlocks:**

```
Distributed Deadlocks  
├── Detection (Example: Chandy-Misra-Haas algorithm)  
└── Prevention (Example: Timeout-based allocation)
```

***

#### **10. Transaction Recovery**

**Definition:**

* **Transaction Recovery** ensures that a system can recover from failures and maintain data consistency.

**Techniques:**

1. **Log-Based Recovery:**
   * Maintain a log of all transactions to replay or undo operations.
   * Example: Database systems like Oracle.
2. **Checkpointing:**
   * Periodically save the system state to reduce recovery time.
   * Example: File systems like NTFS.

***

#### **Mind Map for Transaction Recovery:**

```
Transaction Recovery  
├── Log-Based Recovery (Example: Oracle databases)  
└── Checkpointing (Example: NTFS file system)
```

***

#### **11. Overview of Replication and Distributed Multimedia Systems**

**Replication:**

* **Definition:** Creating multiple copies of data across nodes for fault tolerance and performance.
* **Example:** Replicating databases in a distributed system.

**Distributed Multimedia Systems:**

* **Definition:** Systems that handle multimedia data (e.g., video, audio) across distributed nodes.
* **Example:** Streaming platforms like Netflix or YouTube.

***

#### **Mind Map for Replication and Multimedia Systems:**

```
Replication and Multimedia Systems  
├── Replication  
│   └── Example: Distributed databases  
└── Distributed Multimedia Systems  
    └── Example: Netflix, YouTube
```

***

#### **Summary of Unit 4: Distributed Transaction Processing**

* **Transactions** ensure ACID properties (Atomicity, Consistency, Isolation, Durability).
* **Nested Transactions** allow modular and hierarchical transaction management.
* **Locks** and **Optimistic Concurrency Control** manage access to shared resources.
* **Timestamp Ordering** ensures transactions are executed in a strict order.
* **Atomic Commit Protocols** (2PC, 3PC) ensure distributed transactions are committed or rolled back atomically.
* **Concurrency Control** and **Deadlock Handling** maintain system consistency.
* **Transaction Recovery** ensures system resilience to failures.
* **Replication** and **Distributed Multimedia Systems** handle data redundancy and multimedia processing.

***

#### **Final Mind Map for Unit 4:**

```
Unit 4: Distributed Transaction Processing  
├── Transactions  
│   ├── ACID Properties (Atomicity, Consistency, Isolation, Durability)  
├── Nested Transactions  
│   ├── Parent and Child Transactions  
│   └── Partial Rollback  
├── Locks  
│   ├── Shared Lock, Exclusive Lock  
│   └── Deadlocks, Performance Overhead  
├── Optimistic Concurrency Control  
│   ├── Read, Validation, Write Phases  
│   └── High Concurrency, Rollback Overhead  
├── Timestamp Ordering  
│   ├── Timestamp Assignment, Conflict Resolution  
│   └── No Deadlocks, Starvation  
├── Flat vs. Nested Transactions  
│   ├── Structure, Complexity, Rollback, Use Case  
├── Atomic Commit Protocols  
│   ├── Two-Phase Commit (2PC), Three-Phase Commit (3PC)  
├── Concurrency Control  
│   ├── Lock-Based, Timestamp Ordering, Optimistic Control  
├── Distributed Deadlocks  
│   ├── Detection (Chandy-Misra-Haas), Prevention (Timeout-based)  
├── Transaction Recovery  
│   ├── Log-Based Recovery, Checkpointing  
└── Replication and Multimedia Systems  
    ├── Replication (Distributed Databases)  
    └── Distributed Multimedia Systems (Netflix, YouTube)
```
