MicroService Design — Part 1

Ritik Jain
Crux Intelligence
Published in
5 min readNov 9, 2021
Photo by Kaleidico on Unsplash

Recently microservices gain insane popularity in latest technology stacks. A microservice is an architectural structure that allows rapid, frequent, and reliable delivery of complex applications. It provides high maintainability and testability, loose component coupling (low interdependence), independent deployments and can be managed by small teams.

In this article, I will discuss the best practices for designing microservices. Before designing a particular microservice, we should answer some questions like:

  • What design patterns can be used in the service?
  • What should be the structure of a service?
  • What technology stack should be used?

I will address these questions in this article. So let’s get started.

What should be the service structure?
The service structure refers to the project structure. Industry uses a diverse range of project structures, i’ll discuss some of the most common project structures.

  • Simple Structure (MVC Structure)

Simple structure also known as Model-View-Controller Framework, where the project structure divided into Models, Views and controllers. Model refers to the Database interface (CRUD Application). View refers to application interfaces (API Endpoints), Controller refers to application business logic.
This structure is well suited for simple application without includes multiple functionalities.

Advantages:

  • Segregation at functionality level
  • High Code reusability and readability

Disadvantages:

  • Not scalable
  • No testability and validation
  • Complex maintainability and navigation for large application
  • Divisional Structure

Divisional structures provide the application segregation at business objectives, whereas the simple structure lacks with maintainability of complex application. The divisional structure performs well in terms of navigation and maintenance of applications.

Advantages:

  • Segregation at objective level
  • High code readability and reusability
  • Centralisation and Decentralisation of codebase
  • Individual contribution for individual modules

Disadvantages:

  • Lack of Testability
  • Complex code validation and review

Test driven Division Structure

This structure is similar to Divisional Structure. It is being added with the Testability and code validation.
We can use test-driven divisional structure for building and maintaining complex application with an ease of continuous validation of the modules.

Advantages

  • Similar to Divisional Structure
  • Added Code validation and review

Disadvantages

  • High dependency on Test Coverage
  • Difficult learning curve

Answer — Service Structure can be defined based on application complexity and ease of team. For simple application we can directly use MVC structure, whereas complex application requires a divisional structure.

What design patterns can be used in the service?

A design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern isn’t a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.
By — Source Maker

Source: Jobsity

The patterns describe the solution to the problem, when and where to apply the solution and the consequences of the implementation. Broadly design patterns categorised into

Creational Patterns
Creational design patterns provide a way to create objects while hiding the creation logic, rather than instantiating objects directly using new operator. This gives program more flexibility in deciding which objects need to be created for a given use case. These patterns helps with hiding the main business logic under the abstract umbrella. Creational Patterns like

  • Factory Pattern
  • Builder Pattern
  • Prototype
  • Singleton Pattern
  • Object Pool Pattern

Creational patterns provide a common interface for different objects. As shown below, Based on different parameters, the pattern define the functionality.

Source: Refactoring Guru

Structural Patterns
Structural patterns are considering composition of classes and objects in order to achieve new functionality. These patterns uses the concept of Inheritance for composition. Structural Patterns like

  • Composition
  • Adapter
  • Decorator
  • Bridge
  • Facade
  • Proxy

Structural patterns generally divides the whole complex task into small and simple tasks which later compound and solve the main objective. As shown below

Composite Pattern by Refactoring Guru

Behavioural Patterns

Behavioural design patterns are mainly concerned about the communication between different objects. These patterns can be used where the behaviour or state is known and recurring. Patterns like

  • Template
  • Chain of responsibility
  • Observer
  • Visitors
  • Null Object

Here is a mediator pattern, where multiple objects can communicate with each other. Sharing of different data can be possible using simple classes.

Source: Refactoring Guru

Answer — Choosing the right design pattern for a module is essential. We can have various modules within one application, all with different functionality, so we must choose the right pattern.

What technology stack need to be used for microservice?

Microservice architecture is technology stack agnostic, where we can use different technology based on service requirement. Technology stack should align with business objective.

If business objective requires ML capabilities then we can create the service using Python Stack which includes

API Endpoint:- FastAPI, Flask
Framework:- Tensorflow, PyTorch, Scikit-Learn

For business processes involving a lot of event-driven processes, like in Blockchain, Node.JS is a great choice.

Answer — Technology Stack will always be defined based on the business objective. Before selecting the technology stack, we need to understand the business requirement.

Conclusion

The article have addressed three major questions generally asked before developing any service in microservice environment.

  • Service structure can be defined based on application complexity and ease of team
  • Choosing the right design pattern for a module is essential
  • Understanding the business requirements is essential before answering the technology stack.

--

--

Ritik Jain
Crux Intelligence

Fallen for data and understand the problems which can be resolve. Passionate for ML and MLOps.