Production environment with NGINX implementation of a, B two server load Balancing,
A, B share the same database。
Device a PHP interface made the data
the judgment of storage uniqueness, there is no storage, there is no save.
Now the discovery device
At the same timeUse
the same dataRequested a
Three playsinterface, the database actually found
three sameThe data.
It is now certain that there is no problem with the interface program.
So where is the problem? And how to optimize it?
At first I thought it was a procedural logic problem, but I used the same data for loop 4 times, found that only saved once, the other three times return data duplicate error code
Reply content:
Production environment with Nginx to achieve a, b server load Balancing,A, B share the same database .
Device a PHP interface to do the data storage of the uniqueness of the judgment , there is no storage, there is no memory.
Now that the device has requested three interfaces with the same data at the same time , the database has found three of the same data.
It is now certain that there is no problem with the interface program.
So where is the problem? And how to optimize it?
At first I thought it was a procedural logic problem, but I used the same data for loop 4 times, found that only saved once, the other three times return data duplicate error code
You use for no problem because, for is one processing.
One is to add a unique index
One is to use a transaction and then the query time exists with select for update
One is handled by the queue.
The problem occurs when the concurrency is large, even on a single server.
1. A redis count is added to the middle, incr atomic operation.
2. data table plus unique field index, let the database to solve this problem.
You mean the same client. 3 requests + data or 3 client requests for one request
The problem is here: the decision to store uniqueness is not stored, and there is no existence.
You're not doing business here.
A judgment does not exist, at this time B is writing a, then a has been judged not to exist, a and wrote a.
The judgment should not exist and be written into a transaction to be processed.
If the database engine does not support transactions, implement the transaction lock yourself.