A New Collection of Thoughtful Learning Apps — Now Available on iOS & Android

Image
I’m excited to share a set of mobile apps I’ve recently completed and published on both the Google Play Store and the Apple App Store. These apps are designed with a simple goal in mind: to make meaningful, structured content more accessible, whether you’re studying theology or improving your English vocabulary. 📱 Now Available on Both Platforms All apps are live and available for download: Google Play Developer Page: https://play.google.com/store/apps/dev?id=5835943159853189043 Apple App Store Developer Page: https://apps.apple.com/ca/developer/q-z-l-corp/id1888794100 📖 Theology & Confession Study Apps For those interested in Reformed theology and classical Christian teachings, I’ve developed a series of apps that present foundational texts in a clean, focused reading format: The Belgic Confession Canons of Dort Heidelberg Catechism Westminster Shorter Catechism Each app is designed to provide a distraction-free experience, making it easier to read, reflect, and revisit these im...

ParallelStream vs Virtual Threads in Java 21: What Actually Works for Database Workloads

ParallelStream vs Virtual Threads in Java 21: What Actually Works for Database Workloads



Java offers multiple ways to parallelize work, but not all concurrency models are suitable for database-heavy workloads. In modern systems, especially with large batch processing (e.g., 10,000–50,000 IDs), choosing the right model can drastically change performance.

This article compares ParallelStream and Virtual Threads (Java 21) using a real-world scenario: batch database queries in Oracle.


🧠 The Problem: Large Batch Database Queries

A common backend scenario is fetching data using large ID lists:

Example:
- 40,000 transaction IDs
- Chunked into 1,000 per query
- ~40 database queries executed

The key question becomes: how do we execute these 40 queries efficiently?


⚙️ Approach 1: ParallelStream

IntStream.range(0, batchCount)
    .parallel()
    .mapToObj(i -> repository.query(batch))

How it works

  • Uses ForkJoin common pool
  • Thread count ≈ number of CPU cores
  • Designed for CPU-bound tasks, not blocking IO

Limitations for database workloads

  • Threads are limited (not scalable for IO)
  • Blocking JDBC calls reduce CPU efficiency
  • Can cause underutilization of DB connections
  • No direct control over concurrency level

Result

ParallelStream works, but it is not optimized for database workloads. It behaves like CPU parallelism applied to IO tasks — which is fundamentally mismatched.


⚙️ Approach 2: Virtual Threads (Java 21)

Thread.ofVirtual().start(() -> repository.query(batch));

How it works

  • Each task runs in a lightweight virtual thread
  • Threads are cheap and scalable (millions possible)
  • Blocking JDBC calls do NOT block OS threads
  • Designed specifically for IO-heavy workloads

Key advantage

Virtual threads allow the system to efficiently saturate database connections without overwhelming the CPU scheduler.


📊 Real-World Behavior Comparison

Feature ParallelStream Virtual Threads
Thread model ForkJoin (CPU-based) Lightweight virtual threads
Designed for CPU tasks Blocking IO (DB, HTTP)
Concurrency control Low High (manual control possible)
Scalability Limited (~CPU cores) Very high (thousands+ tasks)
DB workload suitability ❌ Poor ✅ Excellent

🚀 Real Performance Insight

In a production system processing ~40,000 IDs:

  • Sequential batching: ~20–30 seconds
  • ParallelStream batching: inconsistent, often ~10–20 seconds
  • Virtual Threads: ~4 seconds (stable)

The improvement comes not from raw CPU speed, but from better IO concurrency and connection utilization.


🧠 Key Insight

The fundamental difference is simple:

  • ParallelStream = CPU parallelism
  • Virtual Threads = IO parallelism

Using ParallelStream for database workloads is like using a sports car in traffic — powerful, but constrained by the environment.

Virtual threads, on the other hand, behave like adding more lanes to the highway.


🏁 Conclusion

For modern backend systems (especially Spring Boot + JDBC + Oracle):

  • Use ParallelStream for CPU-heavy operations (sorting, mapping, computation)
  • Use Virtual Threads for database calls and external IO

If your workload involves large batch DB queries, Virtual Threads in Java 21 are the clear winner in both simplicity and performance.


Final takeaway: ParallelStream is not broken — it's just the wrong tool for IO-heavy database workloads.

❤️ Support This Blog


If this post helped you, you can support my writing with a small donation. Thank you for reading.


Comments

Popular Posts

A New Collection of Thoughtful Learning Apps — Now Available on iOS & Android

Swagger annotations for API that allows downloading files as zip