Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Service to return unique auto incremented human readable id number

I'm looking to create a simple web service that when polled returns a unique id. The ID has to be human readable (i.e. not a guid, probably in the form 000023) and is simply incremented by 1 each time its called.

Now I need to consider that it may be called by two different applications at the same time and I don't want it to return the same number to each application.

Is there another option than using a database to store the current number?

Surely this has been done before, can anyone point me at some source code if it is.

Thanks,

Neil

like image 738
Neil Avatar asked Mar 22 '10 12:03

Neil


People also ask

How do I get a unique ID?

The simplest way to generate identifiers is by a serial number. A steadily increasing number that is assigned to whatever you need to identify next. This is the approached used in most internal databases as well as some commonly encountered public identifiers.

How do I make a global unique ID?

To Generate a GUID in Windows 10 with PowerShell, Type or copy-paste the following command: [guid]::NewGuid() . This will produce a new GUID in the output. Alternatively, you can run the command '{'+[guid]::NewGuid(). ToString()+'}' to get a new GUID in the traditional Registry format.

How do you create a unique ID in Python?

Generate UUID using Python UUID Module. Let's generates the UUIDs of various versions using the Python uuid module. Using uuid1() - To generate the uuid, we must import uuid module and then call uuid1() method.


1 Answers

Use a critical section piece of code to control flow one at a time through a section of code. You can do this using the lock statement or by being slightly more hardcore and using a mutex directly. Doing this will ensure that you return a different number to each caller.

As for storing it, using a database is overkill for returning an auto incrementing number - although SQLServer and Oracle (and most likely others but i can't speak for them) both provide an auto incrementing keys feature, so you could have the webservice called, generate a new entry in the database table, return the key, and the caller can use that number as a key back to that record (if you are saving more data later after the initial call). This way you also let the database worry about the generation of unique numbers, you don't have to worry about the details of it - although this is not a good option if you don't already have a database.

The other option is to store it in a local file, although that would be expensive to read the file, increment the number, and write it back out, all within a critical section.

like image 99
slugster Avatar answered Oct 13 '22 06:10

slugster