Hard java backend

Make independent calls in parallel

The 30 second answer

If a request calls several services or queries that do not depend on each other, running them in sequence adds their latencies together. Running them in parallel, for example with CompletableFuture or virtual threads, makes the total close to the slowest call. I always add a timeout per call and use a bounded executor, so one slow dependency cannot hold every thread.

If two calls don't depend on each other, start both and wait for both. Total time is roughly the slower call, not the sum. For blocking I/O, pass your own executor (or use virtual threads in Java 21) instead of the common pool.

var a = CompletableFuture.supplyAsync(() -> userClient.get(id), exec);
var b = CompletableFuture.supplyAsync(() -> priceClient.get(id), exec);
CompletableFuture.allOf(a, b).join();
Sources (2)
  1. Java 21 CompletableFuture
  2. JEP 444: Virtual Threads