Powered by AppSignal & Oban Pro

Idempotency — store-and-replay

idempotency.livemd

Idempotency — store-and-replay

Mix.install(
  [
    {:ash_onetime, "~> 2.0.0"},
    {:ash_postgres, "~> 2.15 and >= 2.15.2"}
  ],
  consolidate_protocols: false
)

What this notebook covers

Idempotency is for safe retries of an effectful action. The first execution runs the effect and stores its result; a retry with the same key replays the stored result without re-executing. A retry with the same key but a changed request is a terminal conflict, never a replay.

This notebook walks through fresh execution, replay, and the fingerprint conflict — against a real PostgreSQL. Run each cell top to bottom.

Requires Elixir 1.20+, Ash 3.38.1+, DBConnection 2.10.3+, Postgrex 0.22.4+, and PostgreSQL 18 reachable at DATABASE_URL. Use a development database where you can create and remove schemas. Each run creates its own schema and removes it in the final cell.

Connect to PostgreSQL

database_url = System.fetch_env!("DATABASE_URL")
Logger.configure(level: :warning)

:ok = Application.put_env(:ash, :disable_async?, true)
# Ash >= 3.33 requires an explicit string-length counting mode; codepoints
# matches how the SQL data layer counts (EEF-CVE-2026-82752 remediation).
:ok = Application.put_env(:ash, :default_string_length_count, :codepoints)
:ok = Application.put_env(:ash_onetime_demo, AshOnetimeDemo.Repo, url: database_url, pool_size: 10)

Define the Repo, codec, classifier, and resource

defmodule AshOnetimeDemo.Repo do
  use AshPostgres.Repo, otp_app: :ash_onetime_demo

  @impl AshPostgres.Repo
  def installed_extensions, do: ["ash-functions"]

  @impl AshPostgres.Repo
  def min_pg_version, do: %Version{major: 18, minor: 0, patch: 0}
end

{:ok, _} = AshOnetimeDemo.Repo.start_link()
defmodule AshOnetimeDemo.ChargeCodec do
  @behaviour AshOnetime.Codec

  @impl true
  def format_tag, do: "charge-v1"

  @impl true
  def encode(value, _contract, _opts),
    do: {:ok, format_tag(), :erlang.term_to_binary(value)}

  @impl true
  def decode("charge-v1", payload, _contract, _opts),
    do: {:ok, :erlang.binary_to_term(payload, [:safe])}
end

defmodule AshOnetimeDemo.ChargeClassifier do
  def classify(value, _context), do: {:store, value}
end

defmodule AshOnetimeDemo.Domain do
  use Ash.Domain, validate_config_inclusion?: false

  resources do
    resource AshOnetimeDemo.Charge
  end
end

defmodule AshOnetimeDemo.Charge do
  use Ash.Resource,
    domain: AshOnetimeDemo.Domain,
    data_layer: AshPostgres.DataLayer,
    extensions: [AshOnetime.Resource]

  postgres do
    table "demo_charges"
    repo AshOnetimeDemo.Repo
  end

  multitenancy do
    strategy :context
  end

  attributes do
    uuid_primary_key :id
    attribute :account_id, :uuid, allow_nil?: false, public?: true
    attribute :amount, :integer, allow_nil?: false, public?: true
  end

  actions do
    defaults [:read]

    create :charge do
      transaction? true
      argument :idempotency_key, :string, allow_nil?: false
      accept [:account_id, :amount]
    end
  end

  onetime do
    protect :charge do
      strategy :idempotency
      scope([{:static, "charge"}, {:attribute, :account_id}])
      key({:client, :idempotency_key})
      fingerprint(attributes: [:account_id, :amount])
      response(AshOnetimeDemo.ChargeCodec,
        fields: [:id, :account_id, :amount],
        classify: AshOnetimeDemo.ChargeClassifier
      )
      retention({1, :hour})
    end
  end
end

:ok

Install the admission store

alias AshOnetimeDemo.Repo
alias Ecto.Adapters.SQL

unique = Ecto.UUID.generate() |> String.replace("-", "")
schema = "ash_onetime_demo_#{unique}"
temp = Path.join(System.tmp_dir!(), "ash_onetime_demo_#{unique}")
File.mkdir_p!(temp)

{schema, temp}

source =
  Mix.Tasks.AshOnetime.Gen.Migrations.render(Repo,
    hash_partitions: nil,
    partition_start: Date.utc_today() |> Date.beginning_of_month()
  )

timestamp = "20260101000000"
path = Path.join(temp, "#{timestamp}_install_ash_onetime.exs")
File.write!(path, source)
[{module, _}] = Code.compile_file(path)
version = String.to_integer(timestamp)

SQL.query!(Repo, ~s{CREATE SCHEMA "#{schema}"}, [])
:ok = Ecto.Migrator.up(Repo, version, module, prefix: schema, log: false)

SQL.query!(
  Repo,
  """
  CREATE TABLE IF NOT EXISTS "#{schema}"."demo_charges" (
    id uuid PRIMARY KEY,
    account_id uuid NOT NULL,
    amount bigint NOT NULL
  )
  """,
  []
)

