Skip to content

Conversation

@ykhfree
Copy link
Contributor

@ykhfree ykhfree commented Jul 16, 2025

issue #2224

Add Virtual Thread Support to Resilience4j

Summary

This PR introduces comprehensive support for Java 21's Virtual Threads (Project Loom) across all Resilience4j modules, enabling high-performance concurrent operations with significantly reduced resource overhead compared to traditional platform threads.

What Changed

Core Infrastructure

  • New ExecutorServiceFactory: Central factory for thread management with automatic virtual/platform thread detection
  • Configuration Property: resilience4j.thread.type system property to control thread type (virtual or platform)
  • Backward Compatibility: Seamless fallback to platform threads when virtual threads are unavailable

Module Updates

  • Bulkhead: FixedThreadPoolBulkhead now supports virtual thread pools with proper naming conventions
  • CircuitBreaker: Enhanced SchedulerFactory with thread-safe virtual thread support and automatic configuration detection
  • RateLimiter: Updated SemaphoreBasedRateLimiter implementation for virtual thread compatibility
  • Retry: Virtual thread support in retry mechanisms
  • TimeLimiter: Enhanced timeout handling with virtual threads

ConcurrentHashMap → Smart CompletableFuture Usage

Applied selective CompletableFuture optimization based on operation complexity to prevent virtual thread pinning while maintaining performance:

CompletableFuture Applied (Complex Operations)

  • InMemoryRegistryStore.entryMap: Uses optimized putIfAbsent pattern with complex object creation via mappingFunction.apply()
    • Virtual Thread Benefits: When virtual threads encounter blocking operations (I/O, database calls, complex initialization), they can park without blocking carrier threads, enabling much higher concurrency compared to synchronized blocks that cause thread pinning

Key Optimizations Applied:

  • putIfAbsent Pattern: Replaced computeIfAbsent with more efficient putIfAbsent for better virtual thread performance
  • Exception Handling: Only cleanup failed futures when completeExceptionally() returns true to prevent race conditions
  • JDK 25 Preparation: Added TODO comments indicating future simplification when JDK 25 optimizes computeIfAbsent internally

Result: Optimized memory usage and CPU performance while maintaining virtual thread benefits where they provide genuine value.

Performance Test Results: (with I/O 500-1500ms delays)
- Added a hash collision situation to the scenario

Implementation Thread Type Throughput Max Concurrent Virtual Thread Advantage Status
Future-based HashMap Virtual Thread 101.2 ops/s 100 +370.7% 🥇 Selected
ConcurrentSkipListMap Virtual Thread 89.5 ops/s 100 +194.4% 🥈
ConcurrentHashMap Virtual Thread 25.9 ops/s 85* +63.9% ❌ Thread Pinning
Future-based HashMap Platform Thread 21.5 ops/s 20** baseline -
ConcurrentSkipListMap Platform Thread 30.4 ops/s 20** baseline -
ConcurrentHashMap Platform Thread 15.8 ops/s 20** baseline -

*Limited by thread pinning issues
**Platform threads limited to 20 due to high memory overhead (~2MB per thread vs ~1KB for virtual threads)

Testing Strategy

  • Parameterized Tests: Existing tests now run on both platform and virtual threads via ThreadModeTestBase
  • Dedicated Virtual Thread Tests: New test suites specifically for virtual thread scenarios
  • Performance Validation: Thread metrics and behavior verification across both thread types
  • Compatibility Tests: Ensuring consistent behavior between platform and virtual thread modes

Spring Boot Integration

  • Auto-Configuration: New Resilience4jThreadAutoConfiguration for seamless Spring Boot integration
  • Thread Metrics: ThreadMetricsAutoConfiguration with Micrometer integration for monitoring
  • Configuration Properties: Spring Boot properties support for thread type configuration

Documentation

  • README Updates: Clear instructions for virtual thread configuration and usage
  • Spring Boot Guide: Enhanced documentation with virtual thread examples
  • Migration Guide: Step-by-step instructions for adopting virtual threads

Key Features

Performance Benefits

  • Reduced Memory Footprint: Virtual threads use significantly less memory than platform threads
  • Higher Concurrency: Support for millions of concurrent operations
  • Better Resource Utilization: Efficient handling of I/O-bound operations

Configuration Options

System Property

-Dresilience4j.thread.type=virtual

Spring Boot Configuration

resilience4j:
  thread:
    type: virtual

