Sobes.tech
Senior

Resilience patterns under unstable network conditions to external vendors: retry, circuit breaker.

sobes.tech AI

Answer from AI

In case of an unstable network, resilience patterns such as retry and circuit breaker are applied to external vendors to improve reliability and avoid excessive load.

Retry — an attempt to perform a request again after temporary errors (e.g., timeouts or network failures). It is important to implement exponential backoff between retries and limit the number of retries to prevent additional load.

Circuit breaker — a mechanism that "breaks the chain" of calls to an external service upon detecting repeated errors. It prevents constant attempts to access an unavailable service, giving it time to recover. The circuit breaker typically has three states:

  • Closed: requests pass through.
  • Open: requests are blocked, and an error is returned immediately.
  • Half-Open: periodic requests are attempted to check availability.

Example in Go using the github.com/sony/gobreaker library:

import (
    "fmt"
    "net/http"
    "time"
    "github.com/sony/gobreaker"
)

func main() {
    cb := gobreaker.NewCircuitBreaker(gobreaker.Settings{
        Name:        "VendorAPI",
        MaxRequests: 3,
        Interval:    60 * time.Second,
        Timeout:     30 * time.Second,
        ReadyToTrip: func(counts gobreaker.Counts) bool {
            return counts.ConsecutiveFailures > 5
        },
    })

    body, err := cb.Execute(func() (interface{}, error) {
        resp, err := http.Get("https://vendor.api/resource")
        if err != nil {
            return nil, err
        }
        defer resp.Body.Close()
        if resp.StatusCode >= 500 {
            return nil, fmt.Errorf("server error %d", resp.StatusCode)
        }
        return resp.Body, nil
    })

    if err != nil {
        fmt.Println("Request failed:", err)
    } else {
        fmt.Println("Request succeeded", body)
    }
}

Thus, retry helps handle temporary failures, while the circuit breaker prevents excessive requests to an unavailable service.

Resilience patterns under unstable network conditions… - sobes.tech