Why Did We Shift Away From Database-Generated Ids?

So, every time we want to create a new record, the database engine automatically creates a primary key, and it’s its responsibility to ensure that this key is unique to the table.However, the assumption that the database technology can generate the identifiers for our domain aggregates again ties us with the data storage system..What if we want to change data storage system with a one which doesn’t have a feature of generating auto-increment primary keys?.We can’t..Also, each data storage system has different ways of generating these identifiers which may cause us to end up with a different primitive type..Besides this, these types of keys may not be suitable for a distributed system..For instance, when we have a table in our SQL Server database with generated primary keys, we don’t have an easy way to scale this table horizontally in a distributed environment.These concerns could be overcome by letting the unique aggregate identifier generation happen by the consumer of the domain layer (i.e. the transport layer which communicates with the domain layer through commands and queries)..This reduces the environment dependency which in turn lets us not to rely on the database for Id generation..This approach also has another benefit: it can support distribution..For instance, we can have a table partitioned onto two physical SQL Servers and split the cost of lookups..If we had an auto-increment key, this wouldn’t work with SQL Server for instance.What have we decided to do? ????Based on these facts, we decided to let the consumer of the domain layer to generate the identifiers for the domain aggregates..We are representing these identifiers as 64-bit unsigned integers within the domain layer..The domain consumer is free to decide what the representation of this should be based on their context (e.g. ASP.NET Core MVC can serialize the identifiers as string in order to make it easy for its clients to consume the resources objects, etc.).Why 64-bit integer and why not UUID?Lastly, you might be wondering why 64-bit integers.. More details

Leave a Reply