Data Query is not synchronized due to nginx Load Balancing

Source: Internet
Author: User
The production environment uses nginx to achieve load balancing between servers A and B. A and B share the same database. A PHP interface of the device determines the uniqueness of data storage. If the device does not exist, the device stores the data and does not store the data. Now it is found that the device requests three interfaces with the same data at the same time, and the database actually sends... the production environment uses nginx to achieve load balancing between the two servers A and B, A and B share the same database.
A PHP interface of the device performs data Determination of storage uniquenessIf it does not exist, it is stored. If it exists, it is not stored.
Now we find that the device is Same timeUse Same dataRequested Three timesInterface, the database actually discovered Three identical.
It is certain that the interface program is correct.
So where is the problem? How can we optimize it?

At first, I thought it was a program logic problem, but I used the same data for loop for four times and found that it could only be saved once, and the error code returned for repeated data three times

Reply content:

Use nginx in the production environment to achieve load balancing between servers A and B,A and B share the same database.
A PHP interface of the device performs dataDetermination of storage uniquenessIf it does not exist, it is stored. If it exists, it is not stored.
Now we find that the device isSame timeUseSame dataRequestedThree timesInterface, the database actually discoveredThree identical.
It is certain that the interface program is correct.
So where is the problem? How can we optimize it?

At first, I thought it was a program logic problem, but I used the same data for loop for four times and found that it could only be saved once, and the error code returned for repeated data three times

There is no problem with using for because for is handled one by one.

One is to add a unique index.
One is to use transactions, and then use select for update when the query exists.
One is to process through the queue

When concurrency is high, problems may occur, even on a single server.
1. Add redis count and incr atomic operation in the middle.
2. Add a unique field index to the data table so that the database can solve this problem.

You mean that a single request from the same client results in 3 Requests + data or 3 client requests

The problem lies in this: the storage uniqueness judgment. If the storage does not exist, the storage will not exist.

You have not done any transactions here.

A determines that it does not exist. At this time, B is writing one, and then A determines that it does not exist, and A writes another.

We should put the judgment that does not exist and write one into a transaction for processing.

If the database engine does not support transactions, implement transaction locks by yourself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.