Skip to content

Redis

Introduction

Keep hot data in memory under keys that many servers can share; commonly used for caching, locks, counters, and sessions.

Why use it

Four of the most common uses:

  • Cache: store query results — see Caching.
  • Distributed lock: when multiple servers handle the same work (such as a scheduled job), use it to decide “who runs”.
  • Counter: likes, view counts, rate-limit tallies. The INCR command is atomic (safe under concurrency) — see Rate limiting.
  • Session: keep login state in Redis so any server that receives the user can recognize them.

How to do it

Persistence: does data survive a restart?

Mode Plain-language Trade-off
RDB snapshot Take a full photo every so often Fast restore, but you may lose the last few minutes
AOF log Record every write Loses less; files grow larger
Neither Pure cache Empty after restart; only for rebuildable data

Eviction: what happens when memory is full?

Memory is finite. Cap it with maxmemory, then pick a policy with maxmemory-policy. For pure cache, allkeys-lru (evict least recently used) is common. If the instance holds data you must not drop (locks, sessions), avoid policies that evict arbitrarily, and split cache from important data onto separate deployments.

Component comparison

Self-hosted Redis

Self-hosted open sourceOpen sourceOpsMedium

Install Redis yourself on a server or in a container: a fast in-memory "small warehouse" commonly used for caching, sessions and counters.

Best forProjects with server ops capability that want to control cost and configuration themselves.

Avoid whenNobody on the team can maintain it, or you need high availability but cannot build a cluster yourself.

Pros

Extremely fast reads and writes with rich features (caching, counters, leaderboards, queues, locks)

The largest community, with the most complete documentation and clients

Fully under your control, with no extra cloud service fees

Cons

Data lives in memory; the more memory, the pricier the server, and exceeding the limit triggers eviction or errors

Primary-replica failover, persistence configuration and backups are your responsibility

Exposing it to the public internet without a password gets it compromised, a very common incident

Official docs ↗

Valkey

Self-hosted open sourceOpen sourceOpsMedium

An open-source fork of Redis: after Redis changed its license, the community and several companies maintain it based on the original code, and usage is basically the same as Redis.

Best forProjects that want Redis-style usage but a purely open-source license.

Avoid whenYou depend on certain Redis-proprietary new features or commercial modules.

Pros

Usage is largely compatible with Redis, and most existing clients can connect directly

Uses a permissive open-source license, with no commercial licensing concerns

Increasingly offered by major cloud providers and Linux distributions

Cons

Newer than Redis, and new features may gradually diverge from Redis, so check each one's docs for details

Some Redis modules and add-on features may not be fully compatible

Tutorials and troubleshooting experience in China are still mostly about Redis

Official docs ↗

Cloud-provider managed Redis

Cloud-managedPay-as-you-goOpsLow

A ready-made Redis service from a cloud provider: buy an instance and connect, with backups, replication and upgrades handled by the provider.

Best forProduction environments where data cannot be lightly lost and the team has no ops staff.

Avoid whenAn extremely tight budget, or the project is just a small personal site.

Pros

No maintenance on your side, with built-in primary-standby failover and monitoring

Scaling and backups are basically a few clicks in the console

Access within the same cloud's internal network, with low latency

Cons

Continuous billing by tier, with prices rising noticeably as memory grows

Some advanced commands or settings are restricted by the provider and cannot be changed freely

Tied to the cloud provider; watch for differences when moving from local development to the cloud

Checklist

  • Every cache key has an expiry
  • Persistence strategy and maxmemory-policy are decided
  • Redis is not exposed to the public internet; set a password or access control — see Secrets management
  • Distributed locks have a timeout, and the business remains idempotent
  • The app can degrade when Redis is down instead of becoming wholly unavailable
  • Key names use a consistent prefix (for example business:object:id)