Skip to content

skillDatabasesredis

Redis Data Structures

Picks the right Redis structure, key naming and expiry for queues, counters, leaderboards, rate limits and sessions.

npx workspacetemplates add redis-data-structures --tool claude-code

Writes 1 file to .claude/skills/redis-data-structures/

.claude/skills/redis-data-structures/SKILL.md32 lines · 1.7 KBFile
---
name: redis-data-structures
description: Picks the right Redis structure, key naming and expiry for queues, counters, leaderboards, rate limits and sessions.
---

# Redis data structures

Choose by operation, set expiry by default, and make updates atomic.

## Steps

1. For each feature, state the operations needed (increment, pop oldest, top 10, check membership).
2. Match the structure:
   - Counter or rate limit: `INCR` with `EXPIRE` on first write, or a fixed window key like `rl:{user}:{minute}`.
   - Object with fields: `HASH` (`HSET`, `HINCRBY`), not a JSON string you rewrite whole.
   - Leaderboard: `ZSET` with `ZADD`, `ZREVRANGE` and `ZRANK`.
   - Unique items and membership: `SET`; counting huge uniques cheaply: `HyperLogLog`.
   - Work queue: `STREAM` with consumer groups and `XACK` rather than `LIST` when a lost job matters.
   - Session: `HASH` or string with a sliding `EXPIRE`.
3. Name keys as `app:entity:id:field`. Use hash tags (`{user42}`) when several keys must live on one cluster slot.
4. Give every cache and session key a TTL, with a little random jitter to avoid a stampede at expiry. Plan for eviction by choosing a `maxmemory-policy` (`allkeys-lru` for a cache, `noeviction` for a queue).
5. Make multi-step changes atomic with a Lua script or `MULTI`/`EXEC`. A lock uses `SET key value NX PX 30000` and releases only if the value matches.
6. Never use `KEYS *` in production; use `SCAN`. Watch for big keys with `MEMORY USAGE`.

## Rules

- Do not store data you cannot rebuild unless persistence is configured and tested.
- Bound the size of every list, set and stream (`XTRIM`, `LTRIM`).

## Report

A table of feature, structure, key pattern, TTL and the commands used.

What it does

Redis does well when the structure matches the job, and badly when everything is a string holding JSON. This skill maps each use case to the proper type and adds the expiry and memory care that keep an instance healthy.

It includes safe patterns for atomic updates, so two clients cannot corrupt a counter or double-process a job.

Example

you › Add a per-user rate limit of 100 requests per minute and a daily top-10 board

agent › Rate limit uses INCR on rl:{userId}:{epochMinute} with EXPIRE 70 set via a Lua script so the pair is atomic. The board is a ZSET lb:daily:2026-11-15 using ZINCRBY, trimmed to the top 1000 and expiring after 3 days.

Questions

Redis list or stream for a job queue?
A list is fine for disposable work. Streams add acknowledgements and retries, so use them when losing or double-running a job has a cost.
Why does my Redis memory not shrink after deleting keys?
The allocator may not return memory to the OS and big keys fragment it. Check fragmentation ratio and consider active defrag or a restart window.
  • Database Connection Pooling

    SkillDatabases

    Free
  • Database Migration Safety

    SkillDatabases

    Free
  • Database Schema Design

    SkillDatabases

    Free
  • MongoDB Schema Modeling

    Skillmongodb

    Free