Skip to content

Support custom task ID generator #9662

Open

Description

@chinafzy

Checklist

  • I have checked the issues list for similar or identical feature requests.
  • I have checked the pull requests list for existing proposed implementations of this feature.
  • I have checked the commit log to find out if the same feature was already implemented in the main branch.
  • I have included all related issues and possible duplicate issues in this issue.

Related Issues and Possible Duplicates

Related Issues

  • None

Possible Duplicates

  • None

Brief Summary

Currently, Celery uses UUID for task IDs which is hardcoded in the implementation. In distributed systems, we often need custom ID generation strategies (like Snowflake ID) for better tracing and debugging.

Design

Architectural Considerations

The change would only affect task ID generation in Celery core, with no external dependencies required.

Proposed Behavior

  1. Add a configuration option task_id_generator that accepts a callable
  2. If provided, use this callable instead of the default UUID generator
  3. Maintain backward compatibility by using UUID as default when no custom generator is provided

Proposed UI/UX

Configuration example:

app.conf.update(
    task_id_generator=snowflake_generator
)

Or via configuration:

app.conf.task_id_generator = snowflake_generator

Diagrams

N/A

Alternatives

  1. Monkey-patching the UUID generation function (not recommended as it affects the entire system)
  2. Subclassing Task class (requires changes in all task definitions)

Activity

  1. jiisanda commented on May 4, 2025

    @jiisanda

    After reviewing the current Celery implementation (basically send_task() in celery.app.base and celery.app.utils), its clear there is no clean way to globally override task ID generation today without few workarounds like manually passing task_id to each task or subclassing Task. After exploring the new Stamping API (from 5.3) is good for metadata and traceability, it does not serve the same purpose. Stamping while allowing additional fields like monitoring_id, but Celery's core task tracking (including results backend, retries, and monitoring tools like Flower) still depends on actual task_id.

    After exploring I found out... why celery only supports UUIDs. UUIDs are safe defaults, simple, its immune to clock drift or backward time change issue. So uuids avoids most of the issues but if we want traceability, time-sortable task IDs (for logs, observability) adding a task_id_generator config could be a practical and low-risk improvement. But its necessary to choose the identifier wisely... immediate substitute to UUIDs could be ULIDs which solves the time and lexicologically ordered, have no clock drift issue and is also immune to backward time change... Snowflakes on the other hand are Globally unique, are time sortable but they could have clock issue...

    I would like to contribute to implementing a task_id_generator configuration option in Celery, it could provide this flexibility while keeping UUIDs as the default. This change would be backward compatible and optional, it would be a safe enhancement for users with more advanced ID needs. Let me know your thoughts, would like to take this forward with PR if maintainers are open to it.

  2. auvipy commented on May 6, 2025

    @auvipy
    Member

    may be this could be an option....

  3. jiisanda commented on May 7, 2025

    @jiisanda

    Great! I'll start a PR for adding task_id_generator as an optional config, defaulting to uuid like now... but can be overridden for stuff like custom identifiers... should be clean and backward compatible.

    let me know if there's anything in particular you'd want me to watch out for, otherwise I'll push something soon.

  4. auvipy commented on May 8, 2025

    @auvipy
    Member

    I want to see a draft PR first

  5. added this to the 5.7.0 milestone on May 4, 2026
  6. modified the milestones: 5.7.0, 5.8.0 on Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions