3 ms·
The blog mentions ORM, but it actually has nothing to do with orms, if you read it closer. 1. ID generation is generic problem. You can go two ways about it, o
by tehlike 6y ago
The blog mentions ORM, but it actually has nothing to do with orms, if you read it closer.
1. ID generation is generic problem. You can go two ways about it, one is letting your DB handle/generate it, and one handling it at application level. Your ORM can do it, or they may not. TypeORM/Sequelize are primitive ORMs compared to hibernate/nhibernate, but ORM could be a place where you can handle ID generation, or you can handle it in your business code.
2. .NET has nothing to do with it, if you want to extract information out of it, it applies to any language, concepts are programming concepts.
3. At the time i suggested HiLo > Comb > UUIDv4 and i actually gave my rationale. These days i choose sequential uuid/comb instead, mostly because i don't care about readable ids.
On HiLo:
If you want mostly sequential human readable numbers, instead of using DB generated identity, it's smarter to use a simple algorithm like HiLo algorithm. Hi/Lo is High + Lo referring to bucketization of the numbers. Hi goes from 0...infinity, Lo goes from 0 to bucketsize-1.
The basic idea is, if you have an application (or a distributed system for that matter), you preallocate id ranges to each node instance in your application.
Bucket size: 128
Instance 1: Hi=0, Lo=0
Instance 2: Hi=1, Lo=0
Instances assign ids based on the following formula: Hi * bucketSize + (Lo++);
This means instances can assign ids from 0...127 and and 128 ...255 on their own, without hitting database. When they run out of their range, they make a new query and increment Hi parameter stored in database.
Applications assigning ids without hitting database is important. Think of the following scenario:
If you have Order + Order Details, you can send all the inserts in one go if the order id is preassigned. Otherwise, you have to insert the order, get the id, then insert the order details with order id you just obtained.
On Comb creates less fragmentation in the database. I actually put a small bit of image there that simply shows what happens if you insert thousands of records with id=uuid4 vs comb. One of the tables will be more fragmented. the other will be significantly less..
UUIDv1 is structured as [low bits of timestamp, mid bits of timestamp, high bits of timestamp....] which means first bits of uuid moves faster than middle/higher bits, which is very problematic for the fragmentation.
Feel free to ping me on email if you want to talk more:
tehlike@gmail.com
I was a contributor on NHibernate around 2008.