Introduction

Redis, which stands for Remote Dictionary Server, is a source-available, in-memory storage system. It was developed and maintained by Salvatore Sanfilippo, starting in 2009. Redis is used as a distributed, in-memory key-value database, cache, and message broker, with optional durability. Redis holds all data in memory and due to its design, it offers low-latency reads and writes, making it particularly suitable for use cases that require a cache. Redis is the most popular NoSQL database and is used in companies like Twitter, Airbnb, Tinder, Yahoo, Adobe, and Amazon.

On March 20, 2024, the Redis team announced on their blog that Redis is no longer open source:

Beginning today, all future versions of Redis will be released with source-available licenses. Starting with Redis 7.4, Redis will be dual-licensed under the Redis Source Available License (RSALv2) and Server Side Public License (SSPLv1). Consequently, Redis will no longer be distributed under the three-clause Berkeley Software Distribution (BSD).

https://redis.io/blog/redis-adopts-dual-source-available-licensing/

Here are some alternatives to Redis:

Usage

The syntax of Redis is very simple:

Terminal window
# Setting the key mykey to the string value "Hello world"
SET mykey "Hello world"
# Retrieve the value associated with mykey
GET mykey
# Delete the key
DEL mykey
# Check if the key exists
EXISTS mykey
# Increment/decrement value
INCR days
DECR days
# Define temporary variables
SET sidebar "<div>Sidebar</div>" # set a key with TTL=-1 (no TTL)
TTL sidebar
EXPIRE sidebar 120

Data types

Redis supports different kinds of abstract data structures. Here are some of the core data types that Redis Community Edition implements (https://redis.io/docs/latest/develop/data-types/):

Note: if you want to have more information about some commands, you can visit the documentation here: https://redis.io/docs/latest/commands/

String

Redis strings are the most basic Redis data type, representing a sequence of bytes:

Terminal window
SET mykey "Hello World"
GET mykey

Hash

Redis hashes are record types modeled as collections of field-value pairs:

Terminal window
HMSET user:1000 username Alex birthyear 1999 verified 1
HGETALL user:1000

List

Redis lists are lists of strings sorted by insertion order:

Terminal window
RPUSH posts "Redis" # push on the right
LPUSH posts "Vault" # push on the left
LRANGE posts 0 -1 # show the elements of the list from x to y element
LLEN posts
LPOP posts # remove an element in the left
RPOP posts # remove an element in the right

Set

Redis sets are unordered collections of unique strings:

Terminal window
SADD users "Alex"
SADD users "Hugo"
SMEMBERS users
SREM users "Hugo"
SISMEMBER users "Alex" # check if an element exists
SUNION users users2 # merge 2 sets

Sorted Set

Redis sorted sets are collections of unique strings that maintain order by each string’s associated score:

Terminal window
ZADD myzset 1 "one"
ZADD myzset 2 "two"
ZRANGE myzset 0 -1 WITHSCORES

Stream

A Redis stream is a data structure that acts like an append-only log:

Terminal window
XADD mystream * sensor-id 1234 temperature 20.2
XRANGE mystream - +

Bitmap

Redis bitmaps let you perform bitwise operations on strings:

Terminal window
SETBIT mykey 7 1
GETBIT mykey 7

Bitfield

Redis bitfields efficiently encode multiple counters in a string value:

Terminal window
BITFIELD mykey INCRBY i5 100 1 GET u4 0

Geo-spatial

Redis geo-spatial indexes are useful for finding locations within a given geographic radius or bounding box:

Terminal window
GEOADD France 2.349014 48.864716 "Paris" 6.184417 48.692054 "Nancy"
GEODIST France Paris Nancy

Ecosystem

Redis has a rich ecosystem of modules that extend its capabilities beyond the core data structure server. Here are some of the key modules (https://redis.io/docs/latest/integrate/):

  1. RedisSearch: RedisSearch is a full-text search engine that provides querying, secondary indexing, and full-text search for Redis. It allows you to index your data in a variety of ways, and then search and aggregate it very quickly.
  2. RedisGraph: RedisGraph is a graph database module for Redis. It implements the Cypher query language, and uses a fast, memory-efficient graph algorithm library called GraphBLAS. RedisGraph represents connected data in a compact format that allows for efficient processing of complex graph queries.
  3. RedisBloom: RedisBloom is a module that provides probabilistic data structures to Redis. It includes structures like Bloom filters and Count-Min sketches, which allow you to handle tasks like membership queries and frequency counting in a memory-efficient way.
  4. RedisJSON: RedisJSON is a module that provides native JSON capabilities to Redis. It allows you to store, update, and fetch JSON values from Redis keys.
  5. RedisAI: RedisAI is a module that turns Redis into an AI server. It provides a set of commands for running popular deep learning models, and it allows you to execute AI operations in real-time.
  6. RedisTimeSeries: RedisTimeSeries is a module that provides time series data structures to Redis. It allows you to store, retrieve, and analyze time series data at high speeds.
  7. RedisGears: RedisGears is a serverless engine for transaction, batch, and event-driven operations. It allows you to execute scripts that can process data across all of Redis’ data structures.

When NOT to Use Redis

  • Big Data Storage: Redis stores data in RAM, making it less suitable for handling large datasets. For extensive data storage, it’s better to use Redis for caching purposes rather than as a primary database.
  • ACID Transactions: Redis does not fully support ACID (Atomicity, Consistency, Isolation, Durability) transactions. For more details, you can read my blog post on the CAP theorem.

When to Use Redis

  • Caching: Redis is excellent for caching, which can significantly speed up data access and improve application performance.
  • Session Management: Use Redis to handle sessions efficiently, providing fast and reliable session storage.
  • Queues: Redis is excellent for transferring data between servers using queues, facilitating smooth and reliable data handling in real-time applications.

Pricing

For additional details on pricing and licenses, please follow the provided links:

Try with Docker

You can easily try Redis with docker:

Terminal window
docker run --name my-redis -d redis
docker exec -it my-redis redis-cli
SET hello "Hello, Redis!"
GET hello
docker stop my-redis
docker rm my-redis

Recommended articles