tags:

views:

167

answers:

5

I'd like to create 9-digit numeric ids that are unique across machines. I'm currently using a database sequence for this, but am wondering if it could be done without one. The sequences will be used for X12 EDI transactions, so they don't have to be unique forever. Maybe even only unique for 24 hours.

My only idea:

  1. Each server has a 2 digit server identifier.
  2. Each server maintains a file that essentially keeps track of a local sequence.
  3. id = + <7 digit sequence which wraps>

My biggest problem with this is what to do if the hard-drive fails. I wouldn't know where it left off.

All of my other ideas essentially end up re-creating a centralized database sequence.

Any thoughts?

+2  A: 

How about generating GUIDs (ensures uniqueness) and then using some sort of hash function to turn the GUID into a 9-digit number?
Just off the top of my head...

Traveling Tech Guy
that was my first thought...
Kip
You have to think hard for the hash function, no ?
Vitaly Polonetsky
A hashed guid is no longer unique.
pb
A: 

Cold you use the first 9 digits of some other source of unique data like:

  1. a random number
  2. System Time
  3. Uptime

Having thaught about it for two seconds, none of those are unique on there own but you could use them as seed values for hash functions as was suggested in another answer.

Crippledsmurf
+2  A: 

If HD fails you can just set new and unused 2 digit server identifier and be sure that the number is unique (for 24 hours at least)

Vitaly Polonetsky
This is probably my best bet. User pb's answer would work except that we might generate too many per minute for this to be workable.So I'll need to figure out how to track which ids are being used and when they become available again.
Dave
+2  A: 

The Following

{XX}{dd}{HHmm}{N}

Where {XX} is the machine number {dd} is the day of the month {HHmm} current time (24hr) and {N} a sequential number.

A hd crash will take more than a minute so starting at 0 again is not a problem.

You can also replace {dd} with {ss} for seconds, depending on requirements. Uniqueness period vs. requests per minute.

pb
A: 

Use a variation on:

md5(uniqid(rand(), true));

Just a thought.

grantwparks