Tuesday, December 19, 2023

What Are Microservices: Pros, Cons, Use Cases, and More?

What are microservices?

Microservice is a small, loosely coupled distributed service. Microservice architecture evolved as a solution to the scalability, independently deployable, and innovation challenges with Monolithic architecture (Monolithic applications are typically huge – more than 100,000 lines of code). It allows you to take a large application and decompose or break it into easily manageable small components with narrowly defined responsibilities. It is considered the building block of modern applications. Microservices can be written in a variety of programming languages, and frameworks, and each service acts as a mini-application on its own. Microservice can be considered as the subset of SOA(Service Oriented Architecture). 

In short, microservices are a collection of services that are:

  • Loosely coupled
  • Highly maintainable and testable
  • Organized around business capabilities
  • Independently deployable
  • Owned by a small team

 Characteristics of microservices

  • Each service has a separate database layer, independent codebase, and CI/CD tooling sets.
  • Services are responsible for preserving their data or external state.
  • Every service is independently deployable and can be tested in isolation without depending on other services.
  • Internal communication between the services happens via well-defined APIs or any lightweight communication protocol.
  • Each service can select the technology stack, libraries, and frameworks best suited for its use cases.
  • Services should implement Retry functionality if there is a network or system failure

 Comparison of Microservices with Monolithic and SOA architecture

Three popular architectures — monolithic, service-oriented architecture, and microservices are mostly used to build a new application.

Monolithic architecture – A monolithic application is developed as a single unit, where all features, functionality, and modules are tightly coupled for development, integration, and deployment.

Microservices vs monolithic

 

Microservices

Monolithic

Design

Independent services communicate via APIs

Complex app encompasses several tightly-coupled functions

Data consistency

Difficult to achieve

Easy to achieve

Scalability

Independently scalable

Challenging

Agility

High operational agility

Difficult to achieve operational agility

Service startup 

Relatively quick

Takes more time

Finding failure points

Difficult

Easy

Team communication

More complex

Comparatively straightforward

Service-oriented architecture – Created as a response to the traditional architectural approach, SOA helps decouple an overall application into smaller modules. These separate service modules intercommunicate to meet particular business requirements.

Often, many developers consider microservices as a subtype of SOA, while others insist each solves a different set of problems.

Microservices vs SOA


Microservices

SOA

Design

Services are built in small units

Services can vary in size from small to large

Scalability

Independently scalable

A bit challenging due to dependencies between services & reusable sub-components

Component sharing

Does not involve component sharing generally

Often involves component sharing

Data storage

Each service can have an independent data storage

Involves sharing data storage between services

Remote services

Uses REST and JMS

Uses protocols like SOAP and AMQP

Deployment

Quick and easy

Less flexible


What are the benefits of microservices?

  • Agility – With microservices, small DevOps teams can work independently and act within a well-defined context, for faster development and increasing throughput.
  • Increased resilience and fault tolerance – When appropriately constructed, independent services do not impact one another. Service independence ensures that the failure of a specific service does not crash the enterprise application.
  • Higher-quality end product – Modularisation of an application into discrete components helps app development teams concentrate on a tiny part at a time. This approach simplifies the overall coding and testing process and increases software quality.
  • Real-time processing – A publish-subscribe framework of microservices enables data center processing in real-time. As a result, extensible systems can consume and process large amounts of events or information in real time.
  • Data isolation – Unlike a monolithic application, where different parts of the application might touch the same data, here, only a single microservice is affected. And so, it is much easier to perform schema updates.

 What are the challenges of microservices?

  • Data consistency – Microservice has a distributed system approach to managing data, and if there is redundancy across the data stores, the same item of data appears in multiple places. Duplicated or partitioned data raise the issues of data integrity and consistency.
  • Distributed tracing – It’s challenging to find the failure point, debug, and fix sources of errors with microservices. Without a comprehensive full stack trace, you have to work backward and go through status codes and vague error messages.
  • Network congestion and latency – The use of multiple small, granular services will require more interservice communication. Besides, there can be the issue of additional latency if the chain of service dependencies becomes too long.
  • Operation overhead – Managing numerous independent services can often become a tedious task for the operation team. They need to put in significant effort to avoid any failures and ensure the resilience of the developed application.
  • Testing – Microservices testing is more complicated because of the complex integration, different services, and interdependencies. The testing team has to write many mock services to test even small units and should know various services and channels of communications to have full coverage in their test cases.
  • Technical debt – A polyglot programming environment can often be a double-edged sword! While it allows architects and developers to build a microservices structure using individual infrastructures and different languages, the organization might end up with an enormous system having a thousand ways to deal with every single application. 

Micorservices development technologies and tools

