ryan961 / memo

https://github.com/ryan961/memo/issues
Creative Commons Zero v1.0 Universal
0 stars 0 forks source link

Go Retries: An interactive study of common retry methods #1

Open ryan961 opened 10 months ago

ryan961 commented 10 months ago

πŸ€– AI Summary

The article discusses the importance of handling network request's failures and focuses on the method of retrying failed requests. It visually demonstrates why some retry methods are inefficient and walks the readers through the best practices for retry behavior. The article emphasizes the dangers of implementing retries without any caps or controls, which might lead to system overload. It introduces the concept of 'Exponential Backoff,' a method to manage retry attempts in which the time delay between retries doubles after each attempt. This method is explained to be effective in dealing with system overloads and service crashes. The added aspect of 'Jitter' is presented towards the end, which randomizes the retry delays to prevent retries from overloading the systems at the same intervals, hence providing even better control over retries.

πŸ–‡οΈ Details

πŸ“ Note

ryan961 commented 10 months ago

πŸ“¦ Package

github.com/cenkalti/backoff/v4

πŸ€–οΈ AI Intro

This Go package provides a comprehensive way to handle backoff strategies for retry operations. When an operation fails due to transient issues, such as a temporary network problem or API rate limits, backoff helps to retry the operation after waiting for a progressively increasing amount of time, as described by an exponential backoff algorithm.

πŸ“ Example

package main

import (
    "errors"
    "fmt"
    "time"

    "github.com/cenkalti/backoff/v4"
)

// getExponentialBackoff returns a backoff.ExponentialBackOff with the following settings:
//   - Multiplier: 1.5
//   - InitialInterval: 500 * time.Millisecond
//   - MaxInterval: 10 * time.Second
//   - MaxElapsedTime: 60 * time.Second
func getExponentialBackoff() *backoff.ExponentialBackOff {
    exponentialBackoff := backoff.NewExponentialBackOff()
    exponentialBackoff.Multiplier = 1.5
    exponentialBackoff.InitialInterval = 500 * time.Millisecond
    exponentialBackoff.MaxInterval = 10 * time.Second
    exponentialBackoff.MaxElapsedTime = 60 * time.Second
    return exponentialBackoff
}

func main() {
        // ......
    err := backoff.Retry(func() error {
        err := func() err {}
        if err != nil {
            if !errors.Is(err, fmt.Errorf("some error")) {
                return backoff.Permanent(err) // direct return
            }
            return err // retry
        }
        return nil
    }, backoff.WithContext(getExponentialBackoff(), ctx))
    if err != nil {
        return err
    }
}

🧭 Related

[_**⏫️ Back to list**_](https://github.com/ryan961/memo/issues/2#issue-2080613557)