Python Exponential Backoff with Jitter — ELI5
Imagine the school cafeteria is closed because they’re refilling the food. You walk up, see it’s closed, and try again in 1 minute. Still closed. You try again in 1 minute. Still closed.
Now imagine every kid in school does the exact same thing. Every minute, 500 kids show up at the cafeteria door at the same time. The cafeteria staff can’t even restock because kids keep opening the door every 60 seconds!
Exponential backoff fixes the first problem. Instead of trying every minute, you wait longer each time:
- First try: wait 1 minute
- Second try: wait 2 minutes
- Third try: wait 4 minutes
- Fourth try: wait 8 minutes
You double the wait each time. This gives the cafeteria more and more time to get ready.
Jitter fixes the second problem — the stampede. Even with exponential backoff, if everyone started at the same time, they’d all retry at the same moments (1 minute, 2 minutes, 4 minutes…). So you add some randomness. Instead of waiting exactly 4 minutes, you wait somewhere between 0 and 4 minutes. Each kid picks a different random time, so they don’t all show up at once.
Your Python app does the same thing with servers. When a server is down, your app retries — but waits longer each time (exponential) and adds randomness to the wait (jitter). This gives the server time to recover without getting stampeded by thousands of apps all retrying at the exact same moment.
One thing to remember: Wait longer between retries (exponential), and add randomness (jitter) so everyone doesn’t retry at the same instant. Together, they give broken things time to heal.
See Also
- Python Aggregate Pattern Why grouping related objects under a single gatekeeper prevents data chaos in your Python application.
- Python Bounded Contexts Why the same word means different things in different parts of your code — and why that is perfectly fine.
- Python Bulkhead Pattern Why smart Python apps put walls between their parts — like a ship that stays afloat even with a hole in the hull.
- Python Circuit Breaker Pattern How a circuit breaker saves your app from crashing — explained with a home electrical fuse analogy.
- Python Clean Architecture Why your Python app should look like an onion — and how that saves you from painful rewrites.