Posts Why should we give time-out to a microservice
Post
Cancel

Why should we give time-out to a microservice

Time-out is a way of correcting behavior by placing a misbehaving child in a quiet place alone for a few minutes. Time-out is a chance to calm down.

Misbehaving is not just limited to a child, Once in a while, even a microservice misbehaves, I mean even a microservice fails or doesn’t respond at all. We are witnessing more monolithic apps are evolving into microservices, given the distributed nature of the microservices failure of any given service is inevitable.

If a service fails it may impact other services affecting performance and possibly making other parts of the application inaccessible or in the worst case bring down the whole application. So, we should give time-out to a faulty microservice as a damage control technique.

Time-out is not just a device to discipline kids, We also use it to protect electric appliances, we call it there as a circuit breaker. Its basic function is to interrupt the power supply after a fault is detected.

I believe software engineers at Netflix have drawn some inspiration from electrical engineers and developed a library that mimics circuit breaker functionality, the library is called Hystrix, and The principle on which it is developed is called circuit breaker design pattern.

The Hystrix monitors for failures in a method call. Once the failures reach a certain threshold, it stops calling the faulty method giving it time to recover. During this time it uses an alternative fallback method to fulfill the response. Once the method completely recovers, it starts calling it as usual.

In the following ultra simple example we used HystrixCommand on the line# 21 is to keep monitoring the getOrders() method, if something goes wrong in the method use the getOrdersFallback() instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@Service
public class CustomerServiceImpl implements CustomerService {

	@Autowired
	private OrderService orderService;

	@Override
	public Customer getCustomer(String CustId) {
		Customer cust = new Customer();
		cust.setId("123");
		cust.setFirstName("John");
		cust.setLastName("Smith");

		List<Order> orders = this.getOrders(cust);
		
		cust.setOrders(orders);
		return cust;
	}

	@HystrixCommand(fallbackMethod = "getOrdersFallback")
	public List<Order> getOrders(Customer cust) {

		return orderService.getOrders(cust);
	}

	
	public List<Order> getOrdersFallback(Customer cust) {

		return Collections.emptyList();
	}

}

After all, microservices are developers’ babies. We need to nurture them to be a good citizen of software systems. Go populate these ecosystems of microservices with your mostly well-behaved babies. You can have a look at the example code in my github repository.

P.S : If you are a parent and more interested in time-out for kids than for microservices, here you go Time-out for kids

This post is licensed under CC BY 4.0 by the author.