Caching is one of the highest-impact performance improvements available. Choosing the right cache layer matters. Here's the current picture.
Without cache:
Request → API → Database → Result (200ms+)
With cache:
Request → Cache Hit → Result (1ms)
The difference is ~100-200x speed improvement for cached data.
Redis (Remote Dictionary Server) is far more than a cache — it's a data structure server.
# Strings
SET user:123:name "Vivek"
GET user:123:name
# Hashes
HSET user:123 name "Vivek" city "Delhi"
HGETALL user:123
# Lists
RPUSH tasks "task1" "task2"
LPOP tasks
# Sets
SADD online-users "user123"
SISMEMBER online-users "user123"
# Sorted Sets (leaderboards!)
ZADD leaderboard 1500 "user123"
ZRANK leaderboard "user123"
# Pub/Sub
SUBSCRIBE news-channel
PUBLISH news-channel "Breaking: New model released"Simpler, faster for pure caching:
Memcached wins if you need raw cache throughput with simple data types.
Use Redis. It's more capable, has better client libraries, persistence options, clustering, and the performance difference vs Memcached is negligible for most applications.
The only reason to choose Memcached is if you're already on it and it's working fine.
// Quick Redis setup with ioredis
import Redis from 'ioredis';
const redis = new Redis({ host: 'localhost', port: 6379 });
// Cache an expensive DB query
async function getUserWithCache(id: string) {
const cached = await redis.get(`user:${id}`);
if (cached) return JSON.parse(cached);
const user = await db.user.findUnique({ where: { id } });
await redis.setex(`user:${id}`, 3600, JSON.stringify(user)); // 1hr TTL
return user;
}