Caching and search

Redis

Redis is a fast in-memory data store used for caching, queues, counters, sessions and leaderboards, with rich data structures.

What it is

Redis keeps data in RAM and serves it in micro- to milliseconds. It is more than key-value: it offers strings, lists, sets, hashes, sorted sets, bitmaps, HyperLogLog, streams and geo indexes.

It usually sits next to the main database as a cache, a session store, a rate-limit counter or a light queue. Data can be persisted with RDB snapshots and an AOF log, but Redis still works mainly from memory.

About the license. In 2024 Redis Ltd moved from BSD to RSALv2 / SSPLv1, which led to the Valkey fork. In 2025 AGPLv3 was added as an option for Redis 8, so the code is again available under an OSI-approved license.

Use it when

  • You need a cache in front of a slow database or an external API.
  • You store sessions, one-time tokens or counters with a TTL.
  • You build leaderboards, job queues or low-latency pub/sub.
  • You need atomic operations on counters and sets.

Avoid it when

  • It would be the only store of critical data you cannot lose: memory is expensive and persistence is weaker than in a database.
  • The data does not fit into RAM at a reasonable price.
  • You need complex queries, joins and SQL-level transactions.

Key concepts

TTL
Time to live of a key: after it expires Redis deletes the entry on its own.
Eviction
The policy for removing keys when memory runs out (for example allkeys-lru).
RDB / AOF
Two persistence modes: periodic snapshots and a log of every write.
Replication / Cluster
Replicas for reads and failover, Cluster for sharding data across hash slots.
Single-threaded commands
Commands run sequentially on one thread, so every single command is atomic.

Example

A session with a TTL, a counter and a leaderboard on a sorted set in redis-cli.

Cheat sheet

Redis cheat sheet
Command or codeWhat it does
redis-cliOpen the Redis console
SET user:1 "Ann" EX 60Set a value with a 60 second TTL
GET user:1Read a value
DEL user:1Delete a key
TTL user:1Seconds left before expiry
INCR visitsAtomically increment a counter
HSET user:1 name Ann age 30Write fields into a hash
LPUSH jobs a && RPOP jobsA list as a simple queue
ZADD board 100 annSorted set: a leaderboard with scores
SCAN 0 MATCH user:* COUNT 100Safe key iteration (instead of KEYS)
INFO memoryServer memory usage

How to learn it

  1. Run Redis in Docker and connect with redis-cli: try SET, GET, DEL, TTL.
  2. Learn the core structures: strings, hashes, lists, sets, sorted sets and when to pick each.
  3. Implement a cache-aside cache with a TTL in front of a database query and measure the speedup.
  4. Build a counter or rate limiter with INCR and EXPIRE, then a queue on lists.
  5. Try pub/sub and Streams for simple events between services.
  6. Understand persistence (RDB and AOF), eviction policies (maxmemory-policy) and why everything lives in memory.
  7. Connect Redis from an app through a client (ioredis, node-redis) and avoid common traps: huge keys, KEYS *.
  8. Read about Sentinel, Cluster and distributed locks before taking Redis to production.

Interview questions

  1. 1. Why is Redis so fast?

    Data lives in memory, structures are optimized for their operations, and a single thread runs commands without locking between them. The network protocol is also lightweight.

  2. 2. What is the difference between RDB and AOF?

    RDB takes periodic snapshots: compact, but a crash can lose the last minutes. AOF logs every write: safer, but the file is bigger and writes are slower. Many setups enable both.

  3. 3. What is a cache stampede and how do you avoid it?

    When a hot key expires, many requests hit the database at once. Fix it with a lock for recomputation, TTL jitter and refreshing the cache ahead of expiry.

  4. 4. Redis or Memcached?

    Memcached is simpler and only caches strings. Redis adds data structures, persistence, replication and pub/sub, so one service covers more tasks.

Interview questions from the bank

Comparisons with this technology

Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).