Microservices Design Patterns

Today there is a growing demand for microservices in the market. It has become a go-to solution to build APIs and application. Designing microservices may also have architectural challenges. To avoid any design based difficulty in future a developer must know some common microservices design patterns for building microservices.

What are MicroServices?

It is an implementation of a functionality in the form of service, which is self contained and implements a single business capability.

Principles Behind Microservices

You need to know on what principles microservices are designed and architectured.

  1. Resilient Services – one service goes down, entire application doesn’t go down.
  2. Scalability
  3. Independent and Autonomous Services
  4. Availability – services are available 24×7
  5. Decentralization – control is not at one place, its segregated
  6. Real Time Load Balancing –
  7. Seamless API Integration and continous monitoring
  8. Isolation from Failures – error in one service will not affect others

Design Patterns

1. Aggregator

Collects Related Items of Data and displays them Based on DRY principle.

An aggregator design pattern is design in which it invokes various other microservices which further has different data stores from which they fetch required data. Then show the aggregated data.

2. API Gateway

API Gateway acts as an entry point to forward the clients requests to appropriate microservices. Then, with the help of the load balancer, the load of the request is handled and the request is sent to the respective services.

3. Chained Or Chain of Responsibility

Produces a single output which is a combination of multiple chained outputs. Request passed to all the services and output is collected from all services.

4. Asynchronous Messaging

All the services can communicate with each other, but they do not have to communicate with each other sequentially.

5. Database Or Shared Data

DB can be per service Or shared database

6. Event Sourcing

Creates Events Regarding the changes in the application state.

7. Branch

Branch microservices design pattern is that in which you process the requests and response from two or more independent microservices.

8. CQRS ( Command Query Responsibility Segregator)

In this design pattern the application will be divided into 2 parts, Command and the Query.
The Command part will handle CRUD operations and the Query part will handle the materialized views.

9. Circuit Breaker

This design pattern is basically to handle fail scenarios of the microservices, for instance if a service it down, the system will still request and wait for response, but using circuit breaker pattern, after receiving consecutive failures in response, the system will trip and any further request made to this service will fail, this will save performance of the system.

10. Decomposition

It is always a good idea to break your application into small units. With the help of this pattern, either you can decompose an application based on business capability or on based on the sub-domains.
Vine Pattern or the Strangler Pattern further helps in decomposing big monolithic applications into small units.

Summary

In this article, we learnt about Microservices Design patterns for microservices development and its importance, we also read about principles behind microservices and had a look at various types of design patterns.

I hope you liked it !


Design Patterns for application development

Design patterns for application development are programming language independent strategies for solving a common problem. It means a design pattern represents an idea, not a particular implementation. By using design patterns you can make your code more flexible, maintainable and reusable.

The concept of design patterns in software development was originally presented in the 1994 book Design Patterns: Elements of Reusable Object-Oriented Software. The book was written by four authors who are now known collectively as the “Gang of Four”.

A design pattern provides a general reusable solution for the common problems occurs in software design.

It’s not mandatory to implement design patterns in your project always.

However, the lack of applying successful patterns will surely be visible as the effect of increased development time/effort/cost/bugs/rework and the inability to easily adapt to changing customer requirements.

So How do you find a right design pattern ?

To find out which pattern to use. You just have to try to understand the design patterns and it’s purposes. Only then you will be able to pick the right one.

Types of Design Patterns

Three major types of design patterns for application development.

1. Creational Patterns
2. Structural Patterns
3. Behavioral Patterns

1. Creational Design Patterns

Creational design pattern is about class instantiation or in other words object creation.

Further there are different types of creational design pattern:

  • Factory Method
  • Abstract Factory
  • Prototype
  • Singleton
  • Builder
  • Object Pool

2. Structural Design Patterns

This pattern is about organizing different classes and objects to form some other functionality. Structural class-creation patterns use inheritance to compose interfaces.

Some Structural design patterns are :

  • Adapter
  • Bridge
  • Composite
  • Decorator
  • Flyweight
  • Facade
  • Private Class Data
  • Proxy

3. Behavioral Design Patterns

These patterns are about identifying communication pattern between different objects and realize these patterns. These patterns are most specifically
concerned with communication between objects.

Some examples are :

  • Chain of responsibility
  • Command
  • Interpreter
  • Iterator
  • Mediator
  • Null Object
  • Observer
  • State
  • Strategy
  • Memento
  • Visitor
  • Template method

Five(5) Most important design patterns

1. Singleton

This pattern Ensure a class has only one instance, and provide a global point of access to it.

Scenario: Where application needs only one instance of an object. In addition to it, lazy initialization and global access are necessary.

There are several examples of singleton object where only a single instance of a class should exist such as caches, thread pools, db connection pools and registries.

How to make a class Singleton?

  1. Define a private static attribute in the “single instance” class.
  2. Define a public static method/function in the class – this is to be used by clients to access the object outside this class.
  3. Do “lazy initialization” of object (i.e create object on first use) in the accessor function.
  4. Define all constructors to be protected or private.

2. Factory Method

In Factory Pattern you can define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

The new operator is used to instantiate a class.

We should not prefer using new keyword to instantiate a class object every time as it also makes the code tightly coupled with that particular class.
To accomplish this, in Factory method objects are created by calling a factory method instead of calling a constructor.

3. Observer

This pattern defines a on-to-many dependency between object, such that when one object changes its state the other dependant objects are notified.

Model the independent functionality with a “subject” abstraction.

Model the dependent functionality with an “observer” hierarchy.

The Subject broadcasts events to all registered Observers.

The Subject may “push” information at the Observers, or, the Observers may “pull” the information they need from the Subject.

The pattern consists of two actors, the observer who is interested in the updates and the subject who generates the updates.

For Example: when you subscribe to a news feed from a social media website, whenever there is a new post the subscribers would see the new post.

4. Builder

As the name suggests the builder pattern is used to build objects.

Sometimes the object creation can get complex, one object can have multiple sub-objects, To separate the construction of a complex object from its representation so that the same construction process can create different representations.

The builder pattern might seem similar to the ‘abstract factory’ design pattern but one difference is that the builder pattern creates an object step by step whereas the abstract factory pattern returns the object in one go.

5. Adapter

This design pattern allows incompatible classes to work together by converting the interface of one class into another. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.

Adapter is meant to change the interface of an existing object.
Facade defines a new interface, whereas Adapter reuses an old interface. Remember that Adapter makes two existing interfaces work together as opposed to defining an entirely new one.

For example : If you have two applications, with one producing out output in XML format whereas the other requiring JSON as input, then you’ll need an adapter between the two to make them work seamlessly.

Summary

In this article, we learnt about Design patterns for application development and its importance, we also saw 3 different categories of design patterns and finally had a look at 5 basic and important design patterns.

I hope you liked it !