Perf server side caching

From Intense-Visions/harness-engineering · View interactive version

Perf server side caching is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Design and implement server-side caching strategies — cache-aside, read-through, write-through, and write-behind patterns with Redis and Memcached, multi-tier caching architectures, serialization op

How to use this skill

Skill file

Server-Side Caching

Design and implement server-side caching strategies — cache-aside, read-through, write-through, and write-behind patterns with Redis and Memcached, multi-tier caching architectures, serialization optimization, and distributed cache consistency.

When to Use

Instructions

  1. Choose a caching pattern. Select based on read/write ratio and consistency requirements:

```

Cache-Aside (Lazy Loading):

Read: App → Cache → [miss] → DB → Write to Cache → Return

Write: App → DB → Invalidate Cache

Read-Through:

Read: App → Cache → [miss] → Cache fetches from DB → Return

Write: App → DB → Invalidate Cache

Write-Through:

Read: App → Cache → [miss] → DB → Return

Write: App → Cache → Cache writes to DB → Return

Write-Behind (Write-Back):

Read: App → Cache → [miss] → DB → Return

Write: App → Cache → Return (async write to DB later)

```

  1. Implement cache-aside pattern. The most common server-side caching pattern:

```javascript

async function getUser(userId) {

const cacheKey = user:${userId};

// Try cache first

const cached = await redis.get(cacheKey);

if (cached) {

return JSON.parse(cached);

}

// Cache miss: fetch from database

const user = await db.users.findById(userId);

// Write to cache with TTL

await redis.setex(cacheKey, 3600, JSON.stringify(user));

return user;

}

async function updateUser(userId, data) {

// Write to database

await db.users.update(userId, data);

// Invalidate cache (not update — avoids race conditions)

await redis.del(user:${userId});

}

```

  1. Choose between Redis and Memcached.

| Feature | Redis | Memcached |

| ----------------- | ----------------------------------------------------- | ------------------------------------ |

| Data structures | Strings, hashes, lists, sets, sorted sets, streams | Strings only |

| Persistence | RDB snapshots, AOF log | None (pure cache) |

| Replication | Built-in primary/replica | None native |

| Memory efficiency | Slightly higher overhead per key | Lower overhead, slab allocator |

| Max value size | 512MB | 1MB (default) |

| Multi-threaded | Single-threaded (event loop) + I/O threads (Redis 6+) | Multi-threaded |

| Use case | Complex data, pub/sub, persistence needed | Simple key-value, maximum throughput |

Default recommendation: Redis for most applications (richer feature set). Memcached for simple key-value with very high throughput requirements.

  1. Implement multi-tier caching. Combine in-process (L1) and distributed (L2) caches:

```javascript

const lruCache = new Map(); // L1: in-process, ~1000 entries

const LRU_MAX = 1000;

const LRU_TTL = 30000; // 30 seconds

async function getCached(key) {

// L1: check in-process cache (sub-microsecond)

const l1 = lruCache.get(key);

if (l1 && Date.now() - l1.timestamp < LRU_TTL) {

return l1.value;

}

// L2: check Redis (~0.5-1ms)

const l2 = await redis.get(key);

if (l2) {

const value = JSON.parse(l2);

lruCache.set(key, { value, timestamp: Date.now() });

if (lruCache.size > LRU_MAX) {

const firstKey = lruCache.keys().next().value;

lruCache.delete(firstKey);

}

return value;

}

return null; // Cache miss — caller fetches from DB

}

```

  1. Optimize serialization. JSON is human-readable but not the most efficient. For high-throughput caching:
  1. Set appropriate TTLs. TTL prevents stale data accumulation and bounds memory growth:
  1. Handle cache failures gracefully. The cache is not the source of truth — the database is:

```javascript

async function getUserWithFallback(userId) {

try {

const cached = await redis.get(user:${userId});

if (cached) return JSON.parse(cached);

} catch (error) {

// Cache is down — fall through to database

logger.warn('Cache unavailable, falling back to DB', { error });

}

return db.users.findById(userId);

}

```

Details

Cache Consistency in Distributed Systems

In a distributed system, cache consistency depends on the invalidation pattern:

Worked Example: Instagram Redis Caching

Instagram uses Redis for caching user timelines, storing 300 million user sessions with sub-millisecond read latency. Their architecture uses consistent hashing across a Redis cluster to distribute keys evenly. Each Redis node handles ~100,000 operations per second. They use Redis hashes for user profile data (storing fields individually rather than serializing the entire profile), enabling partial reads and updates without deserializing the full object. Session data uses Redis strings with 24-hour TTL for automatic cleanup.

Worked Example: GitHub Multi-Tier Caching

GitHub reduced database load by 50% by implementing a three-tier caching strategy for repository metadata: (1) per-process LRU cache (L1, ~5MB per process, 10-second TTL) handles repeated reads within a single request lifecycle, (2) Redis cluster (L2, 100GB total) handles cross-process caching with 5-minute TTL, (3) CDN edge caching (L3) handles public repository pages with 60-second TTL. A single page view for a popular repository like torvalds/linux hits L1 for repeated reads of the same repo metadata during template rendering, L2 for repo stats and contributor data, and L3 for the rendered HTML.

Anti-Patterns

Caching without TTL. Without TTL, cache entries live forever. Stale data accumulates, eventually consuming all available memory. Redis will evict keys using its eviction policy (default: noeviction, which rejects new writes when memory is full). Always set explicit TTLs.

Serializing entire ORM objects. ORM objects include metadata, lazy-loaded relation proxies, and internal state. A User ORM object might serialize to 5KB when the actual data is 500 bytes. Extract plain data objects before caching.

Using cache as primary data store without persistence. Redis can persist data (RDB/AOF), but cache instances are often configured without persistence for performance. If the cache restarts, all data is lost. Never use a non-persistent cache as the sole store for important data.

Not handling cache failures gracefully. If every request requires cache and cache goes down, the entire application fails. Implement circuit breakers and database fallback for when cache is unavailable.

Source

Process

  1. Read the instructions and examples in this document.
  2. Apply the patterns to your implementation, adapting to your specific context.
  3. Verify your implementation against the details and edge cases listed above.

Harness Integration

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.