
Because every identifier wants to feel special. When you’re building a system that moves data around between different components, one of the first problems to tackle are how do you make sure all identifiers are unique. You can use sequences inside the code, but identifiers stop being unique when the application restarts. You can use auto increment keys in the database, but that only works if the data is tied to one specific table. There are hacks like using pre-allocated sequences backed by a file, or a sequence table in the database. But all tend to get a little complicated when you want to not worry about how the identifiers are generated, or who gets used to them. I just prefer using UUIDs. UUIDs are guaranteed to be “unique across time and space”, and with a 128-bit space, that’s not a big problem. The time-based algorithm I use relies on the system clock to generate new identifiers that are never repeated, and appends a sequence number to deal with two (or more) applications seeding from the same machine, or the system clock being set backwards. Each UUID also includes an identifier unique to that machine, using the network card’s MAC address, a 47-bit value that’s individually assigned to each network card. A UUID looks something like this:
p UUID.new » "5158feb0-2293-0128-163b-080e46214b35"p UUID.new » "ff2c0c80-2293-0128-163c-080e46214b35"
So feel free to get the code and play with it. And, if you have a quick and simple way for extracting a MAC address in Ruby, I’d like to integrate that as well.