{schema, version, module}

Fresh execution

The first :charge runs the effect and stores its result. replayed?/1 is false.

account_id = "00000000-0000-0000-0000-000000000001"

{:ok, fresh} =
  AshOnetimeDemo.Charge
  |> Ash.Changeset.for_create(:charge, %{account_id: account_id, amount: 10, idempotency_key: "key-1"})
  |> Ash.Changeset.set_tenant(schema)
  |> Ash.create()

%{amount: fresh.amount, replayed?: AshOnetime.replayed?(fresh)}
%{amount: 10, replayed?: false}

The claim's durable address

AshOnetime.Transaction.claim_id/1 is the one sanctioned read of a fresh admission's claim UUID — Admission stays opaque. On the transaction boundary (see Transaction-owned admission), persist the UUID wherever you correlate your own invocation records with the claim. The following cell reserves a separate key inside a transaction you own, stores the current charge count as exact replay bytes, and reads the resulting claim UUID:

{:ok, claim_uuid} =
  Repo.transaction(fn ->
    options = [
      operation: {AshOnetimeDemo.Charge, :receipt},
      partition: "notebook",
      prefix: schema,
      scope: "charge-count",
      key: "receipt-1",
      fingerprint: :crypto.hash(:sha256, "charge-count"),
      retention_seconds: 3600,
      codec: "charge-count"
    ]

    {:execute, admission} = AshOnetime.Transaction.idempotency(Repo, options)
    [[count]] = SQL.query!(Repo, ~s{SELECT count(*) FROM "#{schema}"."demo_charges"}, []).rows
    :ok = AshOnetime.Transaction.complete(admission, Integer.to_string(count))
    AshOnetime.Transaction.claim_id(admission)
  end)

{:ok, ^claim_uuid} = Ecto.UUID.cast(claim_uuid)
%{valid_claim_uuid: true}
%{valid_claim_uuid: true}

claim_id/1 itself is a pure read. Reserving and completing admission still requires the caller's open PostgreSQL transaction.

Retry replays the stored result

The same idempotency_key with the same content returns the stored record. replayed?/1 flips to true, and only one row exists — the effect ran once.

{:ok, replayed} =
  AshOnetimeDemo.Charge
  |> Ash.Changeset.for_create(:charge, %{account_id: account_id, amount: 10, idempotency_key: "key-1"})
  |> Ash.Changeset.set_tenant(schema)
  |> Ash.create()

%{
  same_record: replayed.id == fresh.id,
  replayed?: AshOnetime.replayed?(replayed),
  rows: SQL.query!(Repo, ~s{SELECT count(*) FROM "#{schema}"."demo_charges"}, []).rows |> hd() |> hd()
}
%{replayed?: true, rows: 1, same_record: true}

Same key, different request → terminal conflict

Reusing idempotency_key with a changed amount is rejected as :key_reused_with_different_request. The effect does not run again.

{:error, error} =
  AshOnetimeDemo.Charge
  |> Ash.Changeset.for_create(:charge, %{account_id: account_id, amount: 99, idempotency_key: "key-1"})
  |> Ash.Changeset.set_tenant(schema)
  |> Ash.create()

AshOnetime.Error.code(error)
:key_reused_with_different_request

Bounded maintenance and refusal counts

Run partition maintenance as the table owner. Under FORCE RLS, the owner temporarily un-FORCEs each candidate inside a savepoint, refuses if a DDL hook reapplies FORCE, and checks emptiness with EXISTS under row_security=off. The parent lock is taken before the child. One lock budget (default two seconds) bounds the whole pass: a parent-lock timeout keeps the partition for the next run, and the child and _default locks never wait. The kept total includes occupied partitions. payload_partitions_refused identifies refusals by reason, and only refusals emit :partitions_kept telemetry.

An application role that does not own the tables can run claims-only cleanup with partition_limit: 0; schedule owner-run partition maintenance separately. This cell exercises the additive counts contract without pruning this notebook's replay data:

target = AshOnetime.Store.Postgres.for_repo(Repo, schema)
{:ok, counts} = AshOnetime.Store.cleanup(target, 100, 0)
%{payload_partitions: 0, payload_partitions_kept: 0, payload_partitions_refused: %{}} = counts
Map.take(counts, [:payload_partitions, :payload_partitions_kept, :payload_partitions_refused])
%{payload_partitions: 0, payload_partitions_kept: 0, payload_partitions_refused: %{}}

Clean up

All operations in this notebook are local or nonce admissions. Stop other callers before rolling back the installation. An installation serving external effects must first recover or account for every unresolved operation through its owning application; rollback refuses while any remain. See the rollback guide.

:ok = Ecto.Migrator.down(Repo, version, module, prefix: schema, log: false)
SQL.query!(Repo, ~s{DROP SCHEMA IF EXISTS "#{schema}" CASCADE}, [])
File.rm_rf!(temp)
:ok

Where to next