0
0
DBMS Theoryknowledge~6 mins

Timestamp-based protocols in DBMS Theory - Full Explanation

Choose your learning style9 modes available
Introduction
When multiple users try to change data at the same time, conflicts can happen that cause errors or lost updates. Timestamp-based protocols help decide the order of these changes to keep data correct and consistent.
Explanation
Timestamps as Time Markers
Each transaction is given a unique timestamp when it starts. This timestamp shows the order in which transactions arrive, like a time label. The system uses these timestamps to decide which transaction should go first when conflicts happen.
Timestamps give a clear order to transactions to avoid confusion.
Read and Write Rules
The protocol checks timestamps before allowing a transaction to read or write data. If a transaction tries to read or write data that conflicts with a newer transaction, it may be delayed or rolled back to keep the order correct.
Read and write actions are controlled by timestamps to prevent conflicts.
Avoiding Conflicts and Deadlocks
Unlike locking methods, timestamp-based protocols do not block transactions waiting for others. Instead, they use timestamps to decide which transaction should proceed, reducing the chance of deadlocks where transactions wait forever.
Timestamp protocols reduce waiting and prevent deadlocks by ordering transactions.
Transaction Rollback and Restart
If a transaction violates the timestamp order, it is rolled back and restarted with a new timestamp. This ensures that only transactions following the correct order make changes to the data.
Transactions that break the order are restarted to maintain consistency.
Real World Analogy

Imagine a busy checkout line where each customer gets a ticket number when they arrive. The cashier serves customers in ticket order, even if someone tries to cut in line. If a customer tries to pay out of turn, they are asked to wait until their number is called.

Timestamps as Time Markers → Ticket numbers given to customers when they arrive
Read and Write Rules → Cashier only serving customers in ticket order, not letting anyone cut in
Avoiding Conflicts and Deadlocks → No customers blocking the line by waiting for others; order is always clear
Transaction Rollback and Restart → Customers asked to wait and come back later if they try to pay out of turn
Diagram
Diagram
┌───────────────┐       ┌───────────────┐       ┌───────────────┐
│ Transaction 1 │──────▶│ Timestamp 1   │──────▶│ Reads/Writes  │
└───────────────┘       └───────────────┘       └───────────────┘
       │                                              │
       ▼                                              ▼
┌───────────────┐       ┌───────────────┐       ┌───────────────┐
│ Transaction 2 │──────▶│ Timestamp 2   │──────▶│ Reads/Writes  │
└───────────────┘       └───────────────┘       └───────────────┘
       │                                              │
       ▼                                              ▼
┌───────────────┐       ┌───────────────┐       ┌───────────────┐
│ Transaction 3 │──────▶│ Timestamp 3   │──────▶│ Reads/Writes  │
└───────────────┘       └───────────────┘       └───────────────┘
This diagram shows transactions receiving timestamps and then performing read/write operations in timestamp order.
Key Facts
TimestampA unique number assigned to each transaction to indicate its order.
Read RuleA transaction can read data only if it does not conflict with a newer transaction's write.
Write RuleA transaction can write data only if it does not conflict with a newer transaction's read or write.
RollbackRestarting a transaction with a new timestamp if it violates timestamp order.
DeadlockA situation where transactions wait forever for each other, avoided by timestamp protocols.
Code Example
DBMS Theory
class Transaction:
    _counter = 0
    def __init__(self):
        Transaction._counter += 1
        self.timestamp = Transaction._counter
        self.active = True

class TimestampProtocol:
    def __init__(self):
        self.data = {}
        self.read_ts = {}
        self.write_ts = {}

    def read(self, transaction, item):
        if item in self.write_ts and self.write_ts[item] > transaction.timestamp:
            transaction.active = False  # Rollback
            return None
        self.read_ts[item] = max(self.read_ts.get(item, 0), transaction.timestamp)
        return self.data.get(item, None)

    def write(self, transaction, item, value):
        if (item in self.read_ts and self.read_ts[item] > transaction.timestamp) or \
           (item in self.write_ts and self.write_ts[item] > transaction.timestamp):
            transaction.active = False  # Rollback
            return False
        self.data[item] = value
        self.write_ts[item] = transaction.timestamp
        return True

# Example usage
protocol = TimestampProtocol()
t1 = Transaction()
t2 = Transaction()

print(protocol.write(t1, 'x', 10))  # True
print(protocol.read(t2, 'x'))       # 10
print(protocol.write(t2, 'x', 20))  # True
print(t1.active)                    # True
print(t2.active)                    # True

# t1 tries to write after t2 with higher timestamp
print(protocol.write(t1, 'x', 30))  # False, rollback
print(t1.active)                    # False
OutputSuccess
Common Confusions
Timestamps are the actual time of day when transactions happen.
Timestamps are the actual time of day when transactions happen. Timestamps are logical numbers assigned in order, not real clock times.
Timestamp protocols lock data like other concurrency controls.
Timestamp protocols lock data like other concurrency controls. Timestamp protocols avoid locking by ordering transactions with timestamps.
Transactions never fail or restart in timestamp protocols.
Transactions never fail or restart in timestamp protocols. Transactions can be rolled back and restarted if they break the timestamp order.
Summary
Timestamp-based protocols assign each transaction a unique order number to manage conflicts.
They control read and write operations based on these timestamps to keep data consistent.
Transactions that violate the order are rolled back and restarted to avoid errors and deadlocks.