Writing in the front
As can be seen from the jingdong event on 618 in 2011, the high concurrency still puts great pressure on the server. Jingdong finally solved the problem by extending the event on 618 last year. However, the suning planning this time does not seem to learn from the experience of the accident, and the same problem occurred. The same thing happened on Taobao, and 12306 was also scolded for buying tickets. Therefore, what should be done in this special case should be fully considered in the planning scheme.
Analyze the SEC kill service
Which scenarios belong to the SEC kill service?
- Goods to snap up
- Group of red envelopes
- Coupon collection
- Get train ticket
- Online booking
- …
The story of the starting line of the tadpole race
All that stuff about locks
Pessimistic lock resolution
There are two definitions of pessimistic lock, but in essence pessimistic lock is mainly used for concurrency control of database access.
- Explain a
Pessimistic locking refers to data by outsiders (including current other transactions, this system and transaction processing) from an external system changes, and conservative attitude, therefore, in the process of the whole data processing, the data is locked, in the case of pessimistic locks, in order to ensure the transaction isolation, you need to read consistency lock. Data is locked when read and cannot be modified by other transactions. When modifying or deleting data, it is also locked and cannot be read by other transactions.
- Explain two
In relational database management systems, Pessimistic Concurrency Control (also known as “Pessimistic lock”, or “PCC”) is an approach to Concurrency Control. It prevents a transaction from modifying data in a way that affects other users. If a transaction performs operations that apply a lock to a row, other transactions can perform operations that conflict with the lock only if the transaction releases the lock.
Pessimistic lock processing process
- Before modifying any record, try adding exclusive locking to the record.
- If the lock fails, the record is being modified, and the current query may wait or throw an exception
- If the lock is successfully locked, changes can be made to the record and the transaction will be unlocked after completion
- In the meantime, if there are other operations to modify the record or add an exclusive lock, they will wait for us to unlock or directly throw an exception
Pessimistic lock advantages
- Pessimistic concurrency control is actually a conservative strategy of “lock first, access later”, which guarantees the security of data processing.
- Pessimistic lock based on DB level, no intrusion to business code, easy to use
Disadvantages of pessimistic locking
- Pessimistic locking is suitable for reliable persistent connections, such as C/S applications. For HTTP connections in Web applications, congenital does not apply
- The use of locks means a loss of performance, especially in the case of high concurrency and long lock duration. The performance bottleneck of Web applications is mostly in the database, and pessimistic locks are used to further tighten the bottleneck
- Unlocking mechanisms in abnormal abort situations are cumbersome and costly to design and implement
- Insufficiently rigorous design, may produce inexplicable, not easy to find deadlock problems
- The disadvantage of pessimistic locking is that whether it is a page lock or a row lock, the lock may be held for a long time, which may limit the access of other users for a long time. In other words, pessimistic locking has poor concurrent access
Optimistic lock resolution
Optimistic Concurrency Control (also known as Optimistic Concurrency Control, or “OCC” for short) is one approach to Concurrency Control in relational database management systems. It assumes that multi-user concurrent transactions can be processed without affecting each other and that each transaction can process its affected portion of data without locking. Before committing data updates, each transaction checks to see if other transactions have modified the data since the transaction read it. The committed transaction is rolled back if updates are made to other transactions. Optimistic transaction control was first proposed by Professor H.T.Kung.
In contrast to pessimistic locks, optimistic locks do not use the locking mechanism provided by the database when processing the database. A common way to implement optimistic locking is to record the version of the data
Advantages and disadvantages
Optimistic concurrency control believes that the probability of a data race between transactions is low, so proceed as directly as possible and do not lock until commit time, so there are no locks and deadlocks. However, if you simply do this, you can still run into unexpected results, such as two transactions that read a row from the database, modify it and write it back to the database, and then run into problems.
Optimistic lock implementation process
Ensure atomicity of three – step operation
Second kill core service combat
Implement core services hand in hand
- Based on mysql through the version number;
- Based on mysql through the state;
- Implementation of CAS mechanism based on Memcached;
The cache implements an optimistic locking scheme
Further reflection
- The CAS mechanism will completely solve the SEC kill problem?
- The whole picture of a seckill system from an architect’s point of view?