Sharing the Store
The one change that makes several replicas possible: every replica uses the same PostgreSQL database. This page covers setting it up, what the stores do when a connection to the database drops, and running through PgBouncer.
Cargo feature. The PostgreSQL stores on this page need the
postgresfeature:a2a-protocol-sdk = { version = "0.16", features = ["postgres"] }, ora2a-protocol-server = { version = "0.16", features = ["postgres"] }on its own.
Nothing works across replicas without this. Point every replica at the same PostgreSQL instance:
#![allow(unused)] fn main() { fn card() -> a2a_protocol_types::agent_card::AgentCard { use a2a_protocol_types::agent_card::{AgentCapabilities, AgentCard, AgentInterface, AgentSkill}; AgentCard::new("my-agent", "1.0.0", AgentInterface::jsonrpc("http://localhost:3000")) .with_description("An example agent") .with_skill(AgentSkill::new("echo", "Echo", "Echoes text").with_tags(["echo"])) .with_capabilities(AgentCapabilities::none().with_streaming(true)) } use a2a_protocol_server::push::PostgresPushConfigStore; use a2a_protocol_server::store::PostgresTaskStore; use a2a_protocol_server::{agent_executor, RequestHandlerBuilder}; struct MyExecutor; agent_executor!(MyExecutor, |_ctx, _q| async { Ok(()) }); async fn example() -> Result<(), Box<dyn std::error::Error>> { let url = std::env::var("A2A_DATABASE_URL")?; let handler = RequestHandlerBuilder::new(MyExecutor) .with_agent_card(card()) .with_task_store(PostgresTaskStore::new(&url).await?) // Push configs too: one registered through one replica must be found by // the replica running the task. `build()` refuses a card that declares // push notifications over a shared task store without this. .with_push_config_store(PostgresPushConfigStore::new(&url).await?) .build()?; Ok(()) } }
With that in place, a client whose second request lands on a different replica still finds its task. This is the property everything else here depends on.
Every PostgreSQL store creates its schema at construction, and replicas that
start together against an empty database used to race: CREATE TABLE IF NOT EXISTS is not safe to run concurrently, and one replica could fail its first
start with a duplicate key in pg_type or pg_class. Since 2026-09-23 every
constructor — the task stores, both push-config stores, the shared rate-limit
counter and with_migrations — does its DDL under one transaction-scoped
advisory lock, so they serialize. So do the audit, halt and lease stores,
which have taken it since they were added. The key is the bytes of "a2a_schm" read as
a big-endian i64; an application taking advisory locks of its own on the
same database should avoid it.
A schema of its own
The stores create unprefixed tables (tasks, task_events,
idempotency_keys, push_configs, schema_versions) beside prefixed ones
(a2a_task_claims, a2a_delegations, ...), in the first schema of the
connection's search_path that exists. In a database another application
uses too, give the agent a schema of its own and name it in the URL; nothing
else changes:
CREATE SCHEMA a2a AUTHORIZATION agent;
postgres://agent:PASSWORD@db:5432/app?options=-c%20search_path%3Da2a
search_path=a2a,public (options=-c%20search_path%3Da2a%2Cpublic) works
too, beside another application's public.tasks: since 0.16.0 every check the
start makes before it creates a table, an index, a column or a function looks
in current_schema(), the schema it would create the object in. Until then
the table and index checks searched the whole path, found the other
application's tasks, skipped creating the store's own, and the start failed
on the foreign key from task_events (audit NEW-a-l-2; both forms are pinned
by store::pg_schema::search_path_tests against a live PostgreSQL). A schema
per deployment is also how two A2A deployments share one database. There is no
with_schema option and no table prefix: the URL is the one place this is set
(ADR 0040).
How many connections
Each store opened from a URL opens a pool of its own, of 10 connections
(DEFAULT_POOL_SIZE) unless given another size with new_with_pool_size(url, size); a halt store's listener holds one more, the audit and shared
rate-limit stores have pools of their own too, and each SQL task store keeps a
pool of two for turn-claim renewals (opened at the first renewal, so a replica
that runs no turn never opens it), so that a replica busy with requests does
not starve its own claims of a connection (audit E-F7). Every replica opens all of
them, so what a replica can use is the sum of its pools, and what the
database must allow is that sum times the replicas, plus one more replica's
worth while a rolling update surges a pod. PostgreSQL's default
max_connections is 100, with 3 reserved for superusers: at 10 for tasks
and 10 for push configs, five replicas already ask for 100 (audit E-F6,
D-17), and the sixth connection a new pod asks for is refused (53300),
which the stores answer as unavailable and retry until their bound.
So size the pools to what the work needs rather than to the default. A
send makes a handful of statements on the task store's pool, and the push
config store reads a task's configs once per delivery: examples/deploy-agent
opens DATABASE_POOL_SIZE (10 by default) for tasks and 2 for push configs,
and logs the sum at startup. A pool built by hand and passed to from_pool
can be shared by the plain and tenant-aware task stores
(PostgresTaskStore::pool_options()), and the push-config, audit and
rate-limit stores can share one built with sqlx's defaults. Behind
PgBouncer the arithmetic is the pooler's, not the database's (below).
When a connection drops
A statement whose connection the server ended (a restart, a failover,
pg_terminate_backend, an idle-session timeout) runs once more on a fresh
connection when a second run is harmless, and every other statement is
pinged first (store::pg_retry); with every backend ended mid-turn nothing
was lost (3 runs of 3). A write of a turn's event that the store refuses for
now (a lock or statement timeout, a deadlock) is made again, from 50 ms
doubling to 1 s, for up to HandlerLimits::store_write_retry (30 s); under
a 200 ms statement_timeout with the task row locked for 2 s, it lost three
parts and four log positions before, and none after. During an outage sqlx
itself waits up to its acquire timeout (30 s) for a connection before a
statement fails, so set store_write_retry past the longest failover you
expect beyond that.
#![allow(unused)] fn main() { use std::time::Duration; use a2a_protocol_server::HandlerLimits; // Past the longest failover you expect, on top of sqlx's acquire timeout. let limits = HandlerLimits::default().with_store_write_retry(Duration::from_secs(60)); let _ = limits; }
Hand it to the handler with RequestHandlerBuilder::with_handler_limits.
Every retry is counted in a2a.server.store_retries, labelled by
operation and cause: cause="unavailable" for a turn's event write the
store refused for now (counted by the handler's metrics), and
operation="statement", cause="connection_lost" for a statement run again
on a fresh connection (counted by the store's own metrics, which
PostgresTaskStore::with_metrics sets; a store you construct reports to
nothing until you do). A retry that succeeded lost nothing, so these are not
persistence errors; a rising rate is the database degrading before it starts
losing data.
A pool you build yourself and pass to from_pool keeps its own settings:
with sqlx's defaults it pings each connection before every statement, one
round trip more per statement. PostgresTaskStore::pool_options() returns
the settings new uses (ping only a connection idle for a second or more),
for a pool you size yourself.
Behind PgBouncer
Behind PgBouncer. On 2026-10-29 the claim, replica and store suites
(turn_claims, multi_replica, cross_replica_cancel,
replica_processes, stale_write_position, continuation_save_vs_cancel,
retried_statements, statement_count, postgres_store_tests) ran with
every connection through PgBouncer 1.26.1 in transaction mode, with
max_prepared_statements = 200 (sqlx prepares its statements, and a
transaction-mode pooler needs that setting to carry them), in front of
PostgreSQL 17.11, and so did a 30 s soak (soak_multi_replica, release
build, claims and the upgrade guard on): 20,807 request pairs, 694 a second,
0 failures, 0 cross-replica misses, 0 claims left. All passed but one test,
and that one is a setting, not the claims: a value set with ALTER DATABASE … SET reaches only server
connections opened after it, and PgBouncer hands clients connections it
opened before. So after setting a2a.legacy_claim_ttl (or a
statement_timeout) that way, have the pooler reconnect (RECONNECT on its
admin console) or wait for server_lifetime; until then the old value
holds. The claims, the upgrade guard's mark and the schema lock each last
one transaction, which a transaction-mode pooler keeps on one server
connection.
The halt store's LISTEN does not survive such a pooler, and it is not
stopped by one either: run on a lent server connection, it stays on that
connection when the pooler lends it again, and the next notification
arrives in the middle of a statement, which sqlx fails with unexpected message: NotificationResponse. On 2026-10-30, through PgBouncer 1.26.1,
the halt itself failed to be written that way. Behind a transaction-mode
pooler, build the halt store with
PostgresHaltStore::from_pool_listening_at(pool, direct_url), its listener
on a connection straight to PostgreSQL, or
PostgresHaltStore::from_pool_without_listener(pool), the handlers then
learning of other replicas' halts and cancels at their next poll
(with_halt_poll_interval). from_pool is for a pool that reaches
PostgreSQL directly.
The task stores' log notifications (with_log_notifications, off by default;
Streaming across replicas) have the same failure, for the
same reason, and the same answer: pass LogListen::Direct(url) with the URL of
PostgreSQL itself, so the listener has a session of its own, or leave the
option off and the streams poll. LogListen::Pool is for a pool that reaches
PostgreSQL directly.
On 2026-10-30 every PostgreSQL test in the server crate ran through
PgBouncer 1.26.1 (transaction mode, max_prepared_statements = 200, a
wildcard database entry so each test's scratch database passes through):
147 of 151 passed. The four that did not: the two halt-store tests that
build with from_pool, as above; and two that end the database's
backends and wait for the store to retry a lost connection, which the
pooler hides: it drops the dead server connection and lends the next
statement a new one, so nothing is lost and there is nothing to retry. A
statement in flight when its backend ends is answered by the pooler with
08P01, which the store retries as any 08 error.