cloud message_delivery ai_generated partial

GCP Pub/Sub delivers duplicate messages despite 'exactly-once delivery' being enabled

ID: cloud/gcp-pubsub-exactly-once-not-really

Also available as: JSON · Markdown
65%Fix Rate
82%Confidence
3Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
any active

Root Cause

GCP Pub/Sub 'exactly-once delivery' only guarantees exactly-once within the ack deadline. If processing takes longer than ack deadline (default 10s), the message is redelivered. Also, exactly-once is only supported on pull subscriptions.

generic

Workarounds

  1. 92% success Implement idempotent message processing using message_id
    if redis.setnx(f'processed:{message.message_id}', 1, ex=86400): process(message); message.ack()
  2. 85% success Use modack to extend deadline during long processing
    Periodically call modify_ack_deadline() in a background thread while processing
  3. 95% success Design consumers to be idempotent regardless of delivery guarantees
    Use database upserts, idempotency keys, or dedup tables. Never assume exactly-once from any message queue.

Dead Ends

Common approaches that don't work:

  1. Enable exactly-once delivery and assume no duplicates 88% fail

    Exactly-once only works within the ack deadline window. Slow consumers WILL see duplicates. Push subscriptions don't support it at all.

  2. Increase ack deadline to maximum (600s) to avoid redelivery 72% fail

    600s max may still not be enough for slow processing. Also, increases message invisibility window on failure.

  3. Acknowledge message before processing it 90% fail

    If processing fails after ack, the message is lost forever. No redelivery for acknowledged messages.