Home

What Is And Create A GUID

Blog Date 23 July 2021

The Code

        Guid MyGuid = Guid.NewGuid();
        MyShow.Text = Convert.ToString(MyGuid);

The Explanation

GUID is short for Globally Unique Identifier. 

There, sorted. What, you want more? Pffffft. OK. A GUID is a 128 bit random (pseudo-random) collection of 0s and 1s. With 128 bits the idea is that if you create a GUID in your code the chance of ANYONE else's code ever having created the same GUID is very (very) small. There is a chance but it is (allegedly) highly unlikely. 

However in an infinite universe anything is possible. 

So when might you use a GUID? The primary example for a coder what does what I does is to use as a unique ID for a database table. 

Normally I'll set up my tables such that I have an autonumber column. As each record is added to the database that record automatically is incrementally assigned a new number as a primary key. Record 3 is added and assigned the number 3, record 4 is added and assigned the number 4... it's not rocket science.

And on the small systems I've worked on this is just fine and dandy. However even on small systems a GUID could be useful.

Say I've added a record and I need the ID of the new record to then amend that record later in the workflow. I have to add the new record then retrieve the new autonumber's number - 2 calls to the DB (it can be done in 1 call...). Also, even on small systems there's the possibility that 2 records were added at EXACTLY the same time so, errrr, which "new" record is the code referring to??

A GUID could be used. Throw the GUID in with the SQL call and keep the GUID in the session (or cookie or browser's local/session storage). I now have a new record with an ID that I know and can keep for later workflow changes.

The main DB reason though is on larger, more corporate type databases. With thousands if not millions of records being added to databases spread across many physical computers - autoincrement is just a disaster in the making. Let's say server 55 is asked to autoincrement for a new record. It looks at the table and says "the next available number is id 100,000,001" and uses that. At the same time server 33 is also asked to autoincrement for a new record. It looks at the table and says "the next available number is id 100,000,001" and uses that.

Now we have 2 records with the same unique ID for the table. When the many server's master controller synchronises everything it throws a hissy fit because a primary key ID column has 2 records with the same number. 

If only we'd used a GUID huh? Unless you're really really REALLY unlucky if your code is running on 2,000 threads over 500 computers, each instance of your code should create a different GUID and a fresh record even if all 2000 instances are triggered at precisely the same time.

GUIDs are not text. In the example above we can "see" the GUID as a string. This string will be in HEX format (0-9, a-f). You can, if you're lazy like me, use it as a string. Strictly speaking the best way to use it is as a binary. MySQL has a binary data type. Me? Well I ain't working on BIG DATA so I'll most likely be lazy and use it as text.

 

Reader's Comments

Post Your Comment Posts/Links Rules

Name

Comment

Add a RELEVANT link (not required)

Upload an image (not required)

No uploaded image
Real person number
Please enter the above number below




Home
Admin Ren's Biking Blog