To ORM or not to ORM
Object Relational Mapping (ORM) is a layer that sits between a service and the database used by the service. ORM follows its own dialect for operations – both DDL (Data Definition Language) as well as DML (Data Manipulation Language).?
DDLs are usually managed and referenced by the term migrations in context of an ORM. However, using an ORM requires certain design patterns to be followed across services in an organization, to avoid boilerplate of model definitions and ease of version control. At Agridence, when we segregated or developed a service, oftentimes, the models used were same, which caused us to emphasize and re-think our ORM strategy for a while. This article highlights some of the methods followed, their outcomes, and scope for future development.
Before we dive into detail, here are some highlights on the pros and cons of using an ORM from our use cases.?
Pros?
Cons?
?
Next, we highlight how we dealt with the above challenges and various scenarios encountered.?
Solution 1 – Repository controlled?
If both services are part of same source-controlled repository, models can be described at the top layer allowing both programs to inherit these definitions. This solution works well for coupled services such as including those including but not limited to sidecar patterns.?
Solution 2 – Decouple using an SDK pattern
Define models at a common place and import them as a library. This solution requires careful segregation of business logic and definition files. In this strategy, no service specific business logic should be embedded as the goal is to decouple model definitions from their behaviors. Depending on the programming language used and level of abstraction required when sharing implementations, this distribution can be an easy or slightly complex task to maintain.?
Solution 3 – Service responsibility segregation?
As much as possible, each service must own its own database and other services must interact with the master service to perform data operations instead of using an ORM to directly handle data. This segregation of responsibility results in cleaner code and data ownership. However proper versioning must be implemented by master service so that any changes do not cause immediate disruption in dependent nodes.?
领英推荐
2. Migrations management?
Solution – Remove unwanted migration files actively?
Migration files, if not managed carefully, can end up containing a changelog of entire history for how a codebase performed DDL changes to the underlying tables. While migrations in ORM are a standard and best-practice, one must carefully audit periodically to remove or combine ad-hoc and obsolete migrations.?
3. Schema design?
Solution – Documentation?
Documentation of schema chosen, and their relationships (entity relationship diagrams) are critical for not only explaining design choices to other team members but also helpful for collaboration and improvements. Any changes to this state must be updated to maintain correctness.?
4. Use of ORM?
Solution – Use case driven choice?
No-SQL based applications mostly utilize language specific SDKs and query methodologies without requiring an ORM. In such cases, point 3 above pertaining to documentation and strict version and access control becomes important. For example, consider a No-SQL scenario where incorrect schema management changed datatype of an integer to string, leading to multiple cascading failures.?
Some cloud-based databases offer flexibility of maintaining table schema and their versions on the cloud. This is particularly important for ETL, ELT and streaming data processing. In such cases, the use of an ORM is discretionary as the underlying data-tables may not necessarily be row-oriented.?
5. Understand the database type used?
Solution – Know your database?
Consider a scenario where the database is column-oriented and not row-oriented (for example Postgres). While column-oriented tables are optimized for columnar data access patterns, the same may take longer run durations and resources in row-oriented tables. Knowledge of underlying database (when using an ORM) helps developers in avoiding some common pitfalls associated with data query and processing.??
Investor in Sustainability and Impact Businesses
1 年Tech is our blood, we pride ourselves to be at the cutting edge of tech innovation. #future