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 familiar concept used to correct behavior in children, but its application extends beyond that. In the realm of microservices, where monolithic applications are transitioning to a distributed architecture, the occasional misbehavior or failure of a microservice is inevitable.

When a microservice fails or becomes unresponsive, it can have a cascading effect, impacting other services and potentially rendering parts of the application inaccessible or even causing a complete system outage. To mitigate such situations, implementing a time-out mechanism for faulty microservices becomes crucial.

Just as we use circuit breakers to protect electrical appliances by interrupting power in the event of a fault, software engineers at Netflix drew inspiration from this concept. They developed a library called Hystrix, which applies the circuit breaker design pattern.

Hystrix monitors method calls for failures. Once failures surpass a predefined threshold, it stops invoking the faulty method, allowing it time to recover. In the meantime, Hystrix employs an alternative fallback method to handle requests and provide a response. Once the faulty method has fully recovered, Hystrix resumes calling it as usual.

By incorporating circuit breaker functionality into microservice architectures, Hystrix helps maintain system stability and resilience, safeguarding the overall application from the impact of individual service failures.

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.