photo by Aaron Burden

In the latest release 1.2.0 of id-generator we added a user friendly way of generating ids. Actually the id generation was there for some time: IdGenerator based on Twitter’s Snowflake but we added utility classes to convert Long into user friendly string.

It’s friendly because:

it is monotonic (as underlying Long ), so you may sort it like a plain old string

), so you may sort it like a plain old string it has checksum embedded — so it may be validated

it is easier to read or to be typed in by humans e.g. 9223372036854775807 vs. HPJD-72036-HAPK-58077

vs. by default it has fixed length

customizable — you may define: length of decimal parts, chars that would be used in letter parts and separator.

you may recover seed Long id from pretty id

id from pretty id by default it doesn’t use letters such O and I which may be confused by users with 1 and 0.

Why does it matter? It many cases having monotonic id may be quite convenient e.g. for primary keys, ordering. So if you don’t need creation time you may just relay on sorting by id. Checksum is useful if those ids are used by users and they copy paste them, or create tickets for a support based on those id. Generally whenever human mistake maybe an issue.

You may customize IdPrettifier to fit your needs by:

defining the decimal part length (letter part length depends partsSize and on the alphabet you use),

defining what letters you may use in the letter part (we call it alphabet),

specify the separator

decide if you want to have id with fixed length or not? (by adding leading zeros).

The underlying Long value (we call it seed) may be extracted from the generated id. It might be a good idea to keep the seed id in DB and convert if back and forth in DAO layer.

Performance

Before you will start using IdPrettifier consider if you really need additional complexity brought by it? Maybe using Long id would be enough?

If you still need its features check if overhead on calculating it (sometimes back and forth) will not impact performance of your application. I didn’t make extensive performance tests but I made a couple of test on my machine (with Intel i7–8700K CPU @ 3.70GHz). I compered IdPrettifier with UUID with java-uuid-generator with (I was generating time based UUID). Here are results:

100 - times

0.002 s - IdPrettifier

0.003 s - UUID with java-uuid-generator 1,000 - times

0.013 s - IdPrettifier

0.003 s - UUID with java-uuid-generator 10,000 - times

0.091 s - IdPrettifier

0.004 s - UUID with java-uuid-generator 100,000 - times

0.731 s - IdPrettifier

0.022 s - UUID with java-uuid-generator 1,000,000 - times

6.617 s - IdPrettifier

0.051 s - UUID with java-uuid-generator

In the result UUID is clear winner, just have in mind that serious performance differences are occurring for large numbers of Ids. So if id generation is not massive operation within your system or you don’t need to worry about milliseconds too much, it should not make a difference.