Concurrent duplicate data problems

Source: Internet
Author: User
The requirement is that each piece of data has a number at the same time (accurate to milliseconds). If the number at this time in the database exists, the count is updated. If the number does not exist, add one. The program is to first query and execute insert or update based on the result. There are some, at the same time, the same... the requirement is that at the same time (accurate to milliseconds), each piece of data has a number.
If a number exists at this time, the count is updated. If no number exists, a new one is added.

The program first queries and inserts or updates the program based on the results. There are some cases where the same number exists at the same time.
It should be caused by concurrency. Is it possible to solve this problem only by locking in queries, inserts, and updates?
If multiple tomcat clusters exist, can they be solved only by distributed locks?

Reply content:

The requirement is that at the same time (accurate to milliseconds), each piece of data has a number.
If a number exists at this time, the count is updated. If no number exists, a new one is added.

The program first queries and inserts or updates the program based on the results. There are some cases where the same number exists at the same time.
It should be caused by concurrency. Is it possible to solve this problem only by locking in queries, inserts, and updates?
If multiple tomcat clusters exist, can they be solved only by distributed locks?

The default value is 0 when the data is created. You can simply + x... Why insert

Mysql syntax can solve this problem.

INSERT... ON DUPLICATE KEY UPDATE

Link INSERT... ON DUPLICATE KEY UPDATE

First, how large your concurrency is. If you update the database directly, it will put a lot of pressure on the database.

Provide another way of thinking, using redis
Another method in redis, INCR, can be atomic increments

Redis is a good choice in this scenario.

The main problem is to remove duplicates. Use an hset to insert the key of each record. before inserting a new value, run the hget command to check whether the record has been inserted and no counting is inserted.
INCR as the Count is certainly no problem, read this value.

You can use the database lock mechanism.

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.