Skip to content

Expose Queue Name Property on Queue Bindings #10131

@axmav

Description

@axmav

Describe the solution

Problem Description

Currently, when working with Cloudflare Queues in Workers, queue names are only available via batch.queue in the queue consumer handler. This creates a maintenance issue when queue names are dynamic (e.g., environment-specific like email-queue-staging, email-queue-pr-123) because developers must either:

  1. Hardcode queue names in switch statements (brittle)
  2. Use pattern matching/normalization (complex and error-prone)
  3. Maintain separate mappings (duplicated configuration)

Current Problematic Code

// ❌ Hardcoded queue names - breaks with dynamic environments
switch (batch.queue) {
  case 'email-queue': // What about email-queue-staging or email-queue-pr-123?
  case 'stripe-customer-queue': // What about stripe-customer-staging-queue?
}

// ❌ Complex normalization required
function normalizeQueueName(queueName: string): string {
  if (queueName.startsWith('email-queue')) return 'email-queue'
  if (queueName.includes('stripe-customer') && queueName.includes('queue')) {
    return 'stripe-customer-queue'
  }
  return queueName
}

Proposed Solution

Expose a name property on Queue bindings so developers can reference the actual configured queue name from the binding itself:

interface Queue<Body = unknown> {
  name: string; // NEW: The queue name from wrangler.jsonc
  send(body: Body, options?: QueueSendOptions): Promise<void>;
  sendBatch(messages: Iterable<MessageSendRequest<Body>>, options?: QueueSendBatchOptions): Promise<void>;
}

Desired Usage

// ✅ Clean, maintainable code that works with any queue naming
switch (batch.queue) {
  case env.EMAIL_QUEUE.name: // Automatically resolves to actual queue name
    // Handle email messages
    break;
  case env.STRIPE_CUSTOMER_QUEUE.name: // Works regardless of environment
    // Handle stripe messages  
    break;
}

Benefits

  1. 🔧 Maintainable: No hardcoded queue names
  2. 🌍 Environment-agnostic: Works with staging, production, PR previews
  3. 🛡️ Type-safe: Compile-time validation of queue references
  4. 📝 Self-documenting: Clear relationship between bindings and consumers
  5. ⚡ Simple: Eliminates need for complex normalization logic

Configuration Example

// wrangler.jsonc
{
  "queues": {
    "producers": [
      { "queue": "email-queue-staging", "binding": "EMAIL_QUEUE" }
    ],
    "consumers": [
      { "queue": "email-queue-staging" }
    ]
  }
}

With this feature, env.EMAIL_QUEUE.name would return "email-queue-staging", making the consumer code environment-independent.

Alternative Considered

Using pattern matching (current workaround), but this approach:

  • Requires maintaining separate normalization logic
  • Is error-prone with complex queue naming patterns
  • Duplicates queue naming knowledge across codebase

Use Cases

  • Multi-environment deployments (dev/staging/production)
  • PR preview environments with dynamic queue names
  • Microservices with tenant-specific queues
  • Testing environments with isolated queues

Metadata

Metadata

Assignees

No one assigned

    Labels

    internalRequires support from the Cloudflare Platformproduct:queuesRelating to Cloudflare Queues: https://developers.cloudflare.com/queues/
    No fields configured for Feature.

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions