DevRohan
DevRohan

Reputation: 81

Approach to generate Short Unique key like (AboU8N) in distributed environment?

I know a few of the approaches to generating unique identifiers :

The first Naive approach would be just to put auto-increment on the database column, but it does not provide uniqueness in a distributed environment.

The second approach would be to use a central database only to generate unique IDs, but I don't think it will scale that much

The third approach would be to hash the given URL and generate a hash of it, but well it takes way more space and if I take the preceding part of it there is much chance of collision too.

The well-known approach nowadays is to use something like a Twitter snowflake that contains well mixture of epoch, worker, and sequencer, It scales well but it does not fulfill the problem of short identifier. All I can do with this is generate a base64 string and take the preceding part of it again I have a problem with collision.

Do you guys have worked with similar kinds of problems of short unique ID generation, for let's say not more than 8 characters long? if yes then what was or will be your approach to solving this kind of problem? I tried all of the approaches above for my link shortner app but it did not give me satisfactory results.

All kinds of responses will be well appreciated regardless of any programming language.

Upvotes: 1

Views: 45

Answers (1)

Ian Boyd
Ian Boyd

Reputation: 256991

  • 4-byte timestamp with millisecond precision
  • 1-byte collision counter, in case you generate ID's faster than one per millisecond
  • 3-byte node id (allowing 224-1 = 16-million users)

Giving:

........tttttttt  (timetsamp)
......cc........  (collision counter)
nnnnnn..........  (node id)

Resulting in an 8-byte value:

0xnnnnnncctttttttt  

Which can be stored in an Int64.

You can then convert 8-bytes into 8 characters.

If you're willing to use Unicode, you could get it into 4 "characters".

Upvotes: 1

Related Questions