Optimistic locking and pessimistic locking of database

optimistic locking

Optimistic locking is an idea, which is to add a field to the database table (the database will add a version number field to the table by default), this field will be read before the data is updated, and the cached data will be updated to the table, and then check this field Whether the field has been manipulated by other programs during this period,

If this field has been modified, the update is rejected and the operation fails.

A pessimistic lock is a lock that prevents its operations from being modified during read and write,

Optimistic locks and pessimistic locks are mainly applicable to business scenarios, rather than naively believing that one is better than the other. Optimistic locks are mainly used in scenarios with fewer write operations and more reads, because if there are too many write operations, it will increase the collision with read operations. Probability, resulting in frequent write failures, increasing the number of retries, affecting performance, while pessimistic locks are updated after waiting for the previous result to complete

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326120028&siteId=291194637