S
S
serious9112016-09-21 21:52:12
JavaScript
serious911, 2016-09-21 21:52:12

Order code generation?

Hello.
On the site, users place orders for goods and it is necessary to generate unique identifiers for each order in the following format:

(Letter)(Letter)(Number)(Number)(Number)

For example, AA545, AB434, etc.
What is the problem?
1) The identifier must be unique and in the given format.
2) The database is MongoDB and there are no transactions / auto-increment in it.
At the moment I see 2 implementation options:
1) Come up with some kind of algorithm for generating random codes (you need to solve the problem of collisions)
2) Store the counter somewhere in the database and constantly increment , but it’s not clear how to deal with a lot of simultaneous connections (there seem to be no transactions in MongoDB) + you need to generate code in a given format
Tell me how to correctly solve this problem.
Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ivan, 2016-09-22
@LiguidCool

MySQL? Postgres?
Well you get it...

V
Vitaly, 2016-09-21
@vshvydky

Generate these codes ahead of time and the status of the code is used or free, and then to mongo request to get a free limit of 1 update used, but this solution seems wild to me.

A
Andrew, 2016-09-22
@OLS

According to your option 1) - The required information capacity (19 bits) is too small to deal with collisions, because they obey the birthday paradox - after 700 orders you have a 50% chance of having the first collision.
According to your option 2) - get a counter and encrypt it with some fixed key with a block size of 19 bits. Encryption is a bijective operation - it will guarantee the uniqueness of the ciphertexts.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question