Data query is not synchronized due to nginx load balancing

Source: Internet
Author: User
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.

  • 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.