Environment Variable

export resilience4j.thread.type=virtual

Thread Naming Conventions

  • Platform Threads: bulkhead-{name}-{number} (e.g., bulkhead-test-1)
  • Virtual Threads: bulkhead-{name}-v-{number} (e.g., bulkhead-test-v-0)

Technical Details

ExecutorServiceFactory Design

  • Singleton Pattern: Thread-safe factory with lazy initialization
  • Configuration Detection: Real-time detection of thread type changes
  • Resource Management: Proper shutdown and cleanup of thread pools
  • Fallback Strategy: Graceful degradation to platform threads

Thread Pool Behavior

  • Bulkhead: Virtual thread pools maintain the same semantics as platform thread pools
  • CircuitBreaker: Scheduler factory automatically adapts to thread type changes
  • Context Propagation: Enhanced ContextPropagator for virtual thread compatibility

Testing Architecture

  • ThreadModeTestBase: Base class for parameterized testing across thread types
  • Test Coverage: >95% code coverage maintained across both thread modes
  • Performance Benchmarks: Validation of virtual thread performance benefits

Compatibility

Requirements

  • Java 21+: Virtual threads require Java 21 or later
  • Backward Compatibility: Full compatibility with existing Resilience4j applications
  • Automatic Fallback: Graceful degradation on pre-Java 21 environments

Migration Path

  1. No Code Changes Required: Existing applications work unchanged
  2. Opt-in Activation: Virtual threads must be explicitly enabled
  3. Gradual Adoption: Can be enabled per module or globally

Testing Coverage

Enhanced Existing Tests

  • Parameterized tests running on both platform and virtual threads
  • Thread naming validation for both thread types
  • Behavior consistency verification across thread modes

Breaking Changes

None - This is a fully backward-compatible addition. Existing applications will continue to work unchanged.

Future Considerations

  • Structured Concurrency: Potential integration with Java's structured concurrency features
  • Performance Monitoring: Enhanced metrics for virtual thread pools
  • Framework Integration: Deeper integration with reactive frameworks

@ykhfree ykhfree force-pushed the feature/2224-apply-virtual-thread branch from 0652690 to 05ea7f1 Compare July 16, 2025 04:50
@ykhfree ykhfree force-pushed the feature/2224-apply-virtual-thread branch 4 times, most recently from bc39e43 to ec7372b Compare July 20, 2025 06:21
@ykhfree ykhfree force-pushed the feature/2224-apply-virtual-thread branch 5 times, most recently from 5a7898c to cad3e52 Compare July 25, 2025 13:54
@ykhfree ykhfree force-pushed the feature/2224-apply-virtual-thread branch 6 times, most recently from cccfe12 to b638c63 Compare August 9, 2025 02:08
@ykhfree ykhfree force-pushed the feature/2224-apply-virtual-thread branch 13 times, most recently from e996308 to 0d8c7c1 Compare August 19, 2025 08:31
@ykhfree ykhfree force-pushed the feature/2224-apply-virtual-thread branch 5 times, most recently from c105635 to c99d9af Compare August 21, 2025 06:55
@ykhfree ykhfree marked this pull request as ready for review August 21, 2025 07:04
@dosubot dosubot bot added size:XXL This PR changes 1000+ lines, ignoring generated files. implemented labels Aug 21, 2025
@ykhfree ykhfree force-pushed the feature/2224-apply-virtual-thread branch 4 times, most recently from c4334f2 to 789d987 Compare August 23, 2025 05:03
@ykhfree
Copy link
Contributor Author

ykhfree commented Aug 23, 2025

[Note]
Separate from the review of this PR, I registered an #2343 related to the JDK 21 -> 25 upgrade.
Other reviewers should definitely check it out.

@ykhfree ykhfree force-pushed the feature/2224-apply-virtual-thread branch 12 times, most recently from fd6509f to b7d04b8 Compare August 30, 2025 16:24
@ykhfree ykhfree force-pushed the feature/2224-apply-virtual-thread branch from b7d04b8 to 2a01a59 Compare November 16, 2025 02:57
@ykhfree ykhfree force-pushed the feature/2224-apply-virtual-thread branch from 2a01a59 to 2fa1380 Compare November 16, 2025 03:11
@ykhfree
Copy link
Contributor Author

ykhfree commented Dec 9, 2025

@RobWin
Please review this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

implemented size:XXL This PR changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant