EMZETT.
Login

Redis and in-memory key-value stores

In short: Redis stores data as key-value pairs in main memory and thus answers in fractions of a millisecond; typical for caches, sessions and counters.

In more detail: A key-value store is the simplest database form: a key has a value. Redis keeps data in RAM (optionally backed up to disk) and offers besides strings also lists, sets, hashes, sorted sets and streams. Very fast, but memory is limited and more expensive than disks.

In Depth

Typical uses

  • Cache: store results of expensive database queries for seconds to hours.
  • Sessions: users’ login state.
  • Counters and rate limiting: INCR counts accesses per time window atomically.
  • Leaderboards: sorted sets (ZADD, ZRANGE).
  • Queues and pub/sub: simple message distribution.
  • Distributed locks.

Examples

SET user:42:name "Mia" EX 3600   # expires after 1 hour
GET user:42:name
INCR views:homepage
LPUSH tasks "send-mail"
ZADD leaderboard 150 "Mia"

EX sets an expiry time (TTL) after which the key disappears automatically; ideal for caches.

Persistence and operation

Without measures data is lost on restart. Redis can write snapshots (RDB) or an append-only log (AOF). For resilience you use replication and Redis Sentinel/Cluster. As a cache data loss is usually tolerable; as the sole data source Redis is rarely suitable. Managed services (for example Upstash, ElastiCache) take over operation.

Notes

Choose sensible key names (area:id:field), always set expiry times for cache data, mind cache invalidation and protect the server (password, do not open to the internet). Alternatives: Memcached (simple values only), Valkey (free Redis fork), Dragonfly.

See also: Upstash Redis, Caching strategies, Rate limiting, NoSQL databases