concept retrie in category cloud

appears as: retries, retries, The retries
Cloud Native Patterns: Designing change-tolerant software

This is an excerpt from Manning's book Cloud Native Patterns: Designing change-tolerant software.

The good news is that, as an industry, we’ve been working on solutions to these new problems for some time, and the patterns are fairly well established. When one component needs to communicate with another, it needs to know where to find that other component. When an app is horizontally scaled to hundreds of instances, you need a way to make a configuration change to all instances without requiring a massive, collective reboot. When an execution flow passes through a dozen microservices to fulfill a user request and it isn’t performing well, you need to find where in the elaborate network of apps the problem lies. You need to keep retries—a foundational pattern in cloud-native software architectures in which a client service repeats requests to a providing service when responses aren’t forthcoming—from DDoS[2]-ing your system as a whole.

Figure 9.2. Retries can insulate parts of your distributed system from errors in other parts.
Listing 9.1. Excerpt from ConnectionsPostsController.java
int retryCount = 0;
while (implementRetries || retryCount == 0) {
  try {
    RestTemplate restTemp = restTemplateBuilder
                              .setConnectTimeout(connectTimeout)
                              .setReadTimeout(readTimeout)
                              .build();
    ResponseEntity<PostResult[]> respPosts
      = restTemp.getForEntity(postsUrl + ids + secretQueryParam,
                              PostResult[].class);
    if (respPosts.getStatusCode().is5xxServerError()) {
      response.setStatus(500);
      return null;
    } else {
      logger.info(utils.ipTag() + "Retrieved results from database");
      PostResult[] posts = respPosts.getBody();
      for (int i = 0; i < posts.length; i++)
        postSummaries.add(
          new PostSummary(getUsersname(posts[i].getUserId()),
          posts[i].getTitle(), posts[i].getDate()));
      return postSummaries;
    }
  } catch (Exception e) {
    // Will occur when a connection times out.
    // For this naive implementation, we will simply
    // try again.
    logger.info(utils.ipTag() +
      "On (" + retryCount + ") request to unhealthy posts service  " +
      e.getMessage());
    if (implementRetries)
      retryCount++;
    else {
      logger.info(utils.ipTag() +
        "Not implementing retries - returning with a 500");
      response.setStatus(500);
      return null;
    }
  }
}


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":23}],[{\"line\":22,\"ch\":2},{\"line\":22,\"ch\":25}],[{\"line\":23,\"ch\":4},{\"line\":23,\"ch\":46}],[{\"line\":24,\"ch\":4},{\"line\":24,\"ch\":52}],[{\"line\":25,\"ch\":4},{\"line\":25,\"ch\":17}],[{\"line\":14,\"ch\":6},{\"line\":14,\"ch\":33}],[{\"line\":26,\"ch\":4},{\"line\":26,\"ch\":31}],[{\"line\":32,\"ch\":6},{\"line\":32,\"ch\":33}],[{\"line\":27,\"ch\":6},{\"line\":27,\"ch\":70}],[{\"line\":28,\"ch\":6},{\"line\":28,\"ch\":22}],[{\"line\":29,\"ch\":4},{\"line\":29,\"ch\":25}],[{\"line\":30,\"ch\":6},{\"line\":30,\"ch\":19}]]"}
!@%STYLE%@!

As you can see, the implementation is simple. If you’re implementing retries, which are controlled through a new application property, you’ll make a request to the Posts service. If it times out, you stay in the while loop and try again. You can see the lines that generate the very log messages you looked at when running the sample.

In the previous section, I presented a basic pattern: the Connections’ Posts service will retry calls to the Posts service in the event that it doesn’t initially receive a response. At a high level, this makes complete sense, and the current implementation will handle minor glitches just fine. But when something more significant occurs (the metaphorical highway accident), retries may not be as helpful and may even hurt the overall health of the system. What I want to do here is experiment with the naïve implementation that I presented in the previous section.

  • Retries are but one example of a control-loop pattern.