All guides
Term

Redis

Redis is an open-source, in-memory data structure store used as a distributed, key-value database, cache, and message broker.

Redis is an open-source, in-memory data structure store used as a distributed key-value database, cache, and message broker. Unlike traditional disk-based databases, it keeps the primary dataset in RAM, delivering sub-millisecond latency and high throughput for read and write operations. Beyond simple strings, it natively supports hashes, lists, sets, sorted sets, bitmaps, and streams.

How it works

Redis processes client commands using a single-threaded event loop, which avoids thread contention, complex locking mechanisms, and race conditions during data mutations. Background threads handle slow I/O tasks like unlinking large keys or writing to disk, keeping the main execution path fast and predictable.

Because RAM is volatile, Redis offers two configurable persistence mechanisms to survive restarts without sacrificing runtime speed:

  • RDB (Redis Database): Creates compact point-in-time binary snapshots at defined intervals, ideal for backups and fast disaster recovery.
  • AOF (Append-Only File): Logs every write operation received by the server, allowing finer durability guarantees by replaying commands on startup.

Common production workloads include user session storage, API rate limiting, real-time leaderboards, and caching expensive SQL queries. For distributed scale, Redis Sentinel automates failover across replicas, while Redis Cluster partitions data across multiple nodes.

Example

A typical caching workflow checks Redis before querying a slower primary database, setting a Time-To-Live (TTL) so stale records expire automatically. Using redis-cli, storing and retrieving a serialized user profile looks like this:

SET user:4092:profile '{"name":"Sam","role":"admin"}' EX 3600
GET user:4092:profile
TTL user:4092:profile

Basic GET and SET commands run in O(1) constant time regardless of total database size. The EX 3600 flag ensures the key deletes itself after one hour, preventing unbounded memory growth.

What matters when choosing a server or hosting

Because Redis is memory-bound, server sizing starts with RAM capacity rather than disk space. Provision enough memory to hold your active dataset plus overhead for copy-on-write forks during RDB snapshots or AOF rewrites—a practical rule is keeping baseline memory usage under 70% of total available RAM.

Single-core clock speed matters more than high core counts for raw command throughput, though extra cores help when using TLS termination or background persistence. Network latency between your application servers and the Redis host directly impacts response times, so place instances in the same private network or availability zone. Finally, disable Linux Transparent Huge Pages (THP) and set vm.overcommit_memory=1 on self-managed servers to prevent latency spikes during background saves.

Common mistakes

  • Exposing Redis directly to the public internet: Default installations historically prioritized local speed over perimeter security. Always bind Redis to private interfaces, require authentication via ACLs or passwords, and block port 6379 at the firewall.
  • Running KEYS * in production: Scanning the entire keyspace blocks the single-threaded event loop until completion, stalling all other clients. Use the non-blocking SCAN cursor command instead.
  • Omitting eviction policies: Without an explicit maxmemory-policy such as allkeys-lru, Redis rejects all new write commands once it hits its memory ceiling.

FAQ

Can Redis replace a primary relational database?

For specific workloads like session stores, real-time counters, or ephemeral queues, yes. However, it lacks multi-table joins, complex ad-hoc querying, and strict ACID durability guarantees across arbitrary transactions, so most architectures pair it alongside PostgreSQL or MySQL.

What is the difference between Redis replication and Redis Cluster?

Standard replication copies the entire dataset from one primary node to one or more read replicas for high availability. Redis Cluster automatically shards data across multiple primary nodes, allowing datasets to grow beyond the RAM limit of a single machine.

Why does Redis latency spike during backups?

Creating an RDB snapshot or rewriting an AOF file triggers a fork() system call. On memory-heavy instances with high write rates, copy-on-write page duplication consumes extra RAM and CPU, briefly slowing command execution.

All glossary terms

Glossary

Related terms

Full glossary

New guides every week.

A digest of the best pieces on servers, VPN and self-hosted software. No ads, no spam.

1 email a weekOne-click unsubscribeRU · EN
Only slipjar.app articles, nothing else.