Backend Boilerplates python Advanced

Celery Task with Exponential Backoff

Celery task boilerplate with configurable retry count and exponential backoff.

python
from celery import shared_task
from celery.exceptions import MaxRetriesExceededError

@shared_task(bind=True, max_retries=5, acks_late=True)
def process_event(self, event_id: str):
    try:
        do_work(event_id)
    except Exception as exc:
        delay = 2 ** self.request.retries * 15
        try:
            raise self.retry(exc=exc, countdown=delay)
        except MaxRetriesExceededError:
            send_to_dead_letter(event_id, str(exc))

Prerequisites

  • celery >= 5
  • Redis broker

Pro Tips

  • → Set acks_late=True to prevent message loss.
  • → Monitor with Flower dashboard.

Was this module useful?

Share your experience — it helps the team improve.

Leave Feedback

Commercial Licensing

Acquire a production access token or template license for enterprise deployment.

Module Info

Language
python
Level
Advanced
Read time
5 min
Published
Jul 2024
License
Commercial License

Newsletter

New modules every week, directly to your inbox.