Why Redis is Fast

Redis stores all data in RAM (memory) instead of on disk. RAM access is ~100,000× faster than disk access. A Redis GET operation typically completes in under 1 millisecond — far faster than any database query. This speed makes it ideal as a cache layer in front of slower data stores.

Redis Data Structures

TypeUse CaseKey Commands
StringCaching, counters, flags, tokensSET, GET, INCR, DECR, EXPIRE
HashUser profiles, session data, configHSET, HGET, HGETALL, HDEL
ListQueues, activity feeds, recent itemsLPUSH, RPUSH, LPOP, LRANGE
SetTags, unique visitors, friend listsSADD, SMEMBERS, SISMEMBER, SUNION
Sorted SetLeaderboards, priority queues, time-seriesZADD, ZRANGE, ZRANK, ZSCORE
HyperLogLogApproximate unique count (e.g. daily active users)PFADD, PFCOUNT
StreamEvent log, durable pub/sub, message queueXADD, XREAD, XRANGE

Common Redis Use Cases

1. Database Query Caching

Cache database results with TTL $cacheKey = "user:{$userId}:profile"; $cached = $redis->get($cacheKey); if ($cached) { return json_decode($cached); // cache hit — no DB query } $user = $db->query("SELECT * FROM users WHERE id = ?", $userId); $redis->setex($cacheKey, 3600, json_encode($user)); // cache for 1 hour return $user;

2. Rate Limiting

Simple sliding window rate limiter $key = "ratelimit:{$ip}"; $count = $redis->incr($key); if ($count === 1) { $redis->expire($key, 60); // reset window every minute } if ($count > 100) { return http_response_code(429); // too many requests }

3. Leaderboard (Sorted Set)

Real-time leaderboard // Update score $redis->zadd('leaderboard', ['user:123' => 4500]); // Get top 10 $top10 = $redis->zrevrange('leaderboard', 0, 9, ['WITHSCORES' => true]); // Get rank of a user $rank = $redis->zrevrank('leaderboard', 'user:123');

Redis vs Memcached

FeatureRedisMemcached
Data typesStrings, hashes, lists, sets, sorted sets, streamsStrings only
PersistenceRDB + AOFNone
ReplicationBuilt-in master-replicaNone built-in
ClusteringRedis Cluster (built-in)Client-side sharding only
Pub/SubYesNo
ThreadingSingle-threaded (I/O multiplexing)Multi-threaded
Memory efficiencySlightly lowerSlightly higher for simple strings

Redis Persistence Options

  • No persistence: Pure cache — data lost on restart. Fastest option.
  • RDB snapshots: Save dataset to disk every N seconds. Fast restarts, may lose recent writes.
  • AOF (Append-Only File): Log every write command. More durable — can lose at most 1 second of data (fsync every second). Larger files.
  • RDB + AOF: Best durability — use AOF for recovery, RDB for fast startup.

💡 Use Redis Cloud or Managed Redis

AWS ElastiCache (Redis), Azure Cache for Redis, and Redis Cloud are managed services that handle replication, failover, patching, and backups automatically. For most teams, the operational overhead of self-hosted Redis is not worth it unless you have specific requirements.

How We Research and Update This Guide

We test the underlying formula or workflow, compare outputs with reliable references, and revise examples whenever the page content changes.

  • The workflow or formula is tested directly in the tool and compared against independent reference examples.
  • Examples are kept practical so readers can verify the result without hidden assumptions.
  • Pages are revised whenever the interface, calculation flow, or surrounding guidance materially changes.

Frequently Asked Questions — Redis