Repository navigation
Description
Description:
Currently, the Client.Builder does not expose any way to configure the redirect strategy for the underlying Apache HTTP Client 5. This causes issues in environments where ClickHouse cluster infrastructure uses HTTP redirects (e.g., load balancers, reverse proxies, or cluster coordinators that redirect requests to the appropriate shard/replica).
Root cause:
Apache HTTP Client 5 does not follow redirects for POST requests by default. Since the ClickHouse Java client uses POST for all operations (queries, inserts, commands), any HTTP redirect response (301, 302, 303, 307, 308) from the server-side infrastructure will result in a failed request instead of being transparently followed.
The relevant code is in HttpAPIClientHelper.createHttpClient() where HttpClientBuilder is used without setting a redirect strategy:
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
// No .setRedirectStrategy(...) call
return clientBuilder.build();
Expected behavior:
Client.Builder should provide a way to configure redirect following behavior, for example:
Client client = new Client.Builder()
.addEndpoint("http://my-cluster:8123")
.setUsername("user")
.setPassword("password")
.followRedirects(true) // follow redirects for POST requests
.build();
Or alternatively allow passing a custom RedirectStrategy implementation:
Client client = new Client.Builder()
.addEndpoint("http://my-cluster:8123")
.setUsername("user")
.setPassword("password")
.setRedirectStrategy(new DefaultRedirectStrategy())
.build();
Workaround:
Currently the only workaround is to patch the internal httpClient field inside HttpAPIClientHelper via reflection, which is fragile and not suitable for production use:
Field helperField = client.getClass().getDeclaredField("httpClientHelper");
helperField.setAccessible(true);
Object helper = helperField.get(client);
Field httpClientField = helper.getClass().getDeclaredField("httpClient");
httpClientField.setAccessible(true);
httpClientField.set(helper, HttpClientBuilder.create()
.setRedirectStrategy(new DefaultRedirectStrategy())
.build());
Environment:
clickhouse-java version: 0.9.4
Apache HTTP Client 5 (HC5)
ClickHouse server behind a load balancer / reverse proxy with HTTP redirects enabled
Activity
chernser commentedon Mar 5, 2026
Good day, @ShilovI !
Very good suggestion! Will work on it. Thank you!
chernser commentedon Mar 24, 2026
@ShilovI !
Looks like current apache client redirects all request and I have to disable it. I even consider restricting this redirects because looks like not secure.
Would you please double check and tell more about your use-case?
Thanks!
ShilovI commentedon Mar 24, 2026
Hi! Sorry, I forgot to mention this here.
After research, I also discovered that redirects work in the current driver version.
Our use case is as follows. We're working with the Clickhouse interface for the YTsaurus platform. Without going into too much detail, the technology uses multiple redirects to send a request to the cluster node where the actual computation occurs. Here's the documentation.
polyglotAI-bot commentedon Nov 27, 2026
Recommendation: Keep open — maintainer decision
Decide whether to finish PR #2806, which adds a redirect option: the stale bot closed it unmerged. With the default options,
maindoes not follow a redirect of a POST, although the last comment says that redirects work.main: Not supported on main —Client.Builderhas no redirect option. With a local redirect server,queryRecordsfails withClientException: Unexpected result status 307for a 307 or 308. A 301 or 302 is followed, but the server gets no credentials (Code: 194 … REQUIRED_PASSWORD). Run onmain8480ca001, ClickHouse 26.11.2.4, default options.Analysis:
HttpAPIClientHelper.java:320makes the HttpClient 5 client with no redirect setting, so the HttpClient default strategy applies.HttpAPIClientHelper.java:876reports a 3xx that HttpClient does not follow asUnexpected result status. The run used only the default options.Automated triage by Polyglot AI. A maintainer makes the final decision.