prod-wag-backend-automate-s.../api_services/api_controllers/redis/README.md

86 lines
3.1 KiB
Markdown

# Redis Controller
## Overview
This module provides a robust, thread-safe Redis connection handler with comprehensive concurrent operation testing. The Redis controller is designed for high-performance, resilient database connection management that can handle multiple simultaneous operations efficiently.
## Features
- Singleton pattern for efficient connection management
- Connection pooling with configurable settings
- Automatic retry capabilities for Redis operations
- Thread-safe operations with proper error handling
- Comprehensive JSON data handling
- TTL management and expiry time resolution
- Efficient batch operations using Redis pipelines
## Configuration
The Redis controller is configured with the following default settings:
- Host: 10.10.2.15
- Port: 6379
- DB: 0
- Connection pool size: 50 connections
- Health check interval: 30 seconds
- Socket timeout: 5.0 seconds
- Retry on timeout: Enabled
- Socket keepalive: Enabled
## Usage Examples
The controller provides several high-level methods for Redis operations:
- `set_json`: Store JSON data with optional expiry
- `get_json`: Retrieve JSON data with pattern matching
- `get_json_iterator`: Memory-efficient iterator for large datasets
- `delete`: Remove keys matching a pattern
- `refresh_ttl`: Update expiry time for existing keys
- `key_exists`: Check if a key exists without retrieving it
- `resolve_expires_at`: Get human-readable expiry time
## Concurrent Performance Testing
The Redis controller has been thoroughly tested for concurrent operations with impressive results:
### Test Configuration
- 10,000 concurrent threads
- Each thread performs a set, get, and delete operation
- Pipeline used for efficient batching
- Exponential backoff for connection errors
- Comprehensive error tracking and reporting
### Test Results
```
Concurrent Redis Test Results:
Total threads: 10000
Passed: 10000
Failed: 0
Operations with retries: 0
Total retry attempts: 0
Success rate: 100.00%
Performance Metrics:
Total execution time: 4.30 seconds
Operations per second: 2324.35
Average operation time: 1.92 ms
Minimum operation time: 0.43 ms
Maximum operation time: 40.45 ms
95th percentile operation time: 4.14 ms
```
## Thread Safety
The Redis controller is designed to be thread-safe with the following mechanisms:
- Connection pooling to manage concurrent connections efficiently
- Thread-local storage for operation-specific data
- Atomic operations using Redis pipelines
- Proper error handling and retry logic for connection issues
- Exponential backoff for handling connection limits
## Error Handling
The controller implements comprehensive error handling:
- Connection errors are automatically retried with exponential backoff
- Detailed error reporting with context-specific information
- Graceful degradation under high load
- Connection health monitoring and automatic reconnection
## Best Practices
- Use pipelines for batching multiple operations
- Implement proper key naming conventions
- Set appropriate TTL values for cached data
- Monitor connection pool usage in production
- Use the JSON iterator for large datasets to minimize memory usage