About unique numbers and ID’s

I am sure everyone of us had the requirement to save data in a DB, a hash or whatever. Most of the times we were creating this unique numbers, often used as keys for reference, by ourselves simply counting +1. Sooner or later we realized, oops I used the number already or I need to merge tables and now we have duplicate ID’s and so on and so on. Most DB offer some auto-increment feature or you make use of the row-id as oracle create one (internally for each record). I came across the UUID (wikipedia link), which is a universal unique ID creation method. In Java its damn simple to create one:

import java.util.UUID;
UUID uID = UUID.randomUUID(  );

I worked once for a company which used long integer as their ID field. The max value is 2,147,483,647 and recently they hit the limit facing the problem of hardcoded long integer all over the sourcecode and DB. I guess the UUID would help (using it from the start).
Remark: Putting in a lot of brain power they fixed the problem without refactoring about 1 mill lines of code.

With the help of 122 significant bits in a version 4 UUID you can create 2^122 (5,316,911,983,139,663,491,615,228,241,121,378,304) numbers ! Enough for the lifetime of this universe !


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s