DEV Community

Cover image for From monolith to cloud: Auto Increment to UUID

From monolith to cloud: Auto Increment to UUID

Adrian B.G. on November 20, 2018

This article was originally published on my website https://coder.today/software-engineer-from-monolith-to-cloud-auto-increment-to-uuid-a62f92f387...
Collapse
 
rhymes profile image
rhymes

You will have to get used to a more difficult debugging process, UUID’s are impossible to remember. The trick of memorizing the first or last characters will probably not work.

I feel KSUIDs are a good compromise. You have secure IDs like with UUIDs but they are roughly sortable (and you can memorize a bit): github.com/segmentio/ksuid

Collapse
 
bgadrian profile image
Adrian B.G.

Wow nice, my brain just got bigger.

Also I found this on the ksuid repo segment.com/blog/a-brief-history-o... which is a good extra for my article.

KSUID look nice, and knowing what Segment.io does I see their need for such a thing.

KSUIDs are 20-bytes: a 32-bit unsigned integer UTC timestamp and a 128-bit randomly generated payload. The timestamp uses big-endian encoding, to allow lexicographic sorting. The timestamp epoch is adjusted to March 5th, 2014, providing over 100 years of useful life starting at UNIX epoch + 14e8. The payload uses a cryptographically-strong pseudorandom number generator.

It is like a json web token with a timestamp and crypto-randomness, sounds like a cookie treat.

Collapse
 
biros profile image
Boris Jamot ✊ /

I just switched from a PHP/MySQL app with auto-incremented ID to a Go/Mongodb app with UUID.
The main drawback I noticed is that you can't use internal mongodb document's ObjectId for your queries. It makes your code a bit more complex:

With auto-increment:

query := collection.FindId(1)

With UUID:

query := collection.Find(bson.M{"id": "123e4567-e89b-12d3-a456-426655440000"})

And it's the same for update, remove & upsert.

Collapse
 
bgadrian profile image
Adrian B.G. • Edited

Yes because ObjectID is a BSON object, and adds more text along the way.

You can use other types, like a string with a UUID, but you have to provide the _id at insert and you have to be sure they are unique. MongoDB will add an _id only if is missing and it will be an ObjectId.

Collapse
 
rafalpienkowski profile image
Rafal Pienkowski

There is one point here:

"Entries in a relational database like MySql/SQL/Oracle are usually identified by an incremental, unique (to table) number int(2232). The server collects the parameters, sends an INSERT(...) statement and the database generates a new ID (the next incremental value) and returns it."

Unfortunately, Oracle (unlike MS SQL, MySql) doesn't support an auto-increment data type. You have to create a combination of sequence and trigger for the PR key column. If I good remember Oracle decided to resign from that type of data structure due to performance reasons.

Collapse
 
bgadrian profile image
Adrian B.G.

They got that right, auto increment is a bottle neck in a heavy-write app. But still is funny to see the words Oracle and Performance in the same sentence.