Python & FastAPI•Part 5: ORMs, Databases & Background Workers (56 - 65)
Celery Retries & Task Idempotency:
In Simple Words: Use exponential backoff for network failures and ensure tasks are idempotent (can be executed multiple times without unintended side effects).
python
@celery_app.task(bind=True, max_retries=3, default_retry_delay=60)
def charge_payment(self, payment_id: str, amount: float):
try:
call_payment_gateway(payment_id, amount)
except ConnectionError as exc:
raise self.retry(exc=exc, countdown=2 ** self.request.retries)