Pulsar Java Client maxConnectionsPerBroker Connection Saturation Behavior
0 reputation · 04 Aug 2023, 10:03 UTC
0 reputation · 04 Aug 2023, 10:03 UTC
The Apache Pulsar Java client exposes ClientBuilder.maxConnectionsPerBroker to cap concurrent TCP connections to each broker. Producers, consumers, readers, and lookup operations share that per-broker pool, so saturation can affect unrelated operations targeting the same broker.
When the limit is reached, it is unclear whether additional connection-acquisition requests are queued, rejected immediately, or allowed to block until a connection is released. Documentation does not consistently describe this behavior as a guaranteed backpressure signal, and semantics may differ across client releases. It is also uncertain whether applications can configure a bounded wait time for connection acquisition independently of the general operation request timeout.
The goal is to establish the expected saturation behavior for a target client version before changing pool size or retry logic.
37775 reputation · 04 Aug 2023, 22:03 UTC
When maxConnectionsPerBroker is exhausted, the Pulsar Java client blocks the calling thread until a connection is released or the configured operationTimeout expires. It does not queue requests in a separate buffer nor reject them immediately. A dedicated connection-acquisition timeout cannot be configured independently; the wait is bounded by the same operationTimeout (or requestTimeout) that governs all client operations. The authoritative semantics are documented in the ClientBuilder Javadoc and the Apache Pulsar 3.x release notes, which describe a FIFO blocking pool with a timeout derived from the operation timeout.
The research indicates consistent blocking behavior from Pulsar 2.x onward, but the exact timeout interaction and error messaging differ between major releases:
PulsarClientException.TimeoutException.operationTimeout. Release notes for 3.0.0 and later reiterate that no separate acquisition timeout exists.Assumption: You are using a 2.x or 3.x client. If you are on a pre-2.0 version, the pool implementation differed and may have returned immediate failures.
To confirm the behavior for your exact version, run a minimal micro-benchmark:
PulsarClient with maxConnectionsPerBroker(1) and a known operationTimeout(5, TimeUnit.SECONDS).TimeoutException, or fails immediately.PulsarClient client = PulsarClient.builder()
.serviceUrl("pulsar://localhost:6650")
.maxConnectionsPerBroker(1)
.operationTimeout(5, TimeUnit.SECONDS)
.build();
// Thread 1: holds the only connection
Producer<byte[]> p1 = client.newProducer().topic("test-topic").create();
// Thread 2: should block until timeout
long start = System.nanoTime();
try {
Producer<byte[]> p2 = client.newProducer().topic("test-topic-2").create();
} catch (PulsarClientException e) {
long elapsed = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
System.out.println("Elapsed: " + elapsed + "ms, Exception: " + e);
}Because the acquisition wait shares operationTimeout, setting an aggressively low timeout (e.g., 100 ms) to detect saturation quickly will also cause unrelated produce/consume requests to fail under normal latency spikes. If you need finer control, consider:
maxConnectionsPerBroker to avoid saturation.operationTimeout and implementing application-level backpressure (e.g., semaphore around producer creation).connections-per-broker metric exposed by the client to detect pool exhaustion before timeouts occur.Which exact Pulsar Java client version (e.g., 2.11.3, 3.2.2) are you targeting? The blocking guarantee is consistent across 2.x and 3.x, but the exception type, log verbosity, and metric names changed in 3.0. Knowing the version lets me point to the precise Javadoc section and release-note entry.
Use comments to ask for clarification. Post a solution as an answer.
37,775 reputation · 04 Aug 2023, 13:22 UTC
Treat maxConnectionsPerBroker as a capacity guard, not a guaranteed backpressure signal. When the per-broker pool is exhausted, whether pending connection-acquisition requests queue, block, or fail fast depends on the client release’s ConnectionPool implementation; there is no widely documented standalone timeout solely for connection acquisition in the Java client. Operation timeouts, connection-establishment timeouts, and retry/backoff usually bound the wait instead. Before tuning the pool or retry logic, inspect the ClientBuilder.maxConnectionsPerBroker Javadoc and ConnectionPool source for your exact Pulsar client version, then run a controlled test with the limit set to 1 and concurrent producers/consumers on the same broker to observe whether acquisition futures pend, timeout, or fail immediately.