AshBilling.Deduplication (ash_billing v0.7.0)

Copy Markdown View Source

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.WebhookEvent

Or 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

acquire(event_id, opts \\ [])

@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 through with_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.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

clear()

@spec clear() :: :ok

Clears all tracked events in memory (useful in tests).

complete(event_id, opts \\ [])

@spec complete(String.t() | nil, keyword()) :: :ok | {:error, term()}

Completes processing for an acquired event lease.

Returns :ok if successfully marked as :processed, or {:error, reason} on failure.

mark_processed(event_id, opts \\ [])

@spec mark_processed(String.t() | nil, keyword()) :: :ok | {:error, term()}

Marks an event ID as processed directly.

Returns :ok on success or when already recorded, or {:error, reason} if persistence fails.

processed?(event_id, opts \\ [])

@spec processed?(String.t() | nil, keyword()) :: boolean()

Checks if an event ID has already been processed.

release(event_id, opts \\ [])

@spec release(String.t() | nil, keyword()) :: :ok | {:error, term()}

Releases an acquired event lease on failure so retries can be processed.

start_link(opts \\ [])

Starts the deduplication server and initializes the ETS table for memory strategy.

with_lease(event_id, token, opts, fun)

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.