Release the activation code, how to ensure that a user can only get one?

Source: Internet
Author: User
I now have 10,000 activation code in advance, ask the user to request (can get the user ID), from the 10,000 activation code to extract a user, each user can only get 1.

Here's what I'm designed to do:
Use Redis to store the ID of this 10,000 activation code (the primary key is increased from 1 to 10,000), and when the user requests it, an ID is taken from Redis. Using the atomicity of Redis, each request is guaranteed to have a different activation code.
Then according to the ID to MySQL to find the activation code, the status is set to "issued", while adding "User ID, Activation code ID" record in the record table, and then the actual content of the activation code back to the user. So that the user every request, first check the record table there is no this user record, some words will indicate that the return message has been withdrawn, no words to extract the activation code back to the user.

Originally I thought no problem, but found that the fact that the record table appeared 1 users to get 2 activation code, the 2 records are generated in the same second. I guess this might be the case: a user made 2 requests, the interval of 2 requests was very small, so the 2nd request to detect the record table record, the first request "Insert record table operation" is not completed, resulting in 2 requests have passed.

Excuse me, is that the reason? To ensure that a user can only get one at most what to do?

Reply content:

I now have 10,000 activation code in advance, ask the user to request (can get the user ID), from the 10,000 activation code to extract a user, each user can only get 1.

Here's what I'm designed to do:
Use Redis to store the ID of this 10,000 activation code (the primary key is increased from 1 to 10,000), and when the user requests it, an ID is taken from Redis. Using the atomicity of Redis, each request is guaranteed to have a different activation code.
Then according to the ID to MySQL to find the activation code, the status is set to "issued", while adding "User ID, Activation code ID" record in the record table, and then the actual content of the activation code back to the user. So that the user every request, first check the record table there is no this user record, some words will indicate that the return message has been withdrawn, no words to extract the activation code back to the user.

Originally I thought no problem, but found that the fact that the record table appeared 1 users to get 2 activation code, the 2 records are generated in the same second. I guess this might be the case: a user made 2 requests, the interval of 2 requests was very small, so the 2nd request to detect the record table record, the first request "Insert record table operation" is not completed, resulting in 2 requests have passed.

Excuse me, is that the reason? To ensure that a user can only get one at most what to do?

Do another verification when you insert MySQL

Using a certain algorithm, according to the user ID generated corresponding activation code, and then storage.

Guaranteed to be unique and not duplicated.

How about this?

The client submits once and is not operational until the return value is received.
Must be callback to unlock

Why not first "user ID, Activation code ID" record also first exist Redis?

Is it not possible to save the invitation code directly in Redis?

If it was accidentally clicked two times, then why not use the JS limit can only click once? Or would you like to generate a unique token at the time of submission?
Isn't that a solution?

User operation plus lock can:)
Before operating on the DB, etc. Based on user information such as account generation MD5 key query in memcache whether there is a value does not exist key then set to complete the operation of the DB Insert Del key

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