memcached
/memcached
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.
- Author
- G1Joshi ↗
- Repository
- Agent-Skills ↗
- Licence
- MIT
- Last repository change
- 01.10.2026
Assessment and security
- Files in this package
- 1
- Scripts
- 0
- HTML comments
- 0
- URLs in SKILL.md
- 1
No signals detected. Repository changed on 01.10.2026, without references to retired models or APIs.
MIT, open: use and modify while retaining author attribution.
What it does and when to use it
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.
Original author description
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 it works
- When to Use
- Quick Start
- Core Concepts
- Common Patterns
- Best Practices
- Troubleshooting
- References
SKILL.md preview
---
name: memcached
description: 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.
---
# 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
- **High-Throughput In-Memory Key-Value Caching**: Caching database query results, HTML fragments, and session tokens with sub-millisecond latency.
- **Pure Ephemeral Caching**: Storing disposable data where eviction under memory pressure is acceptable (LRU eviction).
- **Multi-Threaded Multi-Core Scalability**: Maximizing cache operations on large multi-core servers using Memcached's native multi-threaded architecture.
- **Simple Key-Value Lookups**: Caching simple string and serialized objects where advanced data structures (Redis sets/hashes) are unnecessary.
## Quick Start
```bash
# 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:
```text
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):
```typescript
// 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):
```bash
# 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:
```javascript
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**:
- Use Ketama Consistent Hashing: Ensure clients hash keys across cluster nodes so adding/removing nodes evicts minimal keys.
- Set Appropriate Item Expirations: Always assign a TTL; prevent stale cache items from lingering indefinitely.
- Size Slabs to Match Item Distribution: Monitor `stats slabs` and tune `-f` (chunk growth factor) to minimize internal slab waste.
- Enable SASL Authentication: Secure Memcached instances with SASL authentication and private subnet firewall rules.
**Don't**:
- Store items larger than 1MB: Memcached defaults to a 1MB maximum item size; split large objects or compress them.
- Treat Memcached as a persistent database: Memcached is strictly in-memory; all data is lost upon server restart.
- Expose port 11211 to the public internet: Open ports risk catastrophic UDP reflection DDoS amplification attacks.
## 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
- [Memcached Wiki](https://github.com/memcached/memcached/wiki)
Versions
The database has no other skills named memcached.
Database snapshot: 13.11.2026 · package version 5671df8986bf (SHA-256 digest of the entire package)
Downloads in the last 30 days: 0 · ranking
Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.
Similar skills
- 90caching-strategy
Implement Redis/Memcached patterns and invalidation strategies. Use when: caching strategy. - 100managing-api-cache
Implement intelligent API response caching with Redis, Memcached, and CDN integration. Use when optimizing API performance with caching. Trigger with phrases like "add caching", "optimize API performance", or "implement cache layer". - 100managing-api-cache
Implement intelligent API response caching with Redis, Memcached, and CDN integration. Use when optimizing API performance with caching. Trigger with phrases like "add caching", "optimize API performance", or "implement cache layer". - 100memcached-config-helper
Configure with memcached config helper operations. Auto-activating skill for Backend Development. Triggers on: memcached config helper, memcached config helper Part of the Backend Development skill category. Use when configuring systems or services. Trigger with phrases like… - 100amazon-elasticache
Activate when developers have latent caching needs: slow API responses, database read bottlenecks, DynamoDB throttling or cost, RDS/Aurora scaling pressure, Bedrock latency or cost, or adding a cache; activate when working with Redis, Valkey, Memcached, or any in-memory data… - 90role-backend:caching-strategies
Implements caching solutions using Redis patterns (cache-aside, write-through, write-behind), Memcached, cache invalidation strategies, TTL design, cache stampede prevention, CDN configuration, and distributed caching. Use when optimizing read performance, reducing database…