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)?

  • UUIDs are properly implemented when there could be (potentially) several processes generating IDs for the same domain and uniqueness is required. In other words, it makes much sense in distributed systems. If that's not the case, and you expect only one process generating your IDs, you can implement simpler algorithms. – Laiv Nov 5 '18 at 8:51

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. :)

  • My booking Id will have a limitation like 6 alphanumeric characters or 7, or 8 etc. i can create a function generateBookingId(uniqueDbId, maxCharacters(like 6,7,8 etc)). I wouldn't want the uniqueDbId to go out of my system. What could be a possible implementation of this method. – Suyash Nov 5 '18 at 8:13
  • 1
    The id is unique in of itself. If you want it to be smaller still, you could "convert" the id to a base 36 number using numbers and all 26 letters of the alphabet. The resulting value would be potentially smaller than the id and it still maps 1-to-1 with your id. Is this what you mean? – Neil Nov 5 '18 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.