Java core learning path Level 3 This level · 11 of 19
Hard java java-21

How would you handle thousands of concurrent API requests?

The 30 second answer

For mostly I/O-bound requests on Java 21, I'd use virtual threads, one per request, so threads stop being the bottleneck. Then I protect the real limits: bounded database connections, timeouts on every remote call, and a cap on concurrent calls to each downstream service. When overloaded, I reject or queue extra requests instead of falling over. Caching, batching and scaling out behind a load balancer cover the rest.

Could you say this in an interview?
  1. Concurrency model. On Java 21, virtual threads keep blocking code simple while allowing many concurrent requests. In Spring Boot 3.2 and later, spring.threads.virtual.enabled=true makes the embedded web server and Spring Boot's task executors use virtual threads.
  2. Protect bottlenecks. The DB connection pool becomes the real limit; put a Semaphore or bulkhead in front of each downstream service.
  3. Timeouts everywhere. Connect and read timeouts on HTTP clients and DB queries, so slow dependencies don't pile up waiting requests.
  4. Backpressure and load shedding. Rate limiting, returning 429 or 503 early, bounded queues, circuit breakers.
  5. Do less work. Caching, batching, and moving work that doesn't need an immediate answer to a queue.
  6. Scale out. Stateless instances behind a load balancer, with autoscaling.
Semaphore paymentsLimit = new Semaphore(50);

Response callPayments() throws InterruptedException {
    if (!paymentsLimit.tryAcquire(100, TimeUnit.MILLISECONDS)) throw new TooBusyException();
    try { return client.call(); } finally { paymentsLimit.release(); }
}

[Opinion] Points 2 to 6 are standard engineering practice, not from the JEP.

Alternate interview scenarios

"How would you design a system to handle 10,000 requests per second?"

With a concrete number, I start with arithmetic, not tools. Little's law says that, in steady state, the average number of requests in the system equals the average arrival rate times the average time each one spends there. At a steady 10,000 per second and a mean latency of 50 milliseconds, that's about 500 requests in flight on average. That is a starting point, not a capacity target: bursts and slow tail requests push the real peak higher, so I size threads, pools and instances with headroom and confirm by load testing. Next, the database budget. If every request hit the database, that's 10,000 queries per second, so I ask what the read-to-write ratio is and how much a cache can absorb. Reads go to a cache or read replicas. Writes that don't need an immediate answer go through a queue. Then I load test to confirm the per-instance throughput, and scale stateless instances out behind a load balancer. Rate limits and load shedding protect the system above the planned peak.

Sources (4)
  1. JEP 444: Virtual Threadsopenjdk.org
  2. Spring Boot reference: virtual threadsdocs.spring.io
  3. Semaphore Javadocdocs.oracle.com
  4. John D. C. Little and Stephen C. Graves: Little's Law, chapter 5 of Building Intuition (Springer, 2008)ic.unicamp.br
Previous
Next