Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ryan961 opened this issue Jan 14, 2024 · 1 comment
Open

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

ryan961 opened this issue Jan 14, 2024 · 1 comment
Labels
Algorithm Golang All things associated with Go. Reading Daily Reading

Comments

@ryan961
Copy link
Owner

ryan961 commented Jan 14, 2024

🤖 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

  • Retrying in a tight loop is dangerous. You risk getting into overload situations that are difficult to recover from.
  • Retrying with a delay helps a little bit but is still dangerous.
  • Exponential backoff is a much safer way of retrying, balancing user experience with safety.
  • Jitter adds an extra layer of protection, preventing clients from sending synchronised surges of requests.
@ryan961 ryan961 added Algorithm Golang All things associated with Go. labels Jan 14, 2024
@ryan961
Copy link
Owner Author

ryan961 commented Jan 14, 2024

📦 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

@ryan961 ryan961 added the Reading Daily Reading label Jan 23, 2024
@ryan961 ryan961 changed the title Retries: An interactive study of common retry methods Go Retries: An interactive study of common retry methods Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Algorithm Golang All things associated with Go. Reading Daily Reading
Projects
None yet
Development

No branches or pull requests

1 participant