Skip to content
This repository was archived by the owner on Oct 5, 2025. It is now read-only.
This repository was archived by the owner on Oct 5, 2025. It is now read-only.

[ADR] Optimize Concurrent Operations with Virtual Threads (Project Loom) #44

@groldan

Description

@groldan

Decision Title

Optimize Concurrent Operations with Virtual Threads (Project Loom)

Status

  • Proposed (under consideration)
  • Accepted (decision made)
  • Superseded (replaced by newer decision)
  • Deprecated (no longer relevant)

Context

High-concurrency applications using Tileverse Range Reader often require thousands of concurrent range requests. Traditional thread pools can become a bottleneck due to thread creation overhead and context switching costs. Java 21's Virtual Threads (Project Loom) offer lightweight concurrency that could significantly improve performance for I/O-intensive operations like range reading.

Decision

Implement optional Virtual Thread support for concurrent range operations while maintaining compatibility with traditional threading models.

Problem Statement

Current threading limitations impact scalability:

  • Platform threads have significant memory overhead (1-2MB stack per thread)
  • Thread pool exhaustion under high concurrency
  • Context switching overhead with many concurrent operations
  • Blocking I/O operations waste thread resources
  • Manual thread management complexity for optimal performance

Considered Options

Option 1: Full Virtual Thread Migration

Description: Replace all threading with Virtual Threads by default

Pros:

  • Maximum performance benefit
  • Simplified concurrency model
  • Eliminates thread pool management complexity
  • Automatic scaling to thousands of concurrent operations

Cons:

  • Requires Java 21+ minimum version (breaking change)
  • Potential compatibility issues with existing code
  • Limited control over thread behavior
  • May not benefit CPU-intensive operations

Option 2: Optional Virtual Thread Support

Description: Provide Virtual Thread support as an optional configuration

Pros:

  • Backward compatibility maintained
  • Users can opt-in based on Java version
  • Gradual migration path
  • No breaking changes to existing APIs

Cons:

  • Added complexity with dual threading modes
  • Testing overhead for both modes
  • API complexity for configuration

Option 3: Hybrid Threading Model

Description: Use Virtual Threads for I/O operations, platform threads for CPU work

Pros:

  • Optimal resource utilization
  • Performance benefits where they matter most
  • Maintains control over CPU-intensive operations
  • Fine-grained optimization opportunities

Cons:

  • Most complex implementation
  • Difficult to tune and configure
  • Higher maintenance overhead
  • Potential for thread coordination issues

Decision Rationale

Option 2 (Optional Virtual Thread Support) is selected because:

  • Maintains backward compatibility with existing applications
  • Provides clear performance benefits for high-concurrency scenarios
  • Allows gradual ecosystem migration to Java 21+
  • Enables A/B testing of threading models
  • Balances innovation with stability

Architecture Impact

Affected Components

  • Core module (threading abstractions)
  • Cloud provider modules (S3, Azure, GCS)
  • Caching layer (concurrent operations)
  • Authentication system
  • Performance optimizations
  • API design
  • Build system
  • Documentation

Breaking Changes

  • No breaking changes
  • Minor breaking changes (patch release)
  • Major breaking changes (major version bump)

Performance Impact

  • Performance improvement expected
  • Performance neutral
  • May degrade performance (requires optimization)
  • Unknown performance impact (requires analysis)

Security Impact

  • Improves security
  • Security neutral
  • Potential security implications (requires review)

Implementation Plan

Phase 1: Threading Abstraction

  • Create ExecutorService abstraction for threading strategy
  • Implement VirtualThreadExecutorService for Java 21+
  • Add configuration mechanism for threading mode selection
  • Ensure backward compatibility with traditional thread pools

Phase 2: Integration Points

  • Update HTTP client to use configurable executor
  • Modify cloud provider implementations for virtual threads
  • Update caching operations for concurrent access
  • Add virtual thread support to benchmark tests

Phase 3: Performance Optimization

  • Benchmark virtual thread performance vs traditional threads
  • Optimize blocking operations for virtual thread efficiency
  • Tune virtual thread configuration parameters
  • Validate memory usage improvements

Phase 4: Documentation & Rollout

  • Document virtual thread configuration options
  • Create performance tuning guides
  • Add migration recommendations
  • Update troubleshooting documentation

