Plan Redis caching for specific data: key design, cache-aside or write-through patterns, TTLs and invalidation, data types, eviction policy, clustering and monitoring.
Develop a Redis caching strategy for the application below. Decide per use case what Redis stores, how entries are written and invalidated, and how you will know the cache is working. Choosing between cache layers (CDN, HTTP, application) is a separate, earlier decision; this plan assumes Redis is the chosen layer.
## Inputs
Application and use cases to cache (for example product details, sessions, leaderboards): [USE_CASES]
Source of truth for each use case and how it changes: [DATA_SOURCES]
Allowed staleness per use case: [FRESHNESS]
Read/write rates and data sizes, if known: [WORKLOAD]
Redis deployment (version, single instance, replicas, Redis Cluster, managed service) and memory limit: [DEPLOYMENT]
Client library and language: [CLIENT]
## Instructions
1. **Key design**: a namespaced format such as `app:v1:product:{id}`, including every input that changes the value (locale, currency, tenant). Use a version segment for bulk invalidation after schema changes.
2. **Read/write pattern** per use case: cache-aside, write-through or write-behind, with the consistency consequence of each.
3. **Expiration and invalidation**: TTLs derived from [FRESHNESS], invalidation on write events, and jitter or early refresh for hot keys to avoid stampedes.
4. **Data types and storage**: Strings or Hashes for objects, Sorted Sets for rankings, Sets for membership; compression only for large values; size limits per value.
5. **Memory and eviction**: set `maxmemory` and a `maxmemory-policy` that matches the access pattern. Keep data that must not be evicted (for example sessions) in a separate instance or under a policy that cannot evict it.
6. **Correctness boundaries**: data used for decisions such as stock reservation or payments is read from the source of truth, not from cache.
7. **Clustering and sharding**: only if [WORKLOAD] or [DEPLOYMENT] requires it; multi-key operations need keys in the same hash slot (hash tags).
8. **Monitoring and maintenance**: hit ratio from `keyspace_hits`/`keyspace_misses`, `evicted_keys`, `expired_keys`, memory use, latency, connection counts; alerts and runbooks.
## Return
1. A table per use case: key format, Redis type, pattern, TTL with rationale, invalidation trigger, eviction exposure.
2. Implementation code for one read path and one invalidation path in [CLIENT].
3. Redis configuration (memory, eviction policy, persistence choice).
4. Clustering decision.
5. Monitoring plan and challenges with mitigations.
6. What to measure after rollout instead of predicted outcomes.
## Acceptance checks
- Each TTL ties to the stated staleness tolerance.
- Sessions or other non-reconstructible data cannot be evicted silently.
- Inventory or payment decisions do not rely on cached values.
- Cluster advice addresses multi-key operations.Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Use cases: product detail data, user sessions, top-selling products leaderboard, inventory display on product pages Sources: PostgreSQL products and inventory tables; product edits publish a product.updated event; orders publish order.placed Freshness: product details 5 minutes or on edit; leaderboard 1 minute; inventory display 30 seconds, but checkout must use the database Workload: about 2,000 product reads/second at peak; 50,000 products Deployment: managed Redis 7, one primary with a replica, 4 GB Client: Node.js with node-redis
A table with product details as Hashes under app:v1:product:{id} using cache-aside, a TTL within 5 minutes plus deletion on product.updated, and jitter; the leaderboard as a Sorted Set updated with ZINCRBY on order.placed and read with ZRANGE ... REV; inventory display cached for up to 30 seconds while checkout reads PostgreSQL; sessions moved to a separate Redis instance (or a noeviction configuration) so cache eviction cannot log users out; maxmemory with allkeys-lru for the cache instance; no Cluster because 50,000 products fit the stated memory; and a monitoring plan using keyspace hit/miss and evicted_keys. Illustrative expectation, not a recorded model run.Ask questions or share concrete improvements.
No comments yet.
Sign in to join the discussion.
Guides for adapting and checking prompts: