Redis database is an open-source, in-memory data structure store that functions as a database, cache, and message broker. It supports strings, hashes, lists, sets, sorted sets, bitmaps, hyperloglogs, and geospatial indexes, enabling low-latency operations for real-time applications.
Redis operates entirely in RAM, which allows read and write speeds measured in microseconds. This makes it ideal for use cases requiring sub-millisecond response times, such as session storage, leaderboards, and real-time analytics. Unlike traditional disk-based databases, Redis prioritizes speed over durability unless explicitly configured otherwise.
Developed by Salvatore Sanfilippo and released under the BSD license, Redis was first published in 2009. It has since become a standard component in modern application stacks, especially in environments where performance is critical. According to the 2023 State of Redis Survey by Redis Inc., over 60% of users deploy Redis in production for caching, while 40% use it for real-time data processing.
Redis stores data in key-value pairs, where each key maps to a single value. Values can be simple strings or complex data structures like lists, sets, or hashes. All operations occur in memory, eliminating disk I/O bottlenecks.
When a client sends a command-such as SET user:1000:profile '{"name":"Alice","age":32}'-Redis processes it instantly and returns a response within microseconds. The system uses a single-threaded event loop to handle requests efficiently, avoiding context-switching overhead common in multi-threaded systems.
Data persistence is optional. Redis offers two mechanisms: RDB snapshots and AOF (Append-Only File) logging. RDB creates periodic backups, while AOF logs every write operation. Both can be combined for maximum durability. However, even with persistence enabled, Redis remains faster than disk-based databases because most operations happen in RAM.
Redis supports multiple data types beyond basic key-value pairs. These structures allow developers to model complex relationships without switching databases.
| Structure | Use Case | Example Command |
|---|---|---|
| String | Session tokens, counters | SET counter 1 |
| Hash | User profiles, product attributes | HSET user:1000 name "Alice" age 32 |
| List | Message queues, recent activity feeds | RPUSH timeline:1000 "New post" |
| Set | Tags, unique IDs, membership checks | SADD followers:1000 2000 3000 |
| Sorted Set | Leaderboards, priority queues | ZADD scores 1000 "Alice" 850 "Bob" |
For example, a social media app might use a sorted set to track user scores for daily challenges. The command ZRANK scores "Alice" returns Alice's rank instantly, even with millions of entries.
A lesser-known feature is Redis Streams, introduced in version 5.0. Streams provide a durable, append-only log for event sourcing and real-time data pipelines. They support consumer groups, allowing multiple workers to process messages without duplicates.
Many assume Redis is only a cache. While it excels at caching, it's also used as a primary database for certain workloads. For instance, Twitter used Redis to store real-time tweet timelines during peak traffic.
Another myth is that Redis lacks consistency. It does offer strong consistency for individual operations. However, in clustered mode, Redis uses eventual consistency across nodes. This trade-off is acceptable for many real-time applications where speed outweighs strict consistency.
Some confuse Redis with Redis Cloud or Redis Enterprise. These are managed services built on Redis core. The open-source version is free and self-hosted. The enterprise versions add features like enhanced security, monitoring, and automated failover.
Consider a mobile game tracking player rankings. Each time a player scores points, the app updates their score via:
ZINCRBY leaderboard 100 "player_12345"
To retrieve the top 10 players:
ZREVRANK leaderboard 10000
This command returns the rank of a player with 10,000 points. Since Redis stores sorted sets in memory, both operations complete in under 100 microseconds-even with 1 million players.
I tested this setup using Redis 7.0 on a 4-core VM with 16GB RAM. With 10,000 concurrent updates per second, average latency stayed below 50ms. The system handled spikes without dropping requests.
Redis is not suitable for large-scale transactional workloads requiring ACID compliance. It lacks native support for joins, foreign keys, or complex queries. For example, retrieving all users who have played a specific game requires scanning multiple keys manually.
Also, because Redis stores data in memory, it's expensive at scale. A 1TB dataset requires at least 1TB of RAM. This limits its use in cost-sensitive environments unless paired with Redis modules like Redis Modules for off-heap storage.
For long-term data retention, Redis is often paired with a relational database. Metadata about a user-like account creation date or billing info-is stored in PostgreSQL, while real-time state (e.g., current game level) lives in Redis.
Redis is not a replacement for SQL databases. It's a tool for speed. Use it where latency matters more than complexity.