On the problem of concurrency

php backend needs to insert records into the mysql database table. The id of this table is primary key but not self-growing, which needs to be calculated by some rules. For example, if you first check that there are N records created that day, then set the id to xxxN+1. this time. If there is a large number of visits, multiple requests may get the same number of records first, so it is possible to generate the same id, and insert it into the database, then an error will be reported. How should we deal with this situation?

Mar.03,2021

1, if the business requirements are very consistent, add locks, or thread blocking to ensure the strong consistency of the array
2, use non-relational databases such as redis or mongodb or mecache
3, use queues


the first way: change the rules of the primary key to self-increment or other random algorithms to generate this simpler form.
the second way: to maintain the generation and use of primary keys separately, you can generate a certain number of primary keys in advance in accordance with the original rules, get a primary key from it every time you insert a record, and then destroy it, but you need to pay attention to the appropriate time to supplement the primary key. In fact, this is to establish a dynamic data pool.


Redis's incr can solve


locking the database is not a good way, which may cause the commit to fail when concurrency.
it is recommended that you modify the database schema and change the current primary key to the unique index.


it is suggested that id do the reserved field. As you said, the rule is to make the date as a field and the record number of the day as a field to establish a joint unique index

.

redis to count the number of records for the day. If the concurrency is large, you can consider queue consumption

.
Menu