Ad

Generating Short Unique Reference Id With PHP Uniqid Function

- 1 answer

My objective is to create a unique id attach it to a record and store it in the DB. The id should be alpha - numeric and short. I don not want to use the auto increment way of generating ids as this ID is going to be attached to end of a url so I don't want it to be guessable. After some research I have decided to use uniqid() as i understood that its generated based on time.

My worry is will it be unique across the system and is there a chance for the id to become repeated if i am going scale my system using multiple servers and clustering??

i don't want to do a duplicate check before inserting the value into the DB as this can affect the performance over time as records increase.

I am using Larvel framework. Can anyone suggest any other solution to tackle this problem. I am new to laravel.

Thanks in advance

Ad

Answer

Thanks for all the suggestions. I have decided to use uniqid() with prefix as user_id. As uniqid() is system time dependent and can cause a conflict when 2 simultaneous requests hit the server, the user_id prefix will make sure that even if simultaneous request comes it will generate different ids since the user_id associated with each request will be different.

Ad
source: stackoverflow.com
Ad