Calmcode - stamina: good practice

Stamina does retry the right way.

1 2 3 4

Imagine that you're sending 100 requests to a server and they all fail. Then you could wait a few seconds and try all the requests again ... but that might hurt the server. If the first 100 requests couldn't be handled immediately you may be better off giving the service a breather.

That's why stamina takes a principled approach that revolves around combining two concepts.

  1. Exponential Backoff: each time a function fails you wait for for longer before calling the next one. This ensures that if the service is rebooting that you wait for longer each time.
  2. Randomness: to prevent loads of traffic spiking every time you do another retry you should also add some randomness in the waiting time. That way, you "flatten the curve" a bit for the service that you're polling.

Combined, these two approaches give a very sensible strategy for retrying code in Python.