📨 Messaging Advanced ⏱️ 10 min

Idempotent Workers with NATS KV CAS

How BlueRobin uses compare-and-swap semantics in NATS KV to prevent duplicate event processing across multiple worker instances.

By Victor Robin Updated:

Introduction

At-least-once delivery is great for reliability but creates duplicate-processing risk. BlueRobin mitigates this with NATS KV compare-and-swap (CAS) state transitions.

State Machine

  • in_progress
  • completed
  • failed

Combined with a stuck timeout, this enables safe retries without duplicate side effects.

Where It Lives

The pattern is implemented in infrastructure and consumed by workers that process OCR, analysis, and embedding completion events.

Conclusion

Idempotency should be enforced at the event-processing boundary. NATS KV CAS is a simple and effective primitive for that boundary.

Related reading:

  • /nats-advanced-patterns-kv-objectstore/
  • /nats-event-contract-migration-strategy/