Skip to content

Redis and caching

Scope: using Redis, and compatible servers such as Valkey, as a cache, session store, rate limiter or message queue, plus the caching patterns that apply to any shared cache. The rules cite the Redis documentation; the Valkey documentation covers the same topics and says the same about security, eviction, persistence and Cluster. Who owns data and how consistent it must be are in general/data.md (DATA-1, DATA-5). Timeouts, retries and circuit breakers for calls to Redis are in general/operations-reliability.md (OPS-3, OPS-5). For a relational system of record, see PostgreSQL; for HTTP and CDN caching, see Web front end and Serverless and edge hosting.

Entries are sorted by score from 1 to 10 (criteria): 9โ€“10 start here or strongly recommended, 6โ€“8 recommended, 1โ€“5 further reading or conditional.

Adopted references

  • 9 Manage Redis (Redis docs): the baseline for running Redis Open Source: administration, security, ACLs, TLS, configuration, replication, Sentinel, Cluster, persistence, and diagnosing latency and memory problems.
  • 8 Caching guidance (Azure Architecture Center): the baseline for caching in general: when to cache, private and shared caches, expiration, keeping the cache consistent on writes, concurrency, availability and protecting cached data, with Redis examples.
  • 8 Caching challenges and strategies (AWS Builders' Library): what goes wrong when a service comes to depend on its cache: cold starts, cache outages, request coalescing, negative caching and stale data.
  • 7 Cache-Aside pattern (Azure Architecture Center): loading data into a cache on demand and invalidating it on writes, with its consistency trade-offs and when not to use it.
  • 7 Valkey documentation by topic (Valkey): the equivalent guides for Valkey, the Redis-compatible key-value store backed by the Linux Foundation.
  • 7 Redis data types (Redis docs): strings, hashes, lists, sets, sorted sets, streams and the other types, and which problems each one fits.
  • 7 Caching best practices (AWS): lazy caching, write-through, TTLs, eviction policies and the thundering herd.
  • 7 Client-side caching reference (Redis docs): how clients keep a subset of the data in their own memory and how the server tells them when to invalidate it.
  • 6 Redis pipelining (Redis docs): sending many commands without waiting for each reply, to cut round trips.
  • 6 Redis anti-patterns every developer should avoid (Redis): keys without TTLs, hot keys, the KEYS command, ephemeral Redis as a primary database and other common mistakes.

Key rules

Caching patterns

Keys, commands and data types

Security

  • 9 REDIS-10 Set the bind directive in redis.conf to private interfaces, firewall the Redis port so that only the application hosts can reach it, and leave protected mode on. Redis is designed for trusted clients in trusted environments, and anyone who reaches the port can, for example, delete the whole data set with FLUSHALL. This is how Redis applies SEC-20. (Redis, Security: network security, Protected mode, Valkey, Security)
  • 8 REDIS-11 Authenticate every client as a named ACL user with only the commands and key patterns it needs, turn off the passwordless default user, and use TLS for every connection that leaves the host. By default, every connection is authenticated as default, which may run every command on every key, and AUTH is sent unencrypted without TLS. The general rules are SEC-5 and SEC-13 in general/security.md. (Redis, ACL, Redis, Security: authentication, Redis, TLS)

Persistence and availability

How to enforce

Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.

  • 7 Redis ACL command categories: built into Redis and Valkey. Give application users rules such as +@all -@dangerous ~myapp:*, in test environments too, so that Redis rejects KEYS, FLUSHALL, CONFIG and the other dangerous commands, and keys outside the application's prefix, with an error. Partly checks REDIS-6 and REDIS-7.
  • 6 Checkov Terraform policies: misconfiguration checks for managed Redis in Terraform, such as CKV_AWS_31 (ElastiCache in-transit encryption with an auth token), CKV_AZURE_89 (Azure Cache for Redis without public network access), CKV_AZURE_91 (only the TLS port), CKV_GCP_95 and CKV_GCP_97 (Memorystore AUTH and in-transit encryption), CKV_AWS_134 (ElastiCache automatic backups) and CKV2_AWS_50 (ElastiCache Multi-AZ automatic failover). Partly checks REDIS-10, REDIS-11, REDIS-12 and REDIS-14.

Tutorials and further reading