Building a Custom Retry Plugin
This guide shows how to create a custom retry plugin with advanced features like circuit breaker patterns, fallback components, and intelligent error handling for Module Federation applications.
Overview
When building distributed applications with Module Federation, handling offline remotes gracefully is crucial for maintaining application stability. A custom retry plugin can provide sophisticated error handling beyond basic retry mechanisms.
Core Features
A robust retry plugin should include:
- Retry Logic: Configurable retry attempts with exponential backoff
- Circuit Breaker Pattern: Prevent cascading failures by temporarily disabling failed remotes
- Fallback Components: Graceful UI degradation when remotes are unavailable
- Timeout Handling: Prevent hanging requests
- Intelligent State Management: Track remote health and failure patterns
Implementation
Basic Plugin Structure
State Management
Track remote health using a state management system:
Retry Logic with Timeout
Implement exponential backoff with timeout protection:
Error Handling by Lifecycle
Handle different error types based on the failure stage:
Fallback Components
Create graceful fallback UI components:
Usage
Configuration
Integration with Share Strategy
Be aware of how shareStrategy affects remote loading:
For applications that need to handle offline scenarios gracefully, consider using loaded-first strategy combined with your retry plugin.
Best Practices
- Graceful Degradation: Always provide meaningful fallback UI
- Circuit Breaker: Prevent cascading failures with circuit breaker pattern
- Logging: Include comprehensive logging for debugging
- Performance: Cache fallback modules to avoid recreation
- Recovery: Allow automatic recovery when remotes come back online
- Configuration: Make retry behavior configurable per environment
Complete Example
Here's a simplified version of a production-ready retry plugin:
Usage in Rspack Configuration
Advanced Features
Consider adding these advanced features:
- Health Checks: Periodic health checks for circuit breaker recovery
- Alternative Sources: Try loading from multiple manifest URLs
- Metrics: Collect metrics on remote reliability
- User Notifications: Notify users about offline remotes
- Progressive Enhancement: Gracefully handle partial functionality
This approach provides robust error handling while maintaining a good user experience even when remote modules are unavailable.