iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Modules (Search, JSON)

Redis modules: RedisJSON, RediSearch, RedisGraph, RedisBloom, RedisTimeSeries. The optional powers that turn Redis into a Swiss army knife.

Redis — modules

EXAMPLE
# ===== Why modules =====
# Core Redis: strings, lists, hashes, sets, sorted sets, streams.
# Modules add: JSON, search, graph, probabilistic data structures, time series.

# Modules are loaded into the Redis server; clients call new commands.

# ===== Common modules =====
# RedisJSON       JSON.GET / JSON.SET / JSON.ARRAPPEND ...
# RediSearch      FT.SEARCH; full-text + aggregations + secondary indexes
# RedisGraph      GRAPH.QUERY; Cypher-like graph queries (NOTE: project archived 2024)
# RedisBloom      BF.* (bloom filter), CF.* (cuckoo), CMS.* (count-min sketch), TOPK.*
# RedisTimeSeries TS.ADD / TS.RANGE / aggregations for time series
# RedisGears      pipelines / triggers
# RedisAI         model inference (legacy; check current state)

# ===== Load a module =====
# At server start:
# redis-server --loadmodule /path/redisearch.so

# Or runtime:
MODULE LOAD /path/redisearch.so
MODULE LIST
MODULE UNLOAD search

# ===== RedisJSON =====
JSON.SET user:1 $ '{"name":"Alex","age":30,"tags":["vip"]}'
JSON.GET user:1 $.name
JSON.SET user:1 $.tags '[]'
JSON.ARRAPPEND user:1 $.tags '"beta"'
JSON.NUMINCRBY user:1 $.age 1

# Index JSON fields with RediSearch:
FT.CREATE idx:users ON JSON PREFIX 1 user: SCHEMA $.name AS name TEXT $.age AS age NUMERIC
FT.SEARCH idx:users '@name:Alex @age:[25 35]'

# ===== RediSearch =====
FT.CREATE idx:posts ON HASH PREFIX 1 post: SCHEMA title TEXT body TEXT tags TAG
HSET post:1 title 'Hello' body 'world' tags 'intro,redis'
FT.SEARCH idx:posts 'hello @tags:{intro}'

# Aggregations:
FT.AGGREGATE idx:posts '*' GROUPBY 1 @tags REDUCE COUNT 0 AS n SORTBY 1 @tags

# ===== RedisBloom (probabilistic) =====
BF.ADD seen:emails 'a@x.io'
BF.EXISTS seen:emails 'a@x.io'      # 1 (probably seen)
BF.EXISTS seen:emails 'new@x.io'    # 0 (definitely not seen)

CF.ADD cuckoo:items 'item1'         # cuckoo filter (supports delete)
CMS.INITBYDIM cms:counts 2000 10
CMS.INCRBY cms:counts a 1 b 2
TOPK.RESERVE topk:trending 5 200 5 0.9
TOPK.ADD topk:trending dog cat dog

# ===== RedisTimeSeries =====
TS.CREATE temp:sydney RETENTION 86400000 LABELS sensor temp city sydney
TS.ADD temp:sydney * 23.5
TS.ADD temp:sydney * 23.7
TS.RANGE temp:sydney - + AGGREGATION avg 60000

# ===== Where to get modules =====
# Redis Stack: a bundle of modules + Redis. Easiest install:
docker run -p 6379:6379 redis/redis-stack:latest

# ===== When modules win =====
# - JSON-centric apps with secondary indexes (RedisJSON + RediSearch)
# - Probabilistic counts at scale (Bloom, CMS, TopK)
# - Time series telemetry (TS)
# - You already run Redis and want fewer moving parts

# ===== When modules hurt =====
# - You need broad SQL-style queries (Postgres / OpenSearch wins)
# - Strict open-source license requirements (some modules are SSPL)
# - Self-hosted Redis without ops capacity for module upgrades

# ===== Patterns to internalise =====
# - Reach for Redis Stack for the modern bundle
# - RedisJSON + RediSearch covers a LOT of use cases
# - Bloom/CMS/TopK collapse expensive 'exact' counts into tiny memory
# - TS for cheap time-series; do not invent your own

# ===== Pitfalls =====
# - Mixing Redis OSS license (BSD) with module licenses (SSPL, RSAL) — check terms
# - RedisGraph archive notice (2024): pick an alternative for graph
# - Modules that change command set across versions; pin versions
# - Persistence + replication interactions with module data

Why it matters

Modules turn Redis into a Swiss army knife: JSON storage, full-text search, probabilistic counts, time series. Redis Stack is the easiest start. Reach for them when the workload fits and you want fewer moving parts; respect the licensing terms and pin versions across upgrades.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# Load on startup: loadmodule /path/redisearch.so
FT.CREATE idx ON HASH PREFIX 1 doc: SCHEMA title TEXT body TEXT
FT.SEARCH idx "@title:redis"
Try it Yourself »

Discussion

Loading…