Microservices are a software development technique that arranges an application as a collection of loosely coupled services. In a microservices architecture, services are  fine-grained and the protocols are lightweight.Decoupled services allow teams to iterate quickly and with minimal impact to the rest of the system.

Before discussing further,let's see the architecture that prevailed before microservices, i.e. the monolithic architecture.You can say that it is similar to a big container wherein all the software components of an application are assembled together.They are tightly coupled together.

Here are the challenges of monolithic architecture:

  • Inflexible - We can't build Monolithic applications using different technologies.
  • Unreliable - If even one feature of the system does not work, then the entire system does not work.
  • Unscalable - Applications cannot be scaled easily since each time the application needs to be updated, the complete system has to be rebuilt.
  • Blocks Continuous Development - Many features of an application cannot be built and deployed at the same time.
  • Slow Development - Development in monolithic applications takes a lot of time to be built since each and every feature has to be built one after the other.
  • Not Fit for Complex Applications -Components of complex applications are tightly coupled. Components are not reusable.

These are the main reasons that led to the evolution of microservices.

Microservice Architecture is an architectural style that structures an application as a collection of small autonomous services, modeled around a business domain.Now, let us understand more about microservices by looking at its architecture. Refer to the diagram below:

Microservice Architecture
  • Different clients from different devices try to use different services like search, build, configure, and other management capabilities.
  • All the services are separated based on their domains and functionalities and are further allotted to individual microservices.
  • These microservices have their own load balancer and execution environment to execute their functionalities, and at the same time, captures data in their own databases.
  • All the microservices communicate with each other through a stateless server which is either REST or a Message Bus.
  • Microservices know their path of communication with the help of Service Discovery and perform operational capabilities such as automation and monitoring.
  • All the functionalities performed by microservices are communicated to clients via an API Gateway.
  • All the internal points are connected from the API Gateway so anybody who connects to the API Gateway is automatically connected to the complete system.

Feature of Microservices

  • Decoupling - Services within a system are largely decoupled, so the application as a whole can be easily built, altered, and scaled.
  • Componentization - Microservices are treated as independent components that can be easily replaced and upgraded.
  • Autonomy - Developers and teams can work independently of each other, thus increasing speed.
  • Continous Delivery - Allows frequent releases of software through systematic automation of software creation, testing, and approval.
  • Responsibility - Microservices do not focus on applications as projects. Instead, they treat applications as products for which they are responsible.
  • Decentralized Governance - The focus is on using the right tool for the right job. That means there is no standardized pattern or any technology pattern. Developers have the freedom to choose the best useful tools to solve their problems.
  • Agility - Microservices support agile development. Any new feature can be quickly developed and discarded again.

Advantages of Microservices:

Microservice advantages
  • Independent Development - All microservices can be easily developed based on their individual functionality.
  • Independent Deployment - Based on their services, they can be individually deployed in any application.
  • Fault Isolation - Even if one service of the application does not work, the system still continues to function.
  • Mixed Technology Stack - Different languages and technologies can be used to build different services of the same application.
  • Scaling - Individual components can scale as per need, there is no need to scale all components together.

Use case of a shopping cart application

Let's take a classic use case of a shopping cart application. When you open a shopping cart application, all you see is just a website. But, behind the scenes, the shopping cart application has a service for accepting payments, a service for customer services and so on.

Assume that developers of this application have created it in a monolithic framework. Refer to the diagram below:

Monolithic framework

So, all the features are put together in a single code base and are under a single underlying database.

Now, let's suppose that there is a new brand coming up in the market and developers want to put all the details of the upcoming brand in this application. Then, they not only have to rework the service for new labels, but they also have to reframe the complete system and deploy it accordingly.

To avoid such challenges developers of this application decided to shift their application from a monolithic architecture to microservices. Refer to the diagram below to understand the microservices architecture of shopping cart application:

Microservice Framework

This means that developers don't create a web microservice, a logic microservice, or a database microservice. Instead, they create separate microservices for search, recommendations, customer services and so on.

This type of architecture for the application not only helps the developers to overcome all the challenges faced with the previous architecture but also helps the shopping cart application to be built, deployed, and scale up easily.

Examples of Microservices

  • Netflix has a widespread architecture that has evolved from monolithic to SOA.  It receives more than one billion calls every day, from more than 800 different types of devices, to its streaming-video API.  Each API call then prompts around five additional calls to the backend service.
  • Amazon has also migrated to microservices.  They get countless calls from a variety of applications—including applications that manage the web service API as well as the website itself—which would have been simply impossible for their old, two-tiered architecture to handle.
  • The auction site eBay is yet another example that has gone through the same transition.  Their core application comprises several autonomous applications, with each one executing the business logic for different function areas.

Disadvantages of Microservices

  • Increasing number of services can result in information barriers.
  • The architecture brings additional complexity as the developers have to mitigate fault tolerance, network latency, and deal with a variety of message formats as well as load balancing.
  • Being a distributed system, it can result in duplication of effort.
  • When number of services increases, integration and managing whole products can become complicated.
  • When number of services increases, integration and managing whole products can become complicated.
  • Handling use cases that span more than one service without using distributed transactions is not only tough but also requires communication and cooperation between different teams.