Memcached

From G1Joshi/Agent-Skills · View interactive version

Memcached is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Memcached in-memory key-value caching assistance covering slab allocation, multithreaded caching, and binary protocol. Use when caching database queries, session stores, or offloading read traffic.

How to use this skill

Skill file

Memcached

Memcached is a high-performance, distributed memory object caching system. It is simpler than Redis. It does arguably one thing: Key-Value caching of strings/objects in RAM.

When to Use

Quick Start

# Telnet interface
set mykey 0 60 4
data
STORED

get mykey
VALUE mykey 0 4
data
END

Core Concepts

Multi-Threaded Event Loop & Slab Allocation

Memcached allocates memory in fixed slabs to prevent operating system memory fragmentation:

Memory Pool (Slab Allocator)
  ├── Slab Class 1 (Chunk Size: 96 Bytes)  -> Stores small keys & tokens
  ├── Slab Class 2 (Chunk Size: 120 Bytes) -> Stores session snippets
  └── Slab Class N (Chunk Size: 1MB)       -> Maximum item size

Consistent Hashing Client-Side Routing

Clusters scale horizontally by having client drivers calculate server destinations using consistent hashing algorithms (Ketama):

// Memcached Consistent Hashing in Node.js
import Memcached from "memcached";

const memcached = new Memcached(
  ["10.0.1.1:11211", "10.0.1.2:11211", "10.0.1.3:11211"],
  {
    retries: 2,
    retry: 10000,
    remove: true, // Automatically remove failed nodes from ring
  },
);

// Set with expiration (seconds)
memcached.set("user:415:profile", JSON.stringify(userData), 3600, (err) => {
  if (err) console.error("Cache set failed:", err);
});

Atomic Binary Protocol Operations (CAS, INCR)

Guarantees thread-safe counters and optimistic concurrency control via Check-and-Set (CAS):

# Telnet / Binary Protocol command
# add <key> <flags> <exptime> <bytes>
<data>

add lock:daily_report 0 60 1
1

Common Patterns

Cache-Aside Pattern with Expiration

Problem: Database overloaded with identical repetitive reads for user profile data.

Solution:

Check cache first, fallback to DB, and populate cache with TTL:

import Memcached from "memcached";
const memcached = new Memcached("127.0.0.1:11211");

async function getUserProfile(userId) {
  const cacheKey = `user:${userId}`;
  return new Promise((resolve, reject) => {
    memcached.get(cacheKey, async (err, data) => {
      if (data) return resolve(data);
      const dbUser = await db.users.findById(userId);
      memcached.set(cacheKey, dbUser, 3600, (setErr) => {
        if (setErr) console.error("Memcached set failed", setErr);
      });
      resolve(dbUser);
    });
  });
}

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :---------------------------------------- | :--------------------------------------------------------------- | :--------------------------------------------------------------- |

| SERVER_ERROR object too large for cache | Item size exceeds default 1MB slab limit. | Compress data before caching or start daemon with -I 2m flag. |

| High cache eviction rate | Memory limit -m too low for active working set. | Increase allocated memory pool or optimize item TTL expirations. |

| Connection timed out | Memcached thread pool saturated or firewall blocking port 11211. | Monitor stats output and scale Memcached cluster instances. |

References

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.