Unique, numeric, incremental identifier

I need to create a unique, incremental, numeric transaction identifier for each request I make for a specific XML RPC. These numbers should be unique only for my domain, but will be created on several computers.

I really do not want to keep track of this number in the database and engage in row locking, etc. on every single transaction. I tried to crack it with a microsecond timestamp, but there were collisions with only a few threads - my application must support hundreds of threads.

Any ideas would be appreciated.

Edit: what if each transaction id should be larger than the previous request?

+3
source share
7 answers

If you intend to use it from hundreds of threads, working on several machines and requiring an incremental identifier, you will need some centralized place to store and lock the last generated identification number. This does not have to be in the database, but it will be the most common option. A central server that did nothing but served identifiers could provide the same functions, but probably defeated the goal of distributing this.

If they must be incremental, any form of timestamp will not be guaranteed unique.

, , GUID . + , .

+ ? , .

---- EDIT -----

, - timestamp .

-, , 2 , , . , .

-, , , , , .

+4

, . , "incremental" "numeric" - - - ?

, UUID, . (!) . :

, 1 UUID 100 , 50%.

+1

"incremental" , GUID.

, - .

0

Windows, Interlocked API?

0

Google GUID , , , , , .

"" ( ) "" , . .

0

GUID, , db4o SQL Lite

0

If each client can track its own “next identifier”, you can talk to the clock server and get a range of identifiers, perhaps 1000 at a time. As soon as the client runs out of identifier, he will again have to talk to the server.

This will cause your system to have a central id source, and still avoid having to talk to the database for each identifier.

0
source

Source: https://habr.com/ru/post/1704726/


All Articles