Skip to main content

How to Create Shared Look-up tables for microservices

 

Introduction

MySQL is well known for Enum data type with some predefined values. If we try to insert or update any custom values, we will get exception. Coming to MSSQL we will not have any equivalent data type for MySQL Enum. In monolithic architecture-based applications, it is fine to have code based Enums inside the applications and store their values in database TINYINT data type fields since we will have business layer as intermediate between presentation layer and database layer. We are in new era of microservice architecture-based applications. It is not good to have Enums inside the code base since single application may have fine grained services and each service will have their own domain layer. In such cases we may need to duplicate the code based Enum inside service which may bring maintenance nightmare. For this problem we will introduce the look-up tables as solution which will act like Enum, and it will have its own key-value pair records.

Sample Use Case

Let us consider my online store is an application which is built on the top of microservice based architecture. Consider we will have

  1. User management service
  2.  Item Management service
  3. Stock Management service
  4. Order Management service
  5. Notification Service 
Let us assume the sample use case like if user ordered an item from my online store, they should get notification via Push, email, and SMS. They should be notified in following occasions

  1. Item Ordered
  2. Item Shipped
  3. Item Delivered
  4. Item Canceled
  5. Item Rejected

Enum Structure

Enum has read only key value pair structure and for this use case we will have Enum structure like


Enum Based Approach

Since we need to notify user about an ordered item. We need to place OrderStatusEnum, mostly in all microservices like

  1. Item Management service
  2. Stock Management service
  3. Order Management service
  4.  Notification Service

Drawback in Enum based approach

  1.  Code is repeated in all services
  2.  Violating microservice for independent development and deployment. Because every team should need to have awareness about order status Enum.


Another Problem

We have some drawback in above mentioned Enum approach, so if we planned to place Enum in database level. We will again have data store for each microservices and, we don’t have any data type to store Enum in MSSQL.


Look-up Based Approach-Solution

“For every design problem, we should have a solution”

If we think in that way, we can have shared data store in our architecture and we should provision all service to access this store to get the Enum values. In this way, we can centralize the common logic, and this will be available to all services.


Look-up Table structure

Our Look up table should be in key-value pair pattern

Conclusion

Look-up tables are read-only tables, but we can seed additional data if needed, with new unique key. In such way we don’t have to work on any specific service and the shared data will be propagated all over microservices. The data which I mentioned is up to my knowledge, though there may be better solution than that. Please share If you any feedback on my thought/solution.










Comments

Popular posts from this blog

How to resolve ASP.NET core web API 2 mins timeout issue

Introduction We are in the new world of microservices and cross-platform applications which will be supported for multiple platforms and multiple heterogeneous teams can work on the same application. I like ASP.NET Core by the way its groomed to support modern architecture and adhere to the software principles. I am a big fan of dot net and now I become the craziest fan after seeing the sophisticated facility by dot net core to support infrastructure level where we can easily perform vertical and horizontal scaling. It very important design aspect is to keep things simple and short and by the way, RESTFul applications are build and it is a powerful mantra for REST-based application and frameworks. Some times we need to overrule some principles and order to handle some situations. I would like to share my situation of handling HTTP long polling to resolve the ASP.Net core 2 mins issue. What is HTTP Long polling? In the RESTFul term, when a client asks for a query from the serv

How to Resolve ASP.NET Core Key Protection Ring Problem in AWS Lambda

Introduction When it comes to server less web application design using asp.net core razor pages, we definitely need to consider a factor of data protection key management and its lifetime in asp.net core. I developed a site using AWS toolkit of ASP.NET Core Razor Pages. The main advantage of ASP.NET Core is cross-platform from where we can deploy our application in MAC, Linux or windows. I deployed my site initially in IIS Server from which I got the results as expected .but later period I decided to host my site in AWS Lambda in order to meet our client requirement. Strangely, I got unexpected behavior from my site. I just refer the cloud information Lambda Log to identify or pinpoint the case, I got the error Information like “Error Unprotecting the session cookie” from the log. In this article, I tried to explain the root cause of the problem and its solution to overcome such kind of issue. Data Protection in ASP.NET Core This is feature in ASP.NET Core which acts as repl

Which linq method performs better: Where(expression).FirstorDefault() vs .FirstOrDefault(expression)

 Introduction When it comes to LINQ, we always have multiple options to execute the query for the same scenario. Choosing correct one is always challenging aspect and debatable one. In one of our previous articles   Any Vs Count  , we have done performance testing about best LINQ methods over .NET types. In this article, I would like to share about  Where(expression).FirstorDefault() vs .FirstOrDefault(expression) Approaches Performance testing for  Where(expression).FirstorDefault() vs .FirstOrDefault(expression) is very interesting IEnumerable<T> or ICollcetion<T>  .FirstOrDefault(expression) is better than  Where(expression).FirstorDefault() Public API To check the performance, I need some amount of data which should already available. So I decided to choose this  public api . Thanks to publicapis Public API Models Entry class using System ; using System.Collections.Generic ; using System.Text ;   namespace AnyVsCount { public class Entry { pub