Database Locking

Optimistic locking

graph TD
    A[Lock Acquisition] --> B[Check Data Modification]
    B --> C{Modified?}
    C -->|No| D[Acquire Lock]
    D --> E[Single Thread Modification]
    E --> F[Prevent Concurrent Updates]
    F --> G[Maintain Data Consistency]
    C -->|Yes| H[Lock Failed]

Pessimistic locking

graph TD
    A[Request Lock] --> B[Wait for Lock]
    B --> C{Lock Available?}
    C -->|Yes| D[Acquire Lock]
    D --> E[Modify Data]
    E --> F[Release Lock]
    F --> G[Maintain Data Consistency]
    C -->|No| H[Keep Waiting]
    H --> B

Key differences

Table to compare the differences between optimistic and pessimistic locking.

Optimistic locking Pessimistic locking
Lock is acquired before updating the data Lock is acquired after updating the data
Lock is released after updating the data Lock is released before updating the data
Lock is released after updating the data Lock is released before updating the data

Other Terminologies

Everything Becomes Worst in Distributed Systems

In Saga Pattern, the saga is a sequence of steps that are executed in a specific order. The saga pattern is used to coordinate the execution of multiple transactions or activities in a distributed system.

Saga Pattern

In Saga Pattern, the saga is a sequence of steps that are executed in a specific order. The saga pattern is used to coordinate the execution of multiple transactions or activities in a distributed system.

References