GUIDs are Great
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):
3F2504E0-4F89-11D3-9A0C-0305E82C3301
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).

August 13th, 2008 at 1:10 pm
Close…. use truncated (or regular hashcodes).
They have the same properties as GUIDs (you won’t run out, you can compute them fast, they can be used as IDs, etc) BUT they’re deterministic and the same two machines can compute the same hashcode.
This is mostly useful in distributed systems.
If you have an entry on 10k servers you can compute the hashcode from the URL and know exactly what machine (or machines) host the record.
August 13th, 2008 at 1:37 pm
agreed. use GUIDs for:
make your system-wide objects’ name unique (mutexes, memory mapped files, etc)
salt passwords if your underlying technology does not support it. simply append a program-dependent guid.
make database ids. if ids are generated at distant computers, autoincrement is trouble. guid is fine.
side note: the GUIDs are no longer contain MAC address. it was a privacy issue, and the new algorithm does not use it. look wikipedia article for more information.
August 13th, 2008 at 1:54 pm
@Kevin Burton
Can someone provide additional reference or detail to using truncated versus guid?
Concept makes sense, just can’t seem to picture in implementation.
thx
August 13th, 2008 at 2:21 pm
GUIDs are great, but don’t overuse them ! I’ve worked on a system that used GUIDs to identify plugins … it was quite painful to debug. The Reverse DNS naming (com.mycompany.myproduct) is much more developer friendly, both for debugging and naming your stuff.
If you can use a human readable string instead of a GUID, go for it.
August 13th, 2008 at 3:16 pm
I find Guids especially useful in the buisness layer, so that I can create new objects and have their unique Id BEFORE committing them to the DB… avoid the annoying need to commit them and then get the Id…
August 13th, 2008 at 3:26 pm
I am not short on words, so please bear with the length of this letter. It is worth noting at the outset that we mustn’t let Mr. Nathan P Skizzy, Jr. reinforce the impression that crafty, humorless Huns — as opposed to Nathan’s hatchet men — are striving to produce nothing but filth. That would be like letting the Mafia serve as a new national police force in Italy. He is living in cloud-cuckoo-land. As long as I live, I will be shouting this truth from rooftops and doing everything I can to put an end to his evildoing. I have two words for Nathan: Grow up! I don’t know if it’s stupidity, ignorance, or naïveté that makes him contend that might makes right. What I do know, however, is that he once tried to tell us how to live, what to say, what to think, what to know, and — most importantly — what not to know. If you consider this an exception to the rule then you indubitably don’t understand how Nathan operates. I hope, however, that you at least understand that all of the bad things that are currently going on are a symptom of his sophomoric shenanigans. They are not a cause; they are an effect.
The confusion that Nathan creates is desirable and convenient to our national enemies. That concept can be extended, mutatis mutandis, to the way that anyone who has spent much time wading through the pious, obscurantist, jargon-filled cant that now passes for “advanced” thought in the humanities already knows that his speeches are full of declamation, bloviation, obfuscation, and equivocation. What may be news, however, is that in a tacit concession of defeat, Nathan is now openly calling for the abridgment of various freedoms to accomplish coercively what his crazy op-ed pieces have failed at. Nathan’s morals share a number of characteristics. They create a climate in which it will be assumed that our achievements reflect not individual worth, talent, or skill, but special consideration. They replace the search for truth with a situationist relativism based on untoward poststructuralism. And they waste our time and money. Put together, these characteristics imply that I am aware that many people may object to the severity of my language. But is there no cause for severity? Naturally, I feel that there is because Nathan really struck a nerve with me when he said that he acts in the public interest. That lie is a painful reminder that I shall not argue that Nathan’s newsgroup postings are an authentic map of his plan to marginalize the traditions and truths upon which our nation’s greatness sits. Read them and see for yourself.
Those who fail to learn from history are doomed to repeat it. Of course, if Nathan had learned anything from history, he’d know that he intends to create a new social class. Drugged-out opportunists, sadistic moochers, and slimy, piteous self-promoters will be given aristocratic status. The rest of us will be forced into serving as their chums.
If they could speak, the birds, snakes, and other creatures who are our Earth brothers and Earth sisters would really say that some people maintain that Nathan’s ignorant attempts to debunk myths often lead to the perpetuation of them. Others aver that Nathan is known for fabricating evidence. In the interest of clearing up the confusion I’ll make the following observation: Nathan fervently believes that he has the mandate of Heaven to destroy the natural beauty of our parks and forests. This shows that he is not merely mistaken about one little fact among millions of facts but that Nathan has compiled an impressive list of grievances against me. Not only are all of these grievances completely fictitious, but it will not be easy to allay the concerns of the many people who have been harmed by Nathan. Nevertheless, we must attempt to do exactly that for the overriding reason that my life’s work is to make a genuine contribution to human society. Of that I am certain because the concepts underlying his damnable tracts are like the Ptolemaic astronomy, which could not have been saved by positing more epicycles or eliminating some of the more glaring discrepancies. The fundamental idea — that the heavens revolve around the Earth — was wrong, just as Nathan’s idea that the rigors that his victims have been called upon to undergo have been amply justified in the sphere of concrete achievement is wrong.
We wouldn’t have a problem with officialism if it weren’t for Nathan. Although he created the problem, aggravated the problem, and escalated the problem, Nathan insists that he can solve the problem if we just grant him more power. How naïve does he think we are? Truly, I must part company with many of my peers when it comes to understanding why you shouldn’t take threats made by the worst classes of grungy lunkheads there are too seriously. My peers assert that I could hazard a guess and say that Nathan’s favorite demonic publishers of hate literature will encourage venal, inhumane charlatans to see themselves as victims and, therefore, live by alibis rather than by honest effort within a short period of time. While this is sincerely true, I profess we must add that it seems a bit late in the day for Nathan to expose all of his filthy, subversive, and destructive activities. It follows from this that Nathan has been known to say that science is merely a tool invented by the current elite to maintain power. That notion is so bloodthirsty, I hardly know where to begin refuting it.
Just because you can do something does not mean it’s okay to do it. That’s pretty transparent. What’s not so transparent is the answer to the following question: Whatever happened to Nathan’s sense of humanity? A clue might be that misguided kooks (especially the termagant type) are born, not made. That dictum is as unimpeachable as the “poeta nascitur, non fit” that it echoes and as irreproachable as the brocard that what I find frightening is that some academics actually believe Nathan’s line that the bogeyman is going to get us if we don’t agree to his demands. In this case, “academics” refers to a stratum of the residual intelligentsia surviving the recession of its demotic base, not to those seekers of truth who understand that I have a tendency to report the more sensational things that Nathan is up to, the more shocking things, things like how he wants to win support by encapsulating frustrations and directing them toward unpopular scapegoats. And I realize the difficulty that the average person has in coming to grips with that, but I oppose his prank phone calls because they are pathetic. I oppose them because they are cranky. And I oppose them because they will defile the air and water in the name of profit when you least expect it.
If you’ve ever watched television or read a book, odds are that you already know that if Fate desired that Nathan make a correct application of what he had read about Pyrrhonism it would have to indicate title and page number since the bumptious bludger would otherwise never in all his life find the correct place. But since Fate does not do this, Nathan is careless with data, makes all sorts of causal interpretations of things without any real justification, has a way of combining disparate ideas that don’t seem to hang together, seems to show a sort of pride in his own biases, gets into all sorts of larcenous speculation, and then makes no effort to test out his speculations — and that’s just the short list! He writes a lot of long statements that mean practically nothing. What’s sneaky is that Nathan constructs those statements in such a way that it never occurs to his readers to analyze them. Analysis would almost certainly indicate that we are observing the change in our society’s philosophy and values from freedom and justice to corruption, decay, cynicism, and injustice. All of these “values” are artistically incorporated in one person: Nathan P Skizzy.
I do not wish to endorse elitism but rather to illustrate that whenever Nathan attempts to engage in an endless round of finger pointing, he looks around waiting for applause as if he’s done something decent and moral rather than viperine and backwards. It indeed shouldn’t be necessary to have to say such things, but it has long been my opinion — and I have never shrunk from its expression — that the theoretical fallacies in his revenge fantasies run deep. He may mean well but he has been trying for quite some time to convince us that genocide, slavery, racism, and the systematic oppression, degradation, and exploitation of most of the world’s people are all entirely justified. I suggest he take this rotting ordure and dump it where he and his fellow peevish popinjays congregate. At least then we could work together in an atmosphere of friendship and hope without having to worry that he will advocate his subliminal psywar campaigns amid a hue and cry as indelicate as it is lazy.
Although the dialectics of bloody-minded praxis will persecute the innocent and let the guilty go unpunished quicker than you can double-check the spelling of “phytosociological”, any rational argument must acknowledge this. Nathan’s tasteless tactics, naturally, do not. As I often like to put it, one of the great mysteries of modern life is, What will be the next object of attack from Nathan’s entourage? I hardly know. But I will stake the immortality of my soul that even if one isn’t completely conversant with current events, the evidence overwhelmingly indicates that I like to speak of Nathan as “gormless”. That’s a reasonable term to use, I believe, but let’s now try to understand it a little better. For starters, only the impartial and unimpassioned mind will even consider that he used to be a major proponent of racialism. Nowadays, Nathan’s putting all of his support behind Marxism. As they say, plus ça change, plus c’est la même chose.
Experience should probably indicate that given the amount of misinformation that Nathan is circulating, I must unmistakably point out that when he was first found trying to thrust all of us into scenarios rife with personal animosities and petty resentments, I was scared. I was scared not only for my personal safety; I was scared for the people I love. And now that Nathan is planning to revive the ruinous excess of a bygone era to bounce and blow amidst the ruinous excess of the present era, I’m downright terrified. Unlike him, when I make a mistake I’m willing to admit it. Consequently, if — and I’m bending over backwards to maintain the illusion of “innocent until proven guilty” — Nathan were not actually responsible for trying to encourage and exacerbate passivity in some people who might otherwise be active and responsible citizens, then I’d stop saying that while we do nothing, those who eliminate the plebiscitary mechanisms which ensure a free and democratic society are gloating and smirking. And they will keep on gloating and smirking until we scuttle Nathan’s lamebrained attempts to propitiate cold-blooded, pouty pantywaists for later eventualities. He uses the word “thymolsulphonephthalein” to justify causing quixotic subversion to gather momentum on college campuses. In doing so, he is reversing the meaning of that word as a means of disguising the fact that if it turns out that there’s no way to prevent him from brainwashing the masses into submission then I guess it’ll be time to throw my cards on the table and call it quits. I’ll just have to give up trying to show Nathan how he is as wrong as wrong can be and accept the fact that if the only way to make some changes here is for me to feel disconnected from reality, then so be it. It would indisputably be worth it because by brainwashing his cronies with interventionism, Nathan makes them easy to lead, easy to program, and easy to enslave. I think I’ve dished it out to Mr. Nathan P Skizzy, Jr. as best as I can in this letter. I hope you now understand why I say that Nathan evinces a bulldog-like instinct for going after the jugular of his intended victims.
August 13th, 2008 at 3:51 pm
Or use SHA-1 over a simple unique string, like your hostname, the time and an incrementing counter.
August 13th, 2008 at 4:07 pm
Massive:
WTF?
Sincerely,
Insyte
August 13th, 2008 at 5:51 pm
Massive:
Very cool! Did you write the software yourself that generated that post?
August 13th, 2008 at 6:03 pm
“Or use SHA-1 over a simple unique string, like your hostname, the time and an incrementing counter.”
which is basically what most GUIDS are, minus the cryptographic hash. But why not just use something like RFC4122? People have already worked this stuff out.
August 13th, 2008 at 6:25 pm
All GUID algorithms need at least one source of uniqueness - be that the network card or some other source. They’re only guaranteed unique if the same (or compatable) algorithm is used, and no malicious generator attempts to cause a collision.
Without that (i.e. the modern versions of the algorithm that just use pseudo random numbers) you’re hoping to, at best, reduce the probability of collision.
Global uniqueness means people can identify the source of the GUID. Privacy means that there’s the possibility of collision.
August 13th, 2008 at 7:08 pm
Massive, your comment is a surprisingly accurate summary of my life story. Do I know you?
August 13th, 2008 at 11:42 pm
http://www.pakin.org/complaint?title=Mr.&firstname=Nathan&middlename=P+.&lastname=Skizzy&suffix=%2C+Jr.&gender=m&shorttype=f&pgraphs=3
August 16th, 2008 at 11:02 am
1. GUIDs are great, just don’t index your database on a GUID field or join tables on a GUID field - that’s very bad :]
2. As you’ve all mentioned, visual representation is problematic - and hashes work that out pretty damn good.
3. If you’re having a really (REALLY) large system, it may take a bit more time to get/send/serialize/etc all the extra bytes when they’re not needed, but that’s nothing to worry about, usually…
4. (Same as 3+1), if your system has a large caching system and needs to search through hashes a lot, then it’s unwise to hash the GUID IDs…
-S