Topic 24 of 30 · Expert
Why don’t virtual threads remove the need for backpressure?
Virtual threads reduce the cost of blocking-thread concurrency, but they do not increase database connection capacity, CPU throughput or external-service rate limits.
Java 17+; compile each complete program as Main.java.
Detailed answer
Virtual threads reduce the cost of blocking-thread concurrency, but they do not increase database connection capacity, CPU throughput or external-service rate limits.
Bound expensive resources with semaphores or explicit admission policies; set deadlines and propagate cancellation. Limit payload retention while requests wait for scarce downstream capacity.
Use structured lifetime management and an observable overload policy. For Java 17 a bounded executor is an option; Java 21 virtual threads can simplify blocking I/O but still need resource budgets.
Internal working
Scheduling many runnable or blocked tasks and limiting a downstream resource are distinct concerns. Each waiting task may retain a large request object even if its thread is cheap.
Practical scenario
Ten thousand imports each wait for one of twenty database connections. Cheap threads still retain thousands of payloads and create timeout cascades.
import java.util.concurrent.Semaphore;
class Main {public static void main(String[] a)throws Exception{var slots=new Semaphore(2);slots.acquire();try{System.out.println("bounded resource");}finally{slots.release();}}}Common mistakes
- Assuming unlimited concurrency is free.
- Forgetting to release permits on exceptions.
Key takeaways
- Limit scarce resources.
- Propagate deadlines.
- Cheap threads still retain state.
Follow-up challenges
- Where should admission happen?
- How do you distinguish overload from retryable failure?