Redis Core
Orientation fondamentale pour modéliser les données dans Redis. Couvre la sélection des types de données et les conventions de noms de clés — les deux décisions qui influencent le plus directement la mémoire, les performances et la maintenabilité.
When to apply
- Caching objects, sessions, ou per-user state.
- Counters, leaderboards, recent-items lists, unique-membership sets.
- Reviewing ou refactoring Redis key names.
- Deciding between a Redis Hash et a JSON document pour une entité.
1. Choose the right data structure
Pick the type that matches l'access pattern, pas seulement la forme des données.
| Use case | Recommended type | Why |
|---|---|---|
| Simple values, counters | String | Atomic INCR/DECR, SET/GET |
| Object with independently updated fields | Hash | Per-field reads/writes, no whole-object rewrite |
| Queue, recent-N items | List | O(1) push/pop at ends |
| Unique items, membership checks | Set | O(1) SADD/SISMEMBER/SCARD |
| Rankings, score-based ranges | Sorted Set | Score-ordered; ZADD/ZRANGE/ZRANK |
| Nested / hierarchical data | JSON | Path-level updates, nested arrays, RQE indexing |
| Event log, fan-out messaging | Stream | Persistent, consumer groups |
| Vector similarity | Vector Set | Native vector storage with HNSW |
Common anti-pattern: stuffing a flat object into a serialized string. Updating one field means fetch + parse + mutate + rewrite. Use a Hash instead.
See references/choose-data-structure.md for full rationale et Python/Java examples.
2. Use consistent key names
Use colon-separated segments with a stable hierarchy:
{entity}:{id}:{attribute}
user:1001:profile
user:1001:settings
order:2024:items
session:abc123
article:987:likes
game:space-invaders:leaderboard
Rules of thumb:
- Lowercase, colon-separated. No spaces, no mixed casing (
User_1001_Profileis bad). - Keep keys short but readable — keys live in memory et appear in every command.
- Don't use full URLs or long strings as keys. Extract a short identifier, ou use a hash digest of the URL.
- Prefix for multi-tenancy (
tenant:42:user:7:cart) so scans et ACLs can target a tenant cleanly. - Be consistent. Pick one convention per service et apply it across all keys.
See references/key-naming.md for cleanup examples et edge cases.