agentleFS
salmandevsalmandev/everything-gemini/skills/redis-cache-patterns/SKILL.md

redis-cache-patterns

Skill2 starsChanged 28 days agoApache-2.0View on GitHub

Implements enterprise Redis caching strategies (cache-aside, write-through, stale-while-revalidate), distributed locking (Redlock), sliding-window rate limiting, and pub/sub streams.

  • Reads credentials

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    When calculating TTLs, always inject jitter (random variance) to prevent simultaneous mass-expiration of keys:

    line 17
  2. Very specific:

    Never release a Redis lock with a plain DEL command, as doing so can accidentally delete a lock held by another process if the operation …

    line 49
  3. Very specific:

    Structured Namespaces: Use colons for domain hierarchy (e.g., user:1042:profile, session:tok_9918).

    line 157

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Redis Cache Patterns & Distributed Primitives

This skill provides production-tested patterns for high-throughput Redis caching, concurrency coordination, and resilient distributed systems.


🚀 Key Patterns

1. Cache-Aside with Jitter (Preventing Cache Stampedes)

When calculating TTLs, always inject jitter (random variance) to prevent simultaneous mass-expiration of keys:

import { Redis } from 'ioredis';

const redis = new Redis(process.env.REDIS_URL!);

export async function getCachedEntity<T>(
  key: string,
  fetchFn: () => Promise<T>,
  baseTtlSeconds: number = 3600
): Promise<T> {
  const cached = await redis.get(key);
  if (cached) {
    return JSON.parse(cached) as T;
  }

  const freshData = await fetchFn();
  
  // Inject 10-15% random jitter to distribute TTL expiration spikes
  const jitter = Math.floor(Math.random() * (baseTtlSeconds * 0.15));
  const ttl = baseTtlSeconds + jitter;

  await redis.set(key, JSON.stringify(freshData), 'EX', ttl);
  return freshData;
}

2. Distributed Locking with Safe Lua Release

Never release a Redis lock with a plain DEL command, as doing so can accidentally delete a lock held by another process if the operation took longer than the TTL. Use atomic Lua verification:

import { randomUUID } from 'crypto';

const RELEASE_LOCK_SCRIPT = `
  if redis.call("get", KEYS[1]) == ARGV[1] then
    return redis.call("del", KEYS[1])
  else
    return 0
  end
`;

export async function withDistributedLock<T>(
  lockKey: string,
  ttlMs: number,
  task: () => Promise<T>
): Promise<T> {
  const lockToken = randomUUID();
  const acquired = await redis.set(lockKey, lockToken, 'PX', ttlMs, 'NX');

  if (!acquired) {
    throw new Error(`Failed to acquire distributed lock for key: ${lockKey}`);
  }

  try {
    return await task();
  } finally {
    // Atomically release only if the token matches
    await redis.eval(RELEASE_LOCK_SCRIPT, 1, lockKey, lockToken);
  }
}

3. Sliding-Window Rate Limiting (Sorted Sets)

Sliding-window rate limiters prevent burst exploitation around window boundaries:

export async function isRateLimited(
  identifier: string,
  limit: number,
  windowSeconds: number
): Promise<{ allowed: boolean; remaining: number }> {
  const now = Date.now();
  const clearBefore = now - windowSeconds * 1000;
  const key = `ratelimit:${identifier}`;

  const pipeline = redis.pipeline();
  pipeline.zremrangebyscore(key, 0, clearBefore);
  pipeline.zadd(key, now, `${now}-${Math.random()}`);
  pipeline.zcard(key);
  pipeline.expire(key, windowSeconds);

  const results = await pipeline.exec();
  const count = (results?.[2]?.[1] as number) || 0;

  return {
    allowed: count <= limit,
    remaining: Math.max(0, limit - count),
  };
}

4. Redis Streams with Consumer Groups

For reliable, decoupled background tasks without message loss:

// Producer: Append event to stream
export async function publishEvent(streamName: string, event: Record<string, string>) {
  return await redis.xadd(streamName, '*', ...Object.entries(event).flat());
}

// Consumer: Read events with acknowledgments
export async function consumeEvents(
  streamName: string,
  groupName: string,
  consumerName: string,
  handler: (id: string, fields: string[]) => Promise<void>
) {
  const response = await redis.xreadgroup(
    'GROUP', groupName, consumerName,
    'COUNT', 10,
    'BLOCK', 2000,
    'STREAMS', streamName, '>'
  );

  if (!response) return;

  for (const [, messages] of response as any[]) {
    for (const [id, fields] of messages) {
      await handler(id, fields);
      // Acknowledge processed message
      await redis.xack(streamName, groupName, id);
    }
  }
}

5. Memory & Performance Best Practices

  • Structured Namespaces: Use colons for domain hierarchy (e.g., user:1042:profile, session:tok_9918).
  • Use Hashes for Objects: Storing small objects in Redis HSET saves ~40% memory compared to JSON strings due to memory-efficient ziplist/listpack encodings.
  • Always Configure Eviction Policy: In redis.conf, set maxmemory-policy volatile-lru or allkeys-lru to avoid hard out-of-memory errors.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.