Coupon code generation scheme

Source: Internet
Author: User
The project encountered a solution to generate a unique coupon code. there are currently two coupon Generation Schemes:. A batch of coupon codes are generated in advance (a unique index is added for this field in the database), which ensures the uniqueness of the coupon code. It is determined by the background script that the number of coupon codes is less than a certain value, generate a batch of coupon codes B. combined with redis... the project encountered a solution to generate a unique coupon code. there are currently two coupon generation schemes:

A. A batch of coupon codes are generated in advance (the unique index is added for this field in the database) to ensure the uniqueness of the coupon code. The background script pre-judges that the number of coupon codes is smaller than a certain value and generates a new batch of coupon codes.

B. in combination with the redis solution, set the cursor through redis. Take the eight-digit coupon code as an example to find the maximum eight-digit prime number, and then calculate the eight-digit coupon code through the prime number algorithm and cursor, however, the uniqueness of coupon codes that cannot be guaranteed by this method may be repeated.

Solution A is unique but difficult to maintain. Solution B cannot be unique.

Do you have any good algorithm recommendations?

Reply content:

The project encountered a solution to generate a unique coupon code. Currently, there are two methods to generate a coupon code:

A. A batch of coupon codes are generated in advance (the unique index is added for this field in the database) to ensure the uniqueness of the coupon code. The background script pre-judges that the number of coupon codes is smaller than a certain value and generates a new batch of coupon codes.

B. in combination with the redis solution, set the cursor through redis. Take the eight-digit coupon code as an example to find the maximum eight-digit prime number, and then calculate the eight-digit coupon code through the prime number algorithm and cursor, however, the uniqueness of coupon codes that cannot be guaranteed by this method may be repeated.

Solution A is unique but difficult to maintain. Solution B cannot be unique.

Do you have any good algorithm recommendations?

Timestamp, random number, hash function, user information (such as user ID, requested IP address or something), etc ......
Depending on the specific situation, it can be used together to basically achieve that the coupon code generated each time is not repeated.
If the requirements are relatively high, you can create a table and create a unique index. After the index is generated, try to insert it to verify whether it is repeated.
(If the number is repeated, the random number will be re-generated and re-calculated and generated once .)
In advance, the code is complex, and it is better to calculate it in real time when necessary.

Time-related things will be unique, right?

Redis auto-increment can be used, which is atomic and will not be repeated. If it is longer, uuid can be used.

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.