Whenever someone says theyâ€™re going to use a GUID for something, I make it a point to always respond, â€œNo!Â Donâ€™t use a GUID there!Â If you use one there, eventually youâ€™ll use them all up and we wonâ€™t have any left!â€
Of course, GUIDs donâ€™t get â€œused up,â€ and there are plenty to go around (enough for every star in the observable universe to have about 6.8 quadrillion GUIDs to itself), but this point if this post is to tell you how to use them.
A Globally-Unique Identifier (GUID) is generated algorithmically, usually using your network cardâ€™s MAC address (edit: this is no longer the case, please see comments below) as well as the number of 100 nanosecond periods elapsed since 12:00 AM on October 15, 1582 (naturally).Â Using these two pieces of data ensures that your computer wonâ€™t generate the same GUID twice and two different computers wonâ€™t generate the same GUID at the same time.Â Itâ€™s not a â€œ100% guaranteeâ€ but itâ€™s very, very reliable.Â What you get is something that looks like this (as a string):
Despite being a bit unwieldly to type and look at, GUIDs are really good for uniquely identifying logs, transactions, database records and all kinds of things in your code.Â On modern hardware, they can be generated quickly and the standard algorithm allows for 10 million GUIDs to be generated per second without conflict.
Often developers will use a timestamp, a random number or both to try to unique-ify some filename or result.Â This is called â€œreinventing the wheel.â€Â Use a GUID instead, any time you want to tag anything that canâ€™t have a conflict.Â Save yourself future headaches.
If you want to see the classic example of a bug resulting from the practice of using a timestamp as a unique id, look no further than Lotus Notes.Â In older versions of Notes, â€œuniqueâ€ document ids were generated using a timestamp with a resolution of 1/100th of a second.Â To avoid an id conflict when new documents were created very quickly, the implementation would increment an internal counter when it saw a conflict in ids.Â The end result was that if you managed to create, say, 100,000 documents in a single second, you would have documents that appeared to be created 1,000 seconds (about 16 minutes) in the future (since the unique id and the create date were actually the same field).Â This disconnect with reality then could cause databases to fail to replicate properly.Â The problem actually still exists in current versions of Notes, although it has been alleviated by some other architectural changes (but they still use creation date as the unique id).
In defense of the Lotus Notes developers, when it was designed, there was no hardware that could generate records that quickly, and Iâ€™m fairly certain that the GUID standard wasnâ€™t around at the time.
Nowadays, thereâ€™s no excuse to roll your own unique identifier system.Â Use a GUID!
If youâ€™re interested in the internals of how GUID generation is implemented, Iâ€™d recommend looking at this document (PDF).
Related Posts: On this day...
- How Did Greek Yogurt Get So Popular? - 2011
- Meet The YouTube Stars Making $100,000 Plus Per Year - 2010
- Sheriff reprimands deputies over waitress photo - 2009
- Why AT&T Killed Google Voice - 2009
- A "hacker" broke into a Homeland Security Department telephone system over the weekend and racked up about $12,000 in calls to the Middle East and Asia - 2008