When a read lock is added to a piece of data, this piece of data has and only a read lock can be added. Exclusive locks and write locks are not allowed
When a write lock is added to a piece of data, a write lock or a read lock cannot be added to this piece of data.
Lock classification | overview | sample | scenes to be used |
pessimistic lock | Pessimistic locking holds a conservative state for data being modified by the outside world (pessimistic) Therefore, during the entire data processing process, the data is locked Often rely on the locking mechanism provided by the database to achieve. It can be understood as: pessimistic lock, the attitude is pessimistic, always think that the data It will cause conflicts during processing, so the data to be modified will be locked |
Row locks, page locks, table locks, shared locks (read locks) exclusive lock (write lock) |
write more, read less Ensure data security |
optimistic lock | Optimistic locking believes that data will not cause conflicts during processing Therefore, when the data is submitted and updated, it will be formally updated. Whether the data conflicts or not is detected, if a conflict occurs, then Return an error message to the user and let the user decide what to do |
Database optimistic lock, cache optimistic lock | Read more, write less, improve throughput |
The reason for the high throughput of optimistic lock: the table is only locked at the moment when the data is updated
Database optimistic locking implementation scheme:
Advantages: simple and efficient, stable and reliable
Disadvantages: low concurrency
1. Realized by version number
Modify the table structure and add a version column. Every time the inventory is updated, the version number is +1.
update goods_info set amount = amount - #{buys} , version = version + 1
where code = #{code} and version = #{version}
2. Through state control
Judging that the inventory minus the purchase quantity is greater than 0
update goods_info set amout = amout - #{buys}
where code = #{code} and amout-#{buys} >0
Cache optimistic lock implementation scheme:
Using the CAS (Compare and Swap) mechanism
Step analysis: read data ---> compare data----> update data
Scenario Analysis: Spike Scenario
The seckill scene includes: grabbing tickets, commodity seckill, grabbing red envelopes, and online reservations. . .
Features of seckill:
1. Read more and write less: such as grabbing tickets, 6000 people come to grab 200 tickets
2. Short-term high concurrency and heavy load pressure
3. Resource competition is limited, cannot sell more, cannot sell less, cannot resell
According to the characteristics of seckill, optimistic lock can be used to control inventory operation.
service-----Modify inventory method process:
Obtain commodity inventory object --- obtain commodity inventory --- judge whether the inventory is sufficient (whether the inventory is less than the current purchase amount) --- obtain the version number
---Update the inventory with the version number ---The current thread that fails to update sleeps (the sleep time is random) ---Recursively call the method of modifying the inventory
Realize seckill based on cache---realize CAS mechanism