Redis Caching Strategy
Relevant source files
The following files were used as context for generating this wiki page:
The Redis Caching Strategy provides a high-performance data storage layer for session coordination, asynchronous work orchestration, and application-level caching. Redis serves as a volatile data store that reduces latency for frequently accessed information and supports the platform's AI-powered scheduling and dispatch optimization.
Sources: README.md:14, README.md:21
Infrastructure Architecture
The project implements Redis using distinct configurations for local development, staging, and production environments. In cloud environments, the system utilizes AWS ElastiCache for Redis, deployed within private, isolated subnets for security.
Cloud Deployment (AWS)
The infrastructure is defined via both AWS CDK and Terraform. In production, the system deploys a Replication Group with automatic failover and Multi-AZ support.
The diagram shows the network isolation where Redis resides in the protected Data Subnet, accessible only by the application tier.
Sources: infrastructure/aws/cdk/lib/atlasclean-stack.ts:33-51, infrastructure/terraform/main.tf:160-170
Configuration Details
| Option | Development (Docker) | Staging (AWS) | Production (AWS) |
|---|---|---|---|
| Node Type | Container (7-alpine) | cache.t4g.micro | cache.t4g.small |
| Cluster Count | 1 | 1 | 2 (Primary + Replica) |
| Encryption | Disabled | In-transit & At-rest | In-transit & At-rest |
| Failover | Manual | Disabled | Automatic Enabled |
Sources: infrastructure/aws/cdk/lib/atlasclean-stack.ts:133-146, infrastructure/terraform/main.tf:302-318, docker-compose.yml:32-38
Implementation Logic
Connection Management
The redisClient utilizes ioredis to manage connections to the Redis instance. It is configured with lazyConnect and enableAutoPipelining to optimize performance. The system maintains a singleton instance of the client in non-production environments to prevent connection leaks during hot reloads.
export const redisClient =
globalForRedis.redis ??
new Redis(config.redisUrl, {
lazyConnect: true,
maxRetriesPerRequest: 3,
enableAutoPipelining: true,
});
Sources: backend/src/config/redis.ts:8-14
Cache Service Interface
The RedisCache class abstracts the underlying client, providing a simplified interface for standard cache operations. It automatically handles the serialization and deserialization of objects using JSON.stringify and JSON.parse.
| Method | Action | Parameters |
|---|---|---|
get<T> | Retrieves and parses a value from the cache | key: string |
set | Serializes and stores a value | key: string, value: unknown, ttlSeconds?: number |
del | Removes an entry by key | key: string |
Sources: backend/src/infrastructure/cache/RedisCache.ts:3-22
Data Flow for Cache Access
The following sequence diagram illustrates how the application interacts with the caching layer when performing a set operation with an expiration time.
This flow ensures that all application data is stored in a consistent string format with appropriate Time-To-Live (TTL) settings.
Sources: backend/src/infrastructure/cache/RedisCache.ts:9-17
Security and Connectivity
- Security Groups: Access is restricted via EC2 security groups. Only resources within the
ServiceSecurityGroup(ECS Tasks) are permitted to connect to theCacheSecurityGroupon port 6379. - Networking: Redis is deployed into isolated subnets (
SubnetType.PRIVATE_ISOLATED) with no direct internet access. - Authentication: Connection strings are provided via the
REDIS_URLenvironment variable, which includes credentials in local development or points to the primary endpoint address in AWS.
Sources: infrastructure/aws/cdk/lib/atlasclean-stack.ts:101-104, infrastructure/terraform/main.tf:217-226, backend/src/config/redis.ts:9
Conclusion
The Redis Caching Strategy provides the AtlasCleanAI platform with a robust, scalable caching layer. By utilizing ioredis for connection management and a specialized RedisCache utility for data operations, the system achieves low-latency access to session and operational data while maintaining high security through AWS network isolation and encryption.