As microservice is an architectural style, a single tool might not be sufficient to control the entire workflow. An organization should start by understanding the challenges of the existing tech stack and accordingly select a tool based on its functionality, license, benefits, and challenges.

Here are some of the development tools that help increase the developers’ productivity and play an essential role in microservice development.

  1. Containerization platforms – Microservices become independently scalable and deployable by using isolated workload environments that containerization platforms provide. For example, a popular platform, Docker, enables you to containerize your microservices and simplify delivery and management.
  2. Container orchestration platforms –  Kubernetes (K8s) works wonders for automating the scaling, management, and deployment of microservice applications. This renowned framework allows you to manage hundreds or thousands of containers at a production scale.
  3. API management tools – API gateways keep a single service lightweight by handling all requests from the clients and routing them to the appropriate microservice. They also help decrease microservices complexity, avoid exposing internal concerns to external clients and provide security benefits.
  4. Messaging tools – REST/HTTP works well for synchronous request/reply patterns and public-facing APIs. However, it’s not suited for event-driven microservices. A message queue consists of messages sent between small independent services/applications, and it decouples heavyweight processing and smooth spiky workloads. Standard messaging tools are RabbitMQ and Kafka.
  5. Data stores – The data store is used to preserve data needed by the microservices. Renowned session data stores are in-memory caches like Redis or  Memcached.
  6. Service mesh – You can deploy microservices without extensive knowledge of the underlying infrastructure by using a service mesh like Istio, which is quite popular in the Kubernetes community. It also provides additional capabilities like load balancing, intelligent routing, service discovery, policy enforcement, and more for your microservice architecture.

 Common misconceptions about microservices

  • “Micro” means small – A microservice is comparatively “small” than the enormous monoliths. However, making it too small would be a mistake. The size of microservices generally depends on the application.
  • Microservices entirely eliminate complexity – Microservices shift complexity from code design and implementation into system operations where we can automate things.
  • Microservice can “easily” scale systems – Yes, scaling is undoubtedly an advantage here, but it’s not as easy as it sounds. Upscaling can involve managing several services simultaneously, and you have to ensure that the upscaled components integrate with the rest of the system.
  • Microservices work for all applications – One must evaluate systems and have long-term strategies before opting for microservices. Every application in an enterprise cannot be a good fit for a microservice.

Real-world examples of microservices

Microservices can be beneficial for rapidly growing industries like an online marketplace, eCommerce, social media networks, finance, healthcare, etc. Some of the practical use cases for a microservice architecture include –

  • Adding new desired functionality to a large legacy system
  • Providing the scalability and control to handle big data
  • Enabling advanced analytics environments to validate their updated computational models using AB and multivariate testing
  • Collecting, aggregating, and analyzing a data flow for machine learning environments

Organizations experienced a massive change while using microservice in their application, and that’s where the transition from monolithic to microservice came. You can go through some of the real-life examples in applications that use microservice are:

  • Amazon – Initially, Amazon was a monolithic application but when microservice came into existence, Amazon was the first platform to break its application into small components, thereby adapting microservice. Due to its ability to change individual features and resources, the site’s functionality improved to a massive extent. 
  • Netflix – Netflix is one such company that uses microservices with APIs. In 2007, when Netflix started its move towards movie-streaming service, it suffered huge service outages and challenges, then came the microservice architecture which was a blessing to the platform. 
  • Uber – When Uber switched from monolithic nature to a microservice, it experienced a smooth way. Using microservice architecture, the webpage views and searches increased to a greater extent. 

Microservice Pros and Cons

As every coin has two sides, each system has pros and cons as well. Microservices too have some pros (benefits) and cons as well. Let’s have a look at them:

Pros

  • Can independently develop and deploy services
  • Dynamically scalable and quickly functioning
  • Integration with third-party dependencies
  • Has an independent manageable deployment module
  • Different services may use different languages

Cons

  • Multiple services mean multiple resources (difficult to handle)
  • Different services using different languages makes testing difficult
  • Debugging issues
  • Increase in effort while handling it
  • Challenges in deployment

  • Communication between services isn’t easy.

Microservice Frameworks for Java

There are several microservices frameworks that you can use for developing Java. Some of these are:

  1. Spring Boot – This is probably the best Java microservices framework that works on top of languages for Inversion of Control, Aspect-Oriented Programming, and others.
  1. Dropwizard – Dropwizard pulls together stable, mature libraries from the Java ecosystem into a simple, lightweight package that lets you focus on getting things done.
  1. Restlet – Restlet Framework helps Java developers build better web APIs that follow the REST architecture style.
  1. Spark – A micro-framework for creating web applications in Kotlin and Java 8 with minimal effort.

 

No comments:

Post a Comment