Plan: One commit for a mint

Status

Done (2026-11-27) — the issuance-log writer spends each mint’s jti in the transaction that commits its log row, so a mint costs one commit and takes no pool connection to spend its jti; G6 now prints the pool’s busy peak beside its maximum. The capacity run on the change (#3151, note 4342857221) held G6 at R − 1: pool maximum 2 (busy 0) where the run before it failed at 22 (busy 4), with G1 and G3 about 2 ms faster at p99 and G4 no worse.

Closes: #3151. Relates to: #3018 (superseded: one commit is left to time), #2744; #3152 and #3169 are out of scope.
Tree: main at 990010087. Step 2 starts after !2604 merges: both touch xtask/src/cmd/perf/identity/cells.rs.
Maintainer (2026-11-27): approved moving the jti spend into the log writer’s transaction, through a plan.

Context

A mint does two commits in series. The request inserts its jti on the issuer pool (PgJtiStore::spend, services/craig-security/src/identity/jti.rs, one autocommit INSERT). It then waits for its issuance-log row in the group-commit writer’s batch (PgIssuanceLog, identity/issuance_log.rs), which runs on its own one-connection pool (identity/issuer.rs, token_routes).

Gate G6 (ADR-085 D17) requires the issuer pool to stay below its ceiling, IDENTITY_DB_MAX_CONNECTIONS = 22. On the 2026-11-27 issuer-capacity run (release images; deployment-guide.adoc § Identity capacity, § Figures), two replicas carrying the 1,200/s peak reached 22 and three reached 13, and every other judged gate passed. sqlx opens a connection only when a request finds none idle, so a pool of 22 means 22 were checked out at once. Every request holds a pool connection for its jti commit, so a short write stall at 600 requests a second per replica piles them up.

Not known: whether those 22 were busy inside the judged window or were opened earlier and sat idle. The harness counts every connection of the pool, idle ones included (rig::issuer_connections). Step 2 measures it.

Scope

In: the writer spends the jti (D1, D2); the token handler’s order (D3); G6’s evidence in the harness (D4); the ADR-085 amendment and the docs (D5).

Out:

  • The issuer’s privileges: its login stays INSERT-only on identity_jti (ADR-085 D2).

  • The pool’s default size and the fleet’s connection total: decided from step 3’s figures, as its own issue.

  • Requests lost at a killed or restarted replica (#3152, #3169).

  • The log writer’s batch size (#3019), and any linger to grow batches.

Design

D1. The contract

IssuanceLog (identity/issuance_log.rs) gains one method, and identity/jti.rs is deleted with its JtiStore, PgJtiStore and JtiUnavailable. Spend (Fresh | Reused) moves to issuance_log.rs. TokenState loses jti.

/// A client assertion's jti to spend, kept until `expires_at` + 60 s.
pub struct JtiSpend { pub client_id: String, pub jti: String, pub expires_at: DateTime<Utc> }

#[async_trait]
pub trait IssuanceLog: Send + Sync {
    /// Append `row`; answers once its batch committed.
    async fn append(&self, row: LogRow) -> Result<(), LogUnavailable>;
    /// Spend `spend`. `Fresh`: it was unspent, and it and `minted` (when given) committed together.
    /// `Reused`: it was already spent; `minted` was not written.
    async fn spend(&self, spend: JtiSpend, minted: Option<LogRow>) -> Result<Spend, LogUnavailable>;
}

The queue’s entry becomes an enum: Append { row, ack } and Spend { spend, minted, ack }. QUEUE_DEPTH, MAX_BATCH and APPEND_BUDGET are unchanged, and spend waits inside APPEND_BUDGET as append does.

D2. The batch

The writer drains up to MAX_BATCH entries as today, then commits them in one transaction on its own connection.

The spends are sorted by (client_id, jti) in byte order, stably, so two entries with one key keep their arrival order. Every replica inserts in that order, so no two batches wait on each other in a cycle (opposite_order_batches_both_commit).

The fast path. One statement spends every jti of the batch, in the sorted order:

INSERT INTO identity_jti (client_id, jti, expires_at)
SELECT s.client_id, s.jti, s.expires_at
  FROM ROWS FROM (pg_catalog.unnest($1::text[]), pg_catalog.unnest($2::text[]),
                  pg_catalog.unnest($3::timestamptz[])) WITH ORDINALITY AS s(client_id, jti, expires_at, ord)
 ORDER BY s.ord
ON CONFLICT DO NOTHING

When the rows affected equal the spends, every spend is Fresh. The writer then inserts the batch’s rows (the Append rows and every minted row) with the existing INSERT_BATCH, and commits.

The replay path. When fewer rows were affected, the batch holds a replay or two entries with one key. The writer rolls the transaction back, begins another, and inserts the spends one statement each, in the sorted order: INSERT INTO identity_jti (client_id, jti, expires_at) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING. One row affected is Fresh, none is Reused. It then inserts the Append rows and the minted rows of the Fresh spends, and commits. Both paths are one transaction: a jti and its minted row commit together or not at all.

Privilege. The clause is bare on purpose. PostgreSQL requires SELECT on any column a conflict target or a RETURNING names, and the issuer has INSERT on client_id, jti, expires_at and no read (identity/issuer/probe.rs, INSERT_ONLY_COLUMNS). A bare ON CONFLICT DO NOTHING names no column. Step 3’s first test runs both statements under the issuer’s login; if PostgreSQL refuses them, the step stops and the fallback (the same two paths with a plain INSERT under a savepoint, reading SQLSTATE 23505) is recorded as an erratum before it is built.

A caller that left. When the writer forms a batch, it drops a Spend entry whose caller has stopped waiting (ack.is_closed()): that caller already answered 503, so its jti is left unspent. An Append entry is written whether or not its caller waits, as today.

Failures. Each batch runs inside APPEND_BUDGET. A statement error, a failed commit or an overrun rolls the transaction back, and every entry of the batch answers LogUnavailable: that batch spends nothing and releases nothing. The verifier already refuses what the store’s CHECKs refuse (identity/client_auth.rs: a jti of 1–256 visible ASCII characters, exp at most 60 s ahead), so a row reaches a CHECK only when the issuer’s clock runs over 60 s ahead of the database’s.

Across replicas. The primary key still arbitrates. A second replica’s insert of a key another replica’s open transaction holds waits for that transaction to end, then affects no row if it committed.

D3. The token handler

identity/token.rs. authenticate keeps its order (assertion, client id, status, budget, exp) and returns the client and its JtiSpend; it no longer spends. issue then:

  1. computes what a fresh jti would get: grant(…​), then the signed token and its minted row (Result<(TokenResponse, LogRow), TokenError>);

  2. calls state.log.spend(spend, minted), passing the minted row when step 1 succeeded and None when it refused;

  3. answers from both:

The spend and step 1 Answer

Err(LogUnavailable)

503 log_unavailable; no token is released. A failed batch spends nothing; a caller whose budget ran out while its batch was committing may leave its jti spent (Risks)

Reused

401 invalid_client, jti_reused, whatever step 1 found

Fresh, step 1 refused

step 1’s refusal (invalid_target, invalid_scope, 503 no_signing_key); the jti is spent

Fresh, step 1 minted

200: the token, released after the one commit

Every answer and its precedence are today’s, with one wire change: a failed spend answered 503 with error_description jti_unavailable, and that label no longer occurs. token() still appends a refusal’s row through append, as it does today.

The 503 line of token()’s `#[utoipa::path] responses(…​) becomes: "`temporarily_unavailable`: client status or the issuance log’s commit (the jti spend and the row) could not answer, or no key signs; no token is released". The order described in the module docs of token.rs and client_auth.rs follows.

D4. G6’s evidence

xtask/src/cmd/perf/identity. rig::issuer_connections also returns each replica’s busy connections (count(*) FILTER (WHERE state <> 'idle')). monitor::Peaks and gates::PoolAndSweep carry the busy maximum, and G6’s measured text becomes pool max 22 (busy 22), 23915 overdue jti rows. The verdict still judges the pool maximum, as ADR-085 D17 words the gate.

D5. Documentation

  • docs/modules/ROOT/pages/adrs/adr-085-internal-token-issuer.adoc: a new section, == Amendment — #3151 the jti is spent in the log writer’s transaction. D11’s order holds. D13’s writer commits each batch’s jtis and rows in one transaction, and a credential is released only after it. D2’s privileges are unchanged. D17: a request takes a pool connection only for the 5 s client-status read.

  • data-model-security.adoc § identity_jti: the log writer’s connection inserts the rows.

  • deployment-guide.adoc § Identity capacity: § How it measures (G6 prints the busy maximum) and § Figures (both runs).

  • api/craig-security-issuer.adoc: regenerated by cargo xtask api-docs from D3’s 503 description.

  • idp-integration.adoc, the token endpoint’s bullets: the jti is spent by the log writer, in the commit that holds the row, after the status and the budget.

Steps

Each step is one MR on the train.

  1. Plan page. This file under docs/modules/ROOT/pages/plans/, with its nav entry.

  2. G6’s evidence (D4). The harness, its tests, the deployment guide’s § How it measures, changelog.d/3151.pool-evidence.changed.adoc. The issuer-capacity job runs on the MR with ISSUER_CAPACITY_ARGS="--steps gates,gates-below --r 3"; its G6 lines go on #3151 as the figures before.

  3. The writer and the handler (D1–D3, D5). The privilege test first. Then the code, the tests, the ADR amendment, the docs, changelog.d/3151.changed.adoc. The same job runs on the MR; its figures go on #3151 and into § Figures. The plan is archived on this branch.

Risks

Risk Mode Mitigation

PostgreSQL refuses the bare clause under the INSERT-only login

fail-closed

step 3’s first test runs both statements as the issuer before any other code; the fallback is named in D2

One assertion mints twice, on two replicas or inside one batch

fail-open

the primary key arbitrates as today; one_assertion_presented_to_two_writers_mints_once, a_replay_inside_one_batch_mints_once

A token is released though its commit failed

fail-open

the writer answers only after COMMIT returns; a_failed_commit_spends_nothing_and_releases_nothing

A caller’s budget runs out while its batch commits: its jti is spent and its minted row logged, and no token is released

silent

today’s writer has the same case for the row (run_writer, "a caller left before its answer"); a client signs a new assertion for each attempt; the writer drops a spend whose caller left before the batch formed (D2), a_spend_whose_caller_left_is_dropped

Two replicas' batches deadlock on each other’s keys

fail-closed

every batch inserts in one sorted order; opposite_order_batches_both_commit

A hung transaction on one replica holds a key another replica’s batch wants

fail-closed

that batch waits at most APPEND_BUDGET, then rolls back and answers 503; it needs a replay racing a hung replica

A burst of replays puts every batch on the replay path

silent

the client’s budget is spent before the spend, and a full batch is MAX_BATCH statements in one transaction; a_full_batch_of_replays_answers_inside_the_append_budget

The writer’s one connection slows mints

silent

G1, G3 and G4 are measured at R and R − 1 by step 3’s job before it lands, against step 2’s figures

A row the store refuses fails its whole batch

fail-closed

the verifier refuses the same shapes first (D2); only a clock over 60 s ahead reaches it; the_verifier_refuses_what_the_store_checks

A consumer matches jti_unavailable

silent

no in-tree reader (the label occurs once, in token.rs); the changelog names the change

G6 passes while mints queue for the writer instead

silent

the queue is bounded (QUEUE_DEPTH, APPEND_BUDGET, then 503) and G1 and G3 judge the latency the queue would add

Tests

  • Privilege contract (devstack, tests/constraints/identity_spend.rs, new): the_issuer_login_spends_a_batch_without_reading_the_store. The existing the_issuer_appends_to_its_stores_and_cannot_read_or_delete_them and the boot probe’s matrix are unchanged.

  • Regression (the case that would have caught #3151; devstack, tests/constraints/identity_token.rs): a_mint_spends_its_jti_without_a_request_pool_connection. The fixture gives the writer its own pool, as production does, and the endpoint a request pool of one connection. After one mint warms the 5 s status memo, the test holds that connection and sends 20 mints at once; all answer 200 inside the memo’s 5 s. On today’s code they wait for the held connection and answer 503.

  • Concurrency (devstack, identity_spend.rs): one_assertion_presented_to_two_writers_mints_once (two writers, one assertion, many rounds: exactly one Fresh); a_replay_inside_one_batch_mints_once; opposite_order_batches_both_commit (two writers given the same two keys in opposite orders, repeatedly).

  • Atomicity (devstack): a_failed_commit_spends_nothing_and_releases_nothing, from the existing fault injection (the issuer’s INSERT on the log revoked): 503, no token, and the owner reads no row in identity_jti.

  • Property (devstack): a_batch_answers_as_the_sequential_model_does. For arbitrary batches over a small key space with some keys already spent, each entry’s answer equals a model that applies the entries one by one in the sorted order; the store holds the model’s keys and the log the model’s rows.

  • Budget (devstack): a_full_batch_of_replays_answers_inside_the_append_budget.

  • A caller that left (devstack): a_spend_whose_caller_left_is_dropped. While the owner holds a key the writer’s batch waits on, a second spend is queued and its caller dropped; once the owner rolls back, the owner reads the first key in the store and not the second.

  • Unit (identity/token/tests.rs; the Log fake holds the spent set and the Jtis fake goes): the existing tests keep their names. New: the_answers_keep_their_precedence (a replay outranks a refused edge and a missing key; a refused edge and a missing key still spend the jti) and the_verifier_refuses_what_the_store_checks.

  • Contract (wire): an_unaudited_mint_is_a_503_that_releases_nothing (unit and devstack, existing) keeps asserting log_unavailable and no access_token; cargo xtask api-docs --check holds api/craig-security-issuer.adoc to D3’s 503 description.

  • Unit (harness): g6_prints_the_busy_maximum_and_judges_the_pool_maximum.

  • Exercised, not unit-tested: the two issuer-capacity runs named in Steps.

The claims above and their tests:

  • a jti is never accepted twice: one_assertion_presented_to_two_writers_mints_once, a_replay_inside_one_batch_mints_once;

  • a token is never released before its commit: a_failed_commit_spends_nothing_and_releases_nothing;

  • the issuer cannot read its jti store: the_issuer_login_spends_a_batch_without_reading_the_store;

  • a mint never takes a request-pool connection to spend its jti: a_mint_spends_its_jti_without_a_request_pool_connection;

  • two replicas' batches cannot deadlock: opposite_order_batches_both_commit.

Delivery

  • Doc pages: the ADR-085 amendment, data-model-security.adoc, deployment-guide.adoc § Identity capacity, idp-integration.adoc and the regenerated api/craig-security-issuer.adoc (D5).

  • CHANGELOG entries: changelog.d/3151.pool-evidence.changed.adoc (step 2) and changelog.d/3151.changed.adoc (step 3: one commit for a mint; the 503 label; Ops: none).

  • Close: Closes #3151 on step 3 when its run shows G6 holding at R − 1 with G1, G3 and G4 no worse than step 2’s figures. If G6 does not hold, the MR relates to the issue instead, the figures go on it, and the maintainer decides. #3018 closes as superseded, with a line naming the merge.

Review log

Round Vendor Resolved model Verdict

1

Anthropic (Vertex)

vertex-anthropic/claude-fable-5-2

FLAGS on the second question. D5 named a sentence of the deployment guide that is not there. D3’s "nothing spent" was unpinned for a caller whose budget runs out while its batch commits. The sentence was dropped. D2 now drops a spend whose caller left, and Risks and Tests name the remaining case.

2

Google (Vertex)

vertex-gemini/gemini-4-carbon

FLAGS on the second question: the token route’s API page is api/craig-security-issuer.adoc, not craig-security.adoc, and the #[utoipa::path] 503 line was not named. D3 names the new description and D5 the issuer page and idp-integration.adoc; the maintainer then approved the plan for implementation.

Edit this page · latest