ARTICLE AD BOX
I am trying to open to open 10K+ connections to a server, but my app stops at 8K connections when looking at the server logs. When using two remote servers it will struggles when reaching between 8K and 9K connections. Making me belief the problem is on the client side.
I tried the following settings on the client:
Setting the ephemeral ports:net.ipv4.ip_local_port_range = 32768 60999
Setting the ulimit to 65535
But still my Java application can't open more than 8K outgoing connections.
Some experimenting showed that:
Doing a little less than 8K in rapid succession is fine.
Doing 8.1K requests causes a 30 delay before it continues with only little bursts.
During the tests the machine never runs out of CPU or memory. During the delay it goes back to running at an idle 1-2%.
No error or exception is being thrown.
The code:
public class SimpleRunner { HttpClient client = HttpClient.newBuilder() .version(HttpClient.Version.HTTP_1_1) .followRedirects(HttpClient.Redirect.NORMAL) .connectTimeout(Duration.ofDays(1)) .build(); public void main(String... args) throws IOException { long pid = ProcessHandle.current().pid(); System.out.println("pid = " + pid); try (var executor = Executors.newVirtualThreadPerTaskExecutor()) { benchmarkingMethod(executor); } } private void benchmarkingMethod(ExecutorService executor) { IntStream.range(0, 10_000).forEach(i -> executor.submit(() -> { try { String response1 = fetchJavaNineStyle(URI.create("http://localhost:8080/v1/delay/0").toURL()); } catch (Exception e) { throw new RuntimeException(e); } }) ); } String fetchJavaNineStyle(URL url){ try { return client.sendAsync(HttpRequest.newBuilder().GET().uri(url.toURI()).build(), HttpResponse.BodyHandlers.ofString()).get().body(); } catch (Exception e) { throw new RuntimeException(e); } } }