Postgres advisory locks for at-most-once delivery

We replaced a Redis-based mutex with a single Postgres advisory lock and removed an entire class of races.

Amy Team1 min read

Distributed locks are a classic "we need this for one thing" infrastructure trap. We had a Redis-based mutex serving exactly one code path: making sure a webhook's side effects ran once even when Paddle re-delivered the same event.

pg_try_advisory_xact_lock(hashtext($event_id)) does the same job in five characters per call, with no extra dependency, and tied to the same transaction that writes the side effect. If the transaction rolls back, the lock disappears. If it commits, the work is durable. We deleted the Redis dependency the same week.

More in Amy Engineering

View all →

How is Amy's credit system

How we ship a credit-based ledger that survives partial failures, refunds, and webhook re-deliveries — without losing a single cent.

Henry Ng3 min read