Microservices – Client Resiliency Patterns

Before knowing about Client Resiliency Patterns, we should understand the terminologies.

What is Fault Tolerance and Fault Resilience?
Fault Tolerant means the ability to survive (tolerate) when an environment misbehaves by taking corrective actions.
Fault Resilience is the ability to recover quickly from difficulties.

In the world of Microservices, The terms Fault Resilience and Fault Tolerance are used interchangeably.

When we deploy Microservices, it is very common for the services to fail but failure is not a problem. Our target should be how quickly our system can recover from it. Avoid cascading failures and other microservices should not be affected from the failure of a particular microservice.

How fault tolerance works?
Discovery client keeps sending the heart beats to discovery server by saying that I am still alive.

OK! Let’s discuss about the issues we can encounter while working with Microservices:
1. Instance can go down – When we deploy single instance of microservice it can go down and we can face complete communication failure.
To resolve this better approach is to deploy multiple instances so if any one goes down request can be handled by others based on Load balancing.

2. Instance can slow down – If microservice instance slows down it results in slowing down the application performance. Let’s understand how it works.

Let’s say you have created 3 microservices named M1, M2 and M3
M1 communicates with M2 and M3 to get data, consolidates the data and sends the response back to client. M2 calls an external service to get some data which it depends upon.
Scenario 1: If external service goes slow, then M2 will be slow hence M1 will be slow.
Scenario 2: Even if M3 has nothing to do with M2 but since M1 is dependent on M2 and M3, so M3 will be slow until we get the result from M2.
Scenario 3: Let’s say we have another microservice called M4, which is just calling M3 and nothing to with M2, it still might be slow because the way threads work in web server.

One of the approach to solve this problem is by setting the timeout so that if one microservice doesn’t respond in time limit we can take appropriate action.

Setting timeouts on Spring RestTemplate
Method 1:

public RestTemplate getRestTemplate() {
	HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
	return new RestTemplate(clientHttpRequestFactory);

But if we set just timeout then again we encounter with the same problem if the frequency of requests is faster than the timeout duration. To resolve this issue we have better approach called Circuit Breaker.

Author: Mahesh

Technical Lead with 10 plus years of experience in developing web applications using Java/J2EE and web technologies. Strong in design and integration problem solving skills. Ability to learn, unlearn and relearn with strong written and verbal communications.