Posts

Showing posts with the label advisory lock

Advisory Lock

Image
Introduction Unlike Level Locks created by the database at the physical layer, developers can freely create Advisory Locks based on custom logic at the application level The database does not know the meaning of this lock and it only acts as an intermediary to hold the lock (usually a bigint number) The process that arrives first is granted the lock for processing Meanwhile, subsequent processes must line up and wait to acquire the lock When a process finishes processing, it returns the lock to the next process Advantages When using other types of locks (such as Table level lock, Row level lock or Page level lock), you must rely on an actual existing data row in the table to lock, whereas Advisory Lock does not require any available data row, you can use any random number to lock It is extremely lightweight because it only exists on the Database RAM, does not write to the hard drive and does not generate redundant data (dead tuples) Functions To create an advisory lock, Postgres provid...

Lock in Database

Image
Introduction In the database world, a lock is a mechanism that helps the system manage simultaneous access and modification of data by multiple users or processes at a single point in time. This is the exact solution to ensure privacy and safety, because allowing unrestricted, uncontrolled access and modification of data will cause everything to become chaotic. Locks are not created to slow down the system, but rather a mandatory blocking mechanism to keep data accurate and consistent when thousands of users access it simultaneously. Why Do We Need Locks Without locks, the system will fall into a data conflict state leading to data corruption, which can cause three classic problems: Lost Update: Two users modify the same row of data at the same time and the user who saves later will overwrite and completely lose the data of the previous user. Dirty Read: You read data that is being modified by someone else, but they have not committed yet. If they perform a rollback afterwards, the dat...