<!--
SPDX-FileCopyrightText: 2026 BaseLabs Holdings LLC

SPDX-License-Identifier: MIT
-->

# Transactions, retries, and read consistency

Read this page if you group writes in a transaction or connect to an ArcadeDB
cluster. Transaction retries, statement retries, and connection failover have
different replay boundaries.

## Transactions and replay

`Arcadic.transaction/3` passes a session-scoped connection to the callback and
commits on normal return. An exception rolls back and reraises.
`Arcadic.rollback/2` aborts explicitly, causing the transaction to return
`{:error, reason}`. Keep all transaction operations on the callback's connection.

On HTTP, ArcadeDB returns the session in the `arcadedb-session-id` response
header. Arcadic sends it on statements, commit, and rollback. The default
begin request has no body. An explicit isolation level is sent as
`isolationLevel`. ArcadeDB rejects a body that omits that field.

A server can preserve schema changes after rolling back transaction data. See
[Run and recover migrations](../../how-to/migrate-schema.md) before changing
schema.

Managed retries are opt-in. Set `retry: true` for up to three attempts with
jittered exponential backoff starting at 50 ms and capped at 1,000 ms. Pass
`:max_attempts`, `:base_backoff_ms`, and `:max_backoff_ms` in a keyword list to
override the defaults. Only use replayable callbacks. A retryable conflict,
leader rejection, or pre-commit timeout can run the callback again.

```elixir
{:ok, _} =
  Arcadic.transaction(
    conn,
    fn tx ->
      Arcadic.command!(tx, "MERGE (u:User {id: $id}) SET u.seen = $ts", %{"id" => id, "ts" => ts})
    end,
    retry: true
  )
```

A successful HTTP or gRPC statement can report a partial commit, for example
after an explicit SQL batch boundary. Arcadic records that result for the
current transaction attempt and suppresses managed replay if a later call
fails.

A transport failure during commit can leave the result unknown. Reconcile
before replaying. For HTTP autocommit writes, `retries: count` asks ArcadeDB to
retry the statement on a conflict. Session conflicts surface at commit.
`auto_commit: false` passes the server's `autoCommit` flag through unchanged.

## Read consistency and failover

HTTP connections accept `:eventual`, `:read_your_writes`, and `:linearizable`.
`Arcadic.Conn.with_consistency/2` derives a handle at the chosen level.
For read-your-writes, keep the updated connection returned by
`Arcadic.command_bookmarked/4` or `Arcadic.query_bookmarked/4` and pass it to
the next operation. The bookmark has no effect on a single-server deployment.

```elixir
rw = Arcadic.Conn.with_consistency(conn, :read_your_writes)
{:ok, _, next} = Arcadic.command_bookmarked(rw, "CREATE (u:User {id: $id})", %{"id" => id})
{:ok, rows} = Arcadic.query(next, "MATCH (u:User {id: $id}) RETURN u", %{"id" => id})
```

Supply additional HTTP URLs with `hosts: [...]` for availability. Reads can
try the next host on connection failure. Writes fail over only when Arcadic
knows the request failed before sending, or receives a leader rejection.
Sessions pin to a host. Bookmarked calls stay on the primary because a
bookmark is host-relative.
