ballerinaballerina-http

How does Ballerina HTTP RetryConfig works?


I went through the Ballerina documentation and found the following document on the http:RetryConfig.

It has a statusCode field, which I need a further clarification.

    statusCodes int[](default []) - HTTP response status codes which are considered as failures

Is it essential to specify the status codes to consider it a failure? What happens if we do not provide the status codes in this configuration?


Solution

  • First of all, it is important to understand the types of errors the client might encounter and how retries are managed. There are two main types of errors:

    1. Network level errors: These include connection errors, timeout errors, etc.
    2. Application level errors: These are errors returned as HTTP responses with specific status codes.

    If the statusCodes field is not configured in the http:RetryConfig(the default behaviour), the client will only retry in the event of network level errors.

    If you want the client to also retry on specific HTTP status codes(indicating application level errors), you need to explicitly specify these status codes in the statusCodes field. For example, a common use case is to retry on a 503 - Service Unavailable status.

    Here is an example configuration:

    import ballerina/http;
    
    http:Client clientEP = check new ("http://localhost:9090", 
        retryConfig = {
            interval: 3,
            count: 3,
            backOffFactor: 2.0,
            maxWaitInterval: 20,
            statusCodes: [503]  // Retry on HTTP 503 Service Unavailable
        }
    );