Human Friendly UUIDs - A Better Alternative

1. Introduction

The Universally Unique Identifiers (UUID) are 128 bit numbers. Instead of being represented with decimal digits (0-9), they are represented by hexadecimal digits (0-9A-F), and come to a length of 32 characters. An example would be: fdea637057194b3ba505601212405f55. Usually to be a bit more readable dashes are used, when presenting them out, and final result may like this: fdea6370-5719-4b3b-a505-601212405f55

While being one gigantic number, how are UUIDs are useful. Their usefulness id that when randomly generated the UUIDs are almost unique (sometimes referred as practically unique, but not guaranteed unique). Because of that characteristic, a UUID is an excellent candidate for a primary key, which can be used to uniquely identify a linked resource (i.e an invoice, a vehicle, an object, etc).

2. Why Normal UUIDs are Not So Useful to Humans?

However, while being practically unique, UUIDs tend to be of really little other help. First, being random they mean nothing to a human and they cannot be ordered sequentially in the order they were generated.

While not seeming like much of a drawback, its actually a HUGE drawback. Primarily, because humans may easily attach meaning to numbers, when these numbers can be rationalized.

3. Human Friendly UUIDs - A Better Alternative

The human friendly UUID version described here aims to remove this weakness by adding a human readable, meaningful time element, while keeping the practically unique feature of the UUID.

The human friendly UUID is a number, which when generated holds the time of its generation in a human readable format. The human friendly UUID contains the full year, hour, minute, seconds, and microsecond of its generation, followed by twelve random digits. And looks like this: 20150105215121071500123412341234.

4. Readability Matters - Here Come the Dashes

To improve readability, when displayed to a human as a form of reference (i.e invoice number), the human friendly UUID may be grouped in 4 groups separated by dashes. First group represents year, month and day. Second group represents hour, month and second. Third group represents the microseconds. Fourth group is the random number, which may be further subdivided for readability in groups of four digits each. And the final result looks like this: 20150105-215100-071500-123412341234 or 20150105-215100-071500-1234-1234-1234

Example implementation in PHP 5

                echo date('Ymd-His').'-'.substr(explode(" ", microtime())[0],2,8).'-'.rand(0,999999999999);
            

5. How Practically Unique

While arguable, my personal feeling is that being time based, the human friendly UUIDs are more practically unique than random UUID (v4). And the rationale is that while its true that you may only have a limited number of human friendly UUIDs per second, once this second is gone, its gone FOR EVER, and can NEVER EVER return.

6. Human Friendly Unique ID - A Shorter Alternative

While human friendly UUIDs are great sometimes one may need something a bit shorter. Is it possible to have a shorter version, which is practically unique? Here is a format for a Human Friendly Unique Id (HUID) which gives a practically unique ID, without claiming to be universal.

                YYYYMMDD-HHMMSS-MMMMMM-NNN
            

Note. MMMMMM - represent microseconds, and NNN - represent nanoseconds

7. How Practically Unique are HUIDs

A nanosecond (ns) is a unit of time equal to one billionth of a second (10−9 or 1/1,000,000,000 s). Same is the chance for collision. However, this collision chance exists only this very second, and is gone the next. While with random UUID chances of collision increases with every new UUID, which is generated.

Please Do Not Reprint This Article

This article is copyrighted. Please do not reproduce or distribute this article in whole or part, in any form. Links to this article are welcome

Other Related Articles