$npx -y skills add redis/agent-skills --skill redis-coreCore Redis modeling guidance — choose the right data structure (String, Hash, List, Set, Sorted Set, JSON, Stream, Vector Set) and use consistent colon-separated key names. Use when designing a Redis data model, caching objects, deciding between Hash and JSON, building counters,
| 1 | # Redis Core |
| 2 | |
| 3 | Foundational guidance for modeling data in Redis. Covers data-type selection and key-name conventions — the two decisions that most directly drive memory, performance, and maintainability. |
| 4 | |
| 5 | ## When to apply |
| 6 | |
| 7 | - Caching objects, sessions, or per-user state. |
| 8 | - Counters, leaderboards, recent-items lists, unique-membership sets. |
| 9 | - Reviewing or refactoring Redis key names. |
| 10 | - Deciding between a Redis Hash and a JSON document for an entity. |
| 11 | |
| 12 | ## 1. Choose the right data structure |
| 13 | |
| 14 | Pick the type that matches the *access pattern*, not just the shape of the data. |
| 15 | |
| 16 | | Use case | Recommended type | Why | |
| 17 | |---|---|---| |
| 18 | | Simple values, counters | String | Atomic `INCR`/`DECR`, `SET`/`GET` | |
| 19 | | Object with independently updated fields | Hash | Per-field reads/writes, no whole-object rewrite | |
| 20 | | Queue, recent-N items | List | O(1) push/pop at ends | |
| 21 | | Unique items, membership checks | Set | O(1) `SADD`/`SISMEMBER`/`SCARD` | |
| 22 | | Rankings, score-based ranges | Sorted Set | Score-ordered; `ZADD`/`ZRANGE`/`ZRANK` | |
| 23 | | Nested / hierarchical data | JSON | Path-level updates, nested arrays, RQE indexing | |
| 24 | | Event log, fan-out messaging | Stream | Persistent, consumer groups | |
| 25 | | Vector similarity | Vector Set | Native vector storage with HNSW | |
| 26 | |
| 27 | **Common anti-pattern:** stuffing a flat object into a serialized string. Updating one field means fetch + parse + mutate + rewrite. Use a Hash instead. |
| 28 | |
| 29 | See [references/choose-data-structure.md](references/choose-data-structure.md) for full rationale and Python/Java examples. |
| 30 | |
| 31 | ## 2. Use consistent key names |
| 32 | |
| 33 | Use `colon-separated` segments with a stable hierarchy: |
| 34 | |
| 35 | ``` |
| 36 | {entity}:{id}:{attribute} |
| 37 | user:1001:profile |
| 38 | user:1001:settings |
| 39 | order:2024:items |
| 40 | session:abc123 |
| 41 | article:987:likes |
| 42 | game:space-invaders:leaderboard |
| 43 | ``` |
| 44 | |
| 45 | Rules of thumb: |
| 46 | |
| 47 | - **Lowercase, colon-separated.** No spaces, no mixed casing (`User_1001_Profile` is bad). |
| 48 | - **Keep keys short but readable** — keys live in memory and appear in every command. |
| 49 | - **Don't use full URLs or long strings as keys.** Extract a short identifier, or use a hash digest of the URL. |
| 50 | - **Prefix for multi-tenancy** (`tenant:42:user:7:cart`) so scans and ACLs can target a tenant cleanly. |
| 51 | - **Be consistent.** Pick one convention per service and apply it across all keys. |
| 52 | |
| 53 | See [references/key-naming.md](references/key-naming.md) for cleanup examples and edge cases. |
| 54 | |
| 55 | ## References |
| 56 | |
| 57 | - [Redis: Choosing the right data type](https://redis.io/docs/latest/develop/data-types/compare-data-types/) |
| 58 | - [Redis: Keys](https://redis.io/docs/latest/develop/use/keyspace/) |