Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locally unique identifier

Question: When you have a .NET GUID for inserting in a database, it's structure is like this:

60 bits of timestamp, 
48 bits of computer identifier,
14 bits of uniquifier, and
 6 bits are fixed, 
----
128 bits total

Now I have problem with a GUID, because it's a 128 bit number, and some of the DBs I'm using only support 64 bit numbers.

Now I don't want to solve the dilemma by using an autoincrement bigint value, since I want to be able to do offline replication.

So I got the idea of creating a locally unique identifier class, which is basically a GUID downsized to a 64 bit value.

I came up with this:

day  9 bit (12*31=372 d)
year 8 bit (2266-2010 = 256 y)
seconds  17 bit (24*60*60=86400 s)
hostname 12 bit (2^12=4096)
random 18 bit (2^18=262144)
------------------------
          64 bits total

My question now is: The timestamp is pretty much fixed at 34 bits, leaving me with 64-34=30 bits for the hostname + random number.

Now my question: 1) Would you rather increase the hostname-hash bitsize and decrease the random bitsize, or increase the random bitsize and decrease the hostname-hash bitsize.

2) Exists there a hash algorithm that reduces every string to n-Bits? n being ideally = 12 or as near as possible.

like image 228
Stefan Steiger Avatar asked Oct 14 '22 01:10

Stefan Steiger


2 Answers

Actually, .NET-generated GUIDs are 6 fixed bits and 122 bits of randomness.

You could consider just using 64 bits of randomness, with an increased chance of collision due to the smaller bit length. It would work better than a hash.

like image 52
Stephen Cleary Avatar answered Nov 02 '22 11:11

Stephen Cleary


If space isn't a concern, then why don't you just use 2 columns that are 64bits wide, then split the guid in half using 8bytes for each then just convert those to your 64bit numbers and store it in 2 columns, then if you ever do need to upsize to another system, you'll still be unique you'll just need to factor in the rejoining of the 2 columns.

like image 28
Paul Farry Avatar answered Nov 02 '22 11:11

Paul Farry