Diving Into Reactive Microservices
What happens when reactive meets microservices?
Join the DZone community and get the full member experience.Join For Free
It would be beneficial if you understand the concepts of Reactive Architecture before diving into Reactive Microservices.
You can read about Reactive Architecture here:
In this article, we will cover transition from Monoliths to Service Oriented Architecture to Reactive Microservices by applying isolation techniques to the application.
What Are Monoliths?
To start explaining the microservices it’s useful to compare it to the monolithic application. An application is said to be a monolith when it is deployed as a single unit. Monoliths have a single shared database. They communicate with synchronous method calls where you send a message and expect a response immediately.
What Are the Cons of Monoliths?
- Monoliths are limited by the maximum size of a single physical machine. As the monolith grows, it acquires more and more system resources.
- They scale as far as the database allows because they often rely on a relational database which probably runs only a single instance in order to maintain consistency.
- Monolith components have to be scaled as a group, even if certain components of the application may require less resources.
- The deep coupling in monoliths leads to inflexibility, due to which development is typically slow.
- Serious failures in one component often bring down the whole monolith due to cascading failures.
What Is Service-Oriented Architecture?
Service-oriented Architecture is an architectural style that structures an application as a collection of smaller independent services. These microservices can be physically separated and independently deployed and scaled. Each microservice has its own logic and database as well as performs specific function. Communication between microservices can be synchronous or asynchronous, also the communication between these services is done through APIs only. Microservices undergo rapid deployments, it's not uncommon to see them getting deployed once a day or even multiple times a day.
What Are the Pros of Service-Oriented Architecture?
- Due to isolation and loose coupling between microservices, any change to the internals of one microservice does not necessitate a change to another microservice. Also, serious failure in one microservice does not cause cascading failures to other microservices.
- Since these services are accessible through external APIs only, so you're free to evolve the underlying code.
- Microservices also support multiple platforms and languages. It's very easy to build your application in any programming language and using any database.
What Are Reactive Microservices?
The core of Reactive Microservices is finding ways to create more isolation between microservices. Reactive microservices follow principles of isolation using various isolation techniques.
What Are the Principles of Isolation?
As we move from monoliths to microservices, more isolation is being introduced. Isolation provides reduced coupling and increased scalability.
Reactive Microservices are isolated in: State, Space, Time and Failure
Isolation of State
Reactive microservices are accessible through its API only, no backdoor access is provided through the database. It allows microservices to evolve internally without affecting outside.
Isolation in Space
Reactive microservices are independently deployed and do not care about the location of other microservices. The location of a microservice can be changed during deployment without an impact on other microservices. It allows microservices to be scaled up/down to meet demand.
Isolation in Time
Reactive microservices are asynchronous and non-blocking, due to which it leads to more efficient use of resources. Between microservices we expect eventual consistency, which provides increased scalability.
Isolation of Failure
Reactive microservices also isolate failures. A failure in one microservice does not cause another to fail. It allows the system to remain operational in spite of failure.
What Are the Isolation Techniques?
Bulkheading is a technique in which failure zones are created in the application to isolate failures, so that failure in one microservice is not propagated to other microservices and the overall system remains operational(possibly in a degraded state).
Circuit breaker is another technique to isolate failures. When a microservice calls another microservice which is overloaded and may fail. The caller microservice may not realize that the called microservice is under heavy load and it may retry, resulting more load on the called microservice. Caller microservice needs to be careful to avoid this. Circuit breakers are a way to avoid overloading a service by quarantining the failing service so it can fail fast and allowing the service to recover without overloading it.
Message Driven Architecture
Reactive microservices follow message driven architecture. They use asynchronous non-blocking messaging which allows isolation in both time and failure. It means that if a request to a microservice fails, the failure won’t propagate. Also microservices are not dependent on the response from each other, the client service can continue to operate without waiting for the response.
Reactive microservices are autonomous in nature, they can only guarantee their own behaviour through API calls. Isolation of microservices allow them to operate independent of each other. Autonomous microservices have enough information to resolve conflicts and repair failures. Fully autonomous service having no external dependencies, can be scaled indefinitely. Autonomy allows for stronger scalability and availability. Autonomy can be achieved using eventual consistency and by communicating only through asynchronous messaging.
API Gateway Service
Microservices can lead to complexities in the API. A single request may require information from multiple microservices. Clients could send multiple requests but then aggregating these results will be complex. To manage this complexity, API gateway service can be introduced to send requests to individual microservices and aggregate the response. Aggregation logic is moved from client to API gateway service. It handles failures from each service and client has to deal with only gateway service failure.
Opinions expressed by DZone contributors are their own.
How To Scan and Validate Image Uploads in Java
Integration Testing Tutorial: A Comprehensive Guide With Examples And Best Practices
Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL
Why You Should Consider Using React Router V6: An Overview of Changes