Redis Cache Sync Service
Introduction
This service synchronizes data between blob storage and Redis cache using an event-driven architecture. It receives events from an event service and processes them to update the Redis cache. The service can fetch data from URLs mentioned in the events and perform various cache operations like setting, getting, and deleting data.
Architecture
Flow Diagram
Below is a visual representation of the service's architecture and data flow:
flowchart TB
EventService[Event Service] --> EventReceiver[Event Receiver]
subgraph CacheSync["Redis Cache Sync Service"]
EventReceiver --> Validation[Event Validation]
Validation -->|Valid Event| EventProcessor[Event Processor]
Validation -->|Invalid Event| ErrorHandler[Error Handler]
EventProcessor -->|URL in Event| HttpClient[HTTP Client]
HttpClient -->|Fetch Blob Data| DataProcessor[Data Processor]
EventProcessor -->|No URL| CacheOps[Cache Operations]
DataProcessor --> CacheOps
CacheOps -->|SET| RedisSet[Set in Redis]
CacheOps -->|GET| RedisGet[Get from Redis]
CacheOps -->|DELETE| RedisDelete[Delete from Redis]
CacheOps -->|DELETE_PATTERN| RedisDeletePattern[Delete by Pattern]
end
RedisSet --> Redis[(Redis DB)]
RedisGet --> Redis
RedisDelete --> Redis
RedisDeletePattern --> Redis
BlobStorage[(Blob Storage)] --> HttpClient
Components
- Event Processor - Receives and validates events from the event service
- HTTP Client - Fetches data from blob storage URLs provided in events
- Cache Service - Manages Redis connections and cache operations
- Health Checks - Provides
/liveand/readyendpoints for Kubernetes health monitoring
Data Flow
- Service receives an event from the event service
- Event is validated and parsed
- If the event contains blob URLs, the service fetches data from those URLs
- Data is processed according to event type (SET, GET, DELETE, DELETE_PATTERN)
- Redis cache is updated with the processed data
- Service logs the operation and returns appropriate response
Getting Started
Prerequisites
- Node.js (v14.x or higher)
- Redis server
- Kubernetes cluster (for production deployment)
Installation process
-
Clone this repository:
git clone <repository-url> -
Install dependencies:
npm install -
Configure environment variables:
REDIS_HOST: Redis server hostname (default: "localhost")REDIS_PORT: Redis server port (default: 6379)CLUSTER: Current Kubernetes cluster nameLOG_LEVEL: Logging verbosity (debug, info, warn, error)PORT: HTTP port for health checks and metrics
Running locally
npm start
Deployment
The service is containerized and deployed to Kubernetes using CI/CD pipelines.
API References
Event Structure
Events received by the service must conform to the following structure:
{
"key": "<event-type>",
"value": {
"data": "<operation-data>",
"ttl": "<cache-ttl-in-seconds>"
}
}
Supported Event Types
- SET: Store data in Redis cache
- GET: Retrieve data from Redis cache
- DELETE: Remove specific keys from Redis cache
- DELETE_PATTERN: Remove keys matching a pattern from Redis cache
Build and Test
Building the project
npm run build
This uses Webpack to bundle the application for production.
Running tests
npm test
Maintenance
Clearing Redis Cache
For maintenance purposes, you might need to clear the Redis cache. Use the following command with caution:
# Preview what would be deleted (dry run)
find . -maxdepth 1 -mindepth 1 -print
# Delete all files and directories in current directory
find . -maxdepth 1 -mindepth 1 -print0 | xargs -0 rm -rf
⚠️ Warning: Always verify your current directory before running delete commands.
Contribute
To contribute to this project:
- Fork the repository
- Create a feature branch
- Implement your changes
- Write tests for your changes
- Submit a pull request
Please ensure your code follows the project's coding standards and includes appropriate tests.