Redis
An in-memory data store, so fast that it is used as a cache, a session store, a rate limiter and a lightweight queue.
- Type
- In-memory key-value data store
- Created by
- Salvatore Sanfilippo, now Redis Ltd. (open source under AGPLv3 since Redis 8)
- Speed
- Usually well under a millisecond per operation
- In my stack
- Cache, rate limiting and background jobs
What it is
Redis keeps its data in memory instead of on disk, which makes reads and writes extremely fast. Values are stored under keys, and they can be more than plain text: lists, sets, sorted sets, hashes, streams and counters, each with its own atomic operations. Any key can be given an expiry time, after which Redis deletes it on its own.
It can save its data to disk to survive a restart, but it is usually used next to a main database, not instead of one. In 2024 Redis moved away from an open-source license and the community created Valkey, a fully open-source fork. Redis 8 is open source again under the AGPLv3, and both work the same way for the uses described here.
Why I use it
It takes load off the database. Data that is read often and changes rarely, such as sites, users or checklists, is served from memory instead of querying PostgreSQL every time.
Simple, reliable rate limiting. Atomic counters with an expiry make it easy to cap login attempts or API calls, which slows down brute-force attacks.
Shared state across instances. When the API runs as several copies on Kubernetes, sessions, locks and caches live in one place that all of them see.
Background work. Lists and streams make a lightweight queue for tasks like sending emails or generating reports, without adding a heavy message broker.
Where I use it
In the HR management system, Redis runs alongside PostgreSQL behind the Go backend. The rule I follow is simple: PostgreSQL is the source of truth for every employee record, leave request and audit trail entry, and Redis only holds what can be rebuilt at any time. Losing it would slow things down for a moment, never lose data.
Best use cases
- Caching the results of expensive queries or calls to external APIs.
- Rate limiting logins, password resets and public endpoints.
- Sessions and short-lived tokens, with automatic expiry.
- Queues, locks and leaderboards: background jobs, preventing a job from running twice, live rankings.
When I’d use something else
Redis is not where business records should live: anything that must never be lost, or that needs relations and reporting, belongs in PostgreSQL. For a small app on a single server, an in-process cache is often enough and is one less service to run. And for large-scale event streaming between many systems, a dedicated broker like Kafka or RabbitMQ is a better fit.
A small example
Two patterns from a Go backend using the go-redis client: a cache in front of PostgreSQL, and a limit on login attempts.
// Cache-aside: try Redis first, fall back to PostgreSQL
func (s *Store) Site(ctx context.Context, id string) (Site, error) {
var site Site
if b, err := s.rdb.Get(ctx, "site:"+id).Bytes(); err == nil {
if json.Unmarshal(b, &site) == nil {
return site, nil
}
}
site, err := s.db.SiteByID(ctx, id)
if err != nil {
return site, err
}
b, _ := json.Marshal(site)
s.rdb.Set(ctx, "site:"+id, b, 10*time.Minute) // expires on its own
return site, nil
}
// At most 10 login attempts per email every 15 minutes
func (s *Store) AllowLogin(ctx context.Context, email string) bool {
key := "login:" + email
n, _ := s.rdb.Incr(ctx, key).Result()
if n == 1 {
s.rdb.Expire(ctx, key, 15*time.Minute)
}
return n <= 10
}