Urgent.News

What's breaking now, across thousands of outlets.

Tech

Retry with Exponential Backoff in Python, Done Properly

A script that creates invoices runs at 2 a.m. and calls an API. For about thirty seconds, the API returns errors. Without retries, tonight's invoices never go out. With careless retries, a customer might get the same invoice twice. Both are real problems, and they pull in opposite directions. Retrying sounds simple, but doing it properly means deciding what to retry, how long to wait, when to…

A reliable retry system in Python is crucial when working with APIs and web services. This guide presents a small helper that handles retries following five essential rules: focusing on temporary problems, waiting longer after each failure, adding randomness to prevent simultaneous retries, listening to server instructions, and knowing when to stop. Additionally, it emphasizes the importance of not retrying actions that could lead to duplicate outcomes unless they have been made safe.

The helper focuses on specific HTTP error codes, such as timeouts (408), too many requests (429), and various server issues (500, 502, 503, 504). It distinguishes between temporary errors and permanent ones, ensuring that only the former triggers a retry. For temporary errors, the helper calculates the server's requested wait time, if applicable, and implements an exponential backoff strategy to manage the waiting period effectively.

This strategy involves doubling the delay time after each failed attempt while keeping it within a specified maximum limit, adding a random element to avoid uniformity in retry timing.

Implementing this helper can significantly improve the resilience of your scripts when interacting with external services. It is particularly beneficial for tasks like API calls, data retrieval and saving, and posting updates, where brief outages can disrupt operations. By integrating this retry mechanism, developers can maintain efficiency and reliability without overlooking the critical aspect of handling duplicates, ensuring that actions taken are safe and effective.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Homelab security on 512 MB: warden scores my logs and asks before it bans anything

I wanted one place that tells me what is going on in my homelab: who is poking at it from outside, which boxes have known-exploited CVEs, what needs patching, and whether anything new turned up on the…

  • "Warden" monitors homelab security on minimal resources
  • Combines scanners and adds logic without agents
  • Uses Discord for user approval before taking action

More from Saturday 10 October →