Software Development

How do Virtual Threads in Java 21 change the way we handle high-concurrency in microservices?

SA Asked by Sarah Jenkins · 14-03-2024
0 upvotes 14,271 views 0 comments
The question

I've been reading about Project Loom and the introduction of Virtual Threads in Java 21. For someone managing a high-traffic Spring Boot application, how exactly do these lightweight threads improve throughput compared to the traditional Platform Threads? Are we moving away from Reactive Programming entirely now that we can have millions of threads without the memory overhead of the past?

3 answers

0
E
Answered on 18-05-2024

Virtual threads are a game-changer because they decouple Java threads from the underlying OS threads. Traditionally, a platform thread was a 1:1 wrapper around a kernel thread, consuming about 1MB of stack memory. This limited us to a few thousand threads. With Virtual Threads, the stack is stored in the heap, allowing you to spawn millions of them. For I/O-bound microservices, you no longer need complex reactive pipelines like WebFlux to achieve high scalability; you can write simple, blocking code that performs like non-blocking code. 

0
MI
Answered on 20-06-2024

While the performance boost is clear, how does this affect our existing thread-local variables and debugging tools? Most of our APM tools rely on thread IDs to trace requests. 

DA 22-06-2024

Michael, that is a valid concern. Java 21 introduced Scoped Values as a modern alternative to ThreadLocal, specifically designed to handle the massive scale of virtual threads. Most updated APM tools now support virtual thread pinning detection, so as long as you avoid synchronized blocks for long I/O operations, your tracing should remain accurate and highly performant.

0
JE
Answered on 10-07-2024

It basically means the 'one thread per request' model is back and better than ever. It simplifies development significantly compared to asynchronous callbacks. 

SA 12-07-2024

Exactly, Jessica. I agree. This shift reduces the cognitive load on developers while maintaining the extreme scalability we previously only got from reactive frameworks.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session