Heuristic:A2aproject A2a java Backpressure Tuning
| Knowledge Sources | |
|---|---|
| Domains | Optimization, Concurrency, Server |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
Semaphore-based backpressure tuning for the A2A EventQueue: use `DEFAULT_QUEUE_SIZE=1000` to balance producer throughput against consumer (network) speed, preventing unbounded memory growth.
Description
The `EventQueue` uses a two-level backpressure mechanism. The first level is a `Semaphore` on the `MainQueue` with `DEFAULT_QUEUE_SIZE=1000` permits. When the agent executor enqueues events faster than subscribers can consume them, `semaphore.acquire()` blocks the producer thread, providing natural backpressure. The second level is the `ChildQueue`'s bounded `LinkedBlockingDeque` (also sized to `queueSize`), which prevents a single slow subscriber from growing its deque unboundedly — if the child queue is full, it closes immediately. The reactive stream `request-n` contract via Mutiny Zero provides a third layer between the queue and the network.
Usage
Use this heuristic when tuning A2A server performance under load. Symptoms that indicate backpressure tuning is needed: agent executor threads blocking for long periods, `EnqueueTimeoutException` being thrown, or `ChildQueue is full. Closing immediately` warnings in logs.
The Insight (Rule of Thumb)
- Action: Set the queue size via `EventQueue.builder().queueSize(N)`.
- Value: Default `DEFAULT_QUEUE_SIZE = 1000`. For high-throughput streaming agents (e.g., LLM token streaming), consider increasing to 5000-10000. For memory-constrained deployments, reduce to 100-200.
- Trade-off: Larger queues absorb bursts but increase memory usage (~1KB per event × queueSize) and delay backpressure signals. Smaller queues provide faster backpressure but may block producers too aggressively.
- Timeout: Use `enqueueEvent(event, timeout, unit)` for bounded waits; the default `enqueueEvent(event)` blocks indefinitely.
Reasoning
The semaphore acts as a flow control valve: each `acquire()` takes a permit, and the `MainEventBusProcessor` releases the permit in its `finally` block after persisting and distributing the event. This means the semaphore capacity reflects the total number of events "in flight" (queued + being processed). With 1000 permits, up to 1000 events can be buffered before the agent executor thread is blocked. The `ChildQueue` bounded deque prevents a slow SSE consumer from consuming all memory — if `queue.offer(item)` fails, the child is closed immediately (fail-fast), protecting the server from OOM. The two-level design ensures that one slow subscriber cannot degrade service for other subscribers or block the MainEventBusProcessor.
Code Evidence
Semaphore creation from `EventQueue.java:523`:
this.semaphore = new Semaphore(queueSize, true); // fair semaphore
Backpressure acquire from `EventQueue.java:624-635`:
// Acquire semaphore for backpressure
try {
if (unit == null) {
semaphore.acquire();
} else if (!semaphore.tryAcquire(timeout, unit)) {
awaitingChildren.forEach(ChildQueue::cancelPendingFinalEvent);
throw new EnqueueTimeoutException();
}
} catch (InterruptedException e) {
awaitingChildren.forEach(ChildQueue::cancelPendingFinalEvent);
Thread.currentThread().interrupt();
throw new EnqueueInterruptedException(e);
}
ChildQueue bounded deque from `EventQueue.java:899-905`:
public ChildQueue(MainQueue parent) {
this.parent = parent;
// Bound the child queue with the same capacity as its parent so that a slow
// subscriber cannot grow the deque unboundedly.
this.queue = new LinkedBlockingDeque<>(parent.getQueueSize());
}
ChildQueue overflow fail-fast from `EventQueue.java:933-936`:
if (!queue.offer(item)) {
LOGGER.warn("ChildQueue {} is full. Closing immediately.", this);
close(true); // immediate close
}
Default queue size from `EventQueue.java:43`:
public static final int DEFAULT_QUEUE_SIZE = 1000;