Deduplication engine for billing webhook events.
Supports two strategies:
:memory(default) - High-performance in-memory ETS table with hourly TTL cleanup. Best for single-node deployments and local development.:resource- Database-backed Ash resource deduplication with unique constraint enforcement. Guarantees cross-node deduplication across clustered environments (e.g. Fly.io, Kubernetes, ECS).
Configuration
Via application config:
config :ash_billing,
deduplication_strategy: :resource,
deduplication_resource: MyApp.Billing.WebhookEventOr per-Plug in Phoenix Router:
forward "/webhooks/stripe", AshBilling.Plug,
deduplication_resource: MyApp.Billing.WebhookEvent,
domain: MyApp.Billing
Summary
Functions
Atomically acquires a retryable processing lease for an event ID.
Returns a specification to start this module under a supervisor.
Clears all tracked events in memory (useful in tests).
Completes processing for an acquired event lease.
Marks an event ID as processed directly.
Checks if an event ID has already been processed.
Releases an acquired event lease on failure so retries can be processed.
Starts the deduplication server and initializes the ETS table for memory strategy.
Runs webhook work while holding the event's execution fence.
Functions
@spec acquire(String.t() | nil, keyword()) :: {:ok, String.t()} | {:error, :already_processed} | {:error, :already_processing} | {:error, term()}
Atomically acquires a retryable processing lease for an event ID.
Use with_lease/4 to fence execution. A token alone cannot prevent work from
overlapping after expiry, and external effects still require idempotency.
Returns:
{:ok, lease_token}- Lease acquired; execute work throughwith_lease/4.{:error, :already_processed}- The event was already processed.{:error, :already_processing}- The event is currently being processed by another worker.{:error, reason}- Persistence error during acquisition.
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec clear() :: :ok
Clears all tracked events in memory (useful in tests).
Completes processing for an acquired event lease.
Returns :ok if successfully marked as :processed, or {:error, reason} on failure.
Marks an event ID as processed directly.
Returns :ok on success or when already recorded, or {:error, reason} if persistence fails.
Checks if an event ID has already been processed.
Releases an acquired event lease on failure so retries can be processed.
Starts the deduplication server and initializes the ETS table for memory strategy.
Runs webhook work while holding the event's execution fence.
PostgreSQL holds a row lock in a transaction until work and lease completion finish. Other data layers use a monitored, node-local OTP lock. External effects remain at-least-once: handlers must use the event ID as an idempotency key.
A callback returning {:error, reason} propagates that error and rolls back
PostgreSQL writes; other callback results return {:ok, result}. Raw consumers
must release a failed lease after rollback before retrying. They still own the
outer commit when invoking this function in a caller-owned transaction.