I am facing trouble in creating a unique, customer facing Booking Id. It's a simple thing to do at backend where we can create a UUID and maintain uniqueness. But the UUID looks ugly from a Frontend Customer perspective. I wouldn't want to keep quoting or using this 36 character string everywhere.

I do understand to achieve uniqueness, we HAVE to use something like UUID. So i am stuck here.

What are the ideas to achieve uniqueness for a Booking Id (e.g. ABC123)?

有帮助吗?

解决方案

Generally, the way to accomplish this without some ugly UUID is to rely on the database. By this I mean autoincrement ids or sequences (depends on which database you're using of course). While yes, you could assign the values yourself, but you run into some difficulties with concurrency where you must ensure that retrieving a unique id is an operation which cannot be run contemporarily with other threads. If you wish to go this route, you of course can, but generally you're persisting to a database anyway so it makes just as much sense to use the tools that are already given to you.

Conceptually the idea is simple. You insert the data, and retrieve its unique newly assigned id (ideally in the same operation). That id is its handle and cannot be reassigned to another entity by accident.

If this doesn't answer your question, please let me know in the comments and I'll try to answer whatever doubts you may have.

Edit: If you wish to create a small handle for this id for the user to use, you could convert the id to it's equivalent base 62 form using numbers 0-9, letters a-z and uppercase letters A-Z. The resulting code would be most surely short and easy to remember.

To do this, take your number and take the modulus 62. Map this remainder to values [0-9] [A-Z] [a-z]. Divide this number by 62, and assuming you are left with a non-zero number, repeat the process and append the new number/letter to the left.

For example id 123456 would convert to W7E using this algorithm. To reverse it, simply perform the operations in reverse. If your max is 8 characters, it means your database ids can be as large as 218,340,105,584,895, so I don't think you'll run into problems of that type anytime soon. :)

许可以下: CC-BY-SA归因
scroll top