Dependencies

  • Java 21+ runtime for virtual thread support
  • Executor abstraction design
  • Performance benchmarking infrastructure
  • Integration testing across threading models

Effort Estimate

  • Medium (1-4 weeks)

Quality Attributes Impact

Performance

Impact: Positive
Details: Expected 3-5x improvement in concurrent operation throughput with significantly reduced memory usage

Reliability

Impact: Positive
Details: Virtual threads reduce thread pool exhaustion scenarios and improve fault isolation

Security

Impact: Neutral
Details: No security implications; virtual threads use same security model as platform threads

Maintainability

Impact: Positive
Details: Simplified concurrency model reduces complexity of thread management code

Usability

Impact: Positive
Details: Automatic scaling reduces need for manual thread pool tuning

Portability

Impact: Negative
Details: Virtual thread features require Java 21+, but fallback maintains Java 17+ compatibility

Consequences

Positive Consequences

  • Dramatic improvement in concurrent operation scalability
  • Reduced memory footprint for high-concurrency applications
  • Simplified thread management and configuration
  • Better resource utilization for I/O-intensive workloads
  • Future-ready architecture for modern Java applications

Negative Consequences

  • Additional complexity in configuration and testing
  • Potential debugging challenges with virtual threads
  • Dependency on newer Java versions for optimal performance
  • Need for dual-mode testing and maintenance

Risks

  • Risk: Virtual thread performance may not meet expectations in all scenarios

    • Impact: Medium
    • Probability: Low
    • Mitigation: Comprehensive benchmarking and fallback to platform threads
  • Risk: Compatibility issues with existing thread-local usage

    • Impact: Low
    • Probability: Medium
    • Mitigation: Thorough testing and documentation of thread-local considerations

Validation & Verification

How will this decision be validated?

  • Performance benchmarks
  • Proof of concept implementation
  • Community feedback
  • Expert review
  • Production testing

Success Criteria

  1. 3x improvement in concurrent request throughput under high load
  2. 50%+ reduction in memory usage for thread management
  3. Zero compatibility issues with existing single-threaded usage
  4. Smooth fallback behavior on Java 17/20 runtimes

Rollback Plan

If virtual threads prove problematic:

  1. Disable virtual thread option via configuration
  2. Fall back to traditional thread pool implementation
  3. Remove virtual thread dependency in future release if needed

Documentation Requirements

  • Architecture Decision Record (ADR-009)
  • API documentation update
  • Architecture documentation update
  • Migration guide (if breaking changes)
  • Developer guide updates

Stakeholder Impact

Library Users

Impact: Positive - Optional performance improvement without breaking changes

Library Contributors

Impact: Neutral - Additional testing complexity but cleaner concurrency model

Ecosystem Integration

Impact: Positive - Better performance for server applications and frameworks

Related Decisions

  • Related to ByteBuffer pooling (shared resource management under high concurrency)
  • Connected to performance benchmarking infrastructure requirements
  • Dependencies on Java 21+ adoption timeline

References

Timeline

Target Decision Date: Q1 2025
Target Implementation Date: Q2 2025
Target Release: Version 1.2

Additional Context

API Configuration Design

// Automatic virtual thread detection (Java 21+)
RangeReader reader = S3RangeReader.builder()
    .uri(uri)
    .withVirtualThreads() // Auto-detects capability
    .build();

// Explicit executor configuration
ExecutorService executor = VirtualThreadExecutorService.create();
RangeReader reader = S3RangeReader.builder()
    .uri(uri)
    .withExecutor(executor)
    .build();

// Global virtual thread configuration
RangeReaderSettings.setDefaultThreadingMode(ThreadingMode.VIRTUAL);

Performance Expectations

Based on preliminary analysis:

  • Concurrent Operations: 10,000+ vs 200-500 with platform threads
  • Memory Usage: ~1KB per virtual thread vs 1-2MB per platform thread
  • Startup Time: Negligible overhead vs significant thread pool creation
  • Latency: Reduced due to elimination of thread pool queuing

This architectural decision positions the library for modern high-concurrency Java applications while maintaining compatibility with existing deployments.

Metadata

Metadata

Assignees

No one assigned

    Labels

    architectureArchitecture decision or designdecisionArchitecture decision recorddesignDesign and user interfaceperformancePerformance requirement or optimization

    Type

    No type

    Projects

    Status

    No status

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions