The similar questions has already been asked, but my question is quite different with some research done over it.
On signing up to a website, I provide
FirstName: Ajay, LastName: Kumar
Now the backend API need to create a unique-Id from these two texts. Why?
Yes, I have used the autoincrement Integer column for storing the user-id, but this is the Unique_id which will be shown in the URL of the user's profile page, the way most of the websites(fb, twitter, quora) do. I think it helps in SEO for searching the people's profile.
Approaches in my mind:
1. FirstName + LastName + mysql-auto-generated-id
API cannot know the autogenerated ID before the creation of the user.
2. Concat FirstName & LastName and us it as unique id, if the DB fails
and returns id violation error, then start appending digits starting from 0 until an valid Id is found.
It increases the database roundtrips.
3. FirstName + LastName + [random integer numbers]
It also increse the database roundtrip.
4. Use UUID concatenated to FirstName + LastName.
Since UUID is 128-bit, that's quite long and I need to substring it. This could again cause duplicate IDs
5. concatenating current timestamp with FirstName + LastName.
But that value is also quite long.
The most efficient way I have is to use UUID and substring 4-5 starting characters from it. If the unique-id is already taken, try with another UUID. I think this is best in case of decreasing the database roundtrips as well.
I am curious to know how websites handle this(other than this database recursive calls until a valid Unique-id is found)? There is proper digit assingnment in unique-id(e.g in Quora). E.g tim-cook-1
, time-cook-2
.