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.