Learn Labs
Data Structures

Specialized Types

HyperLogLog, geo, JSON, search, time series, Bloom filters, and vector sets

Beyond the five core types, Redis has types that trade exactness for tiny memory (HyperLogLog, Bloom filters), and, since Redis 8, the formerly separate modules built in: JSON, a query engine, time series, and vector similarity. Confirm they're loaded:

MODULE LIST
# timeseries, search, bf (Bloom), ReJSON, vectorset

HyperLogLog: count unique things in 12 KB

A HyperLogLog estimates the number of distinct elements with a standard error of 0.81%, using at most 12 KB, whether you add a thousand items or a billion.

PFADD app:visitors:today user:17 user:942 user:17duplicates don't countPFCOUNT app:visitors:today~2452 (seed.py added 5000 picks of 3000 IDs)PFCOUNT app:visitors:mon app:visitors:tueunique across both daysPFMERGE app:visitors:week app:visitors:mon app:visitors:tue ...

Storing every visitor ID in a set would cost tens of MB per day at scale; the HyperLogLog is 12 KB per day, forever. The catch: you can count but never list members, and you can't remove one.

Geospatial

Geo commands store longitude/latitude in a sorted set (the score is a 52-bit geohash), so they get range queries for free:

GEOADD app:stores -0.1276 51.5072 london 2.3522 48.8566 parisGEODIST app:stores london paris km343.6268GEOSEARCH app:stores FROMLONLAT 0 51 BYRADIUS 500 km ASC WITHDISTGEOSEARCH app:stores FROMMEMBER london BYBOX 1000 1000 km

Typical uses: nearest stores, drivers within 2 km, delivery zones.

JSON

Native JSON documents with path-based reads and atomic updates of nested fields:

JSON.SET app:user:1 $ '{"name":"Ada","tags":["admin"],"address":{"city":"London"}}'JSON.GET app:user:1 $.address.city["London"]JSON.SET app:user:1 $.address.city '"Paris"'JSON.ARRAPPEND app:user:1 $.tags '"beta"'JSON.NUMINCRBY app:user:1 $.logins 1JSON.DEL app:user:1 $.tags[0]

Use JSON when objects are nested and you update parts of them. A flat object is still cheaper as a hash.

Search and query

The Redis Query Engine builds secondary indexes over hashes or JSON documents, kept up to date automatically on every write:

FT.CREATE idx:product ON HASH PREFIX 1 app:product: SCHEMA
name TEXT
category TAG
price NUMERIC SORTABLE

FT.SEARCH idx:product "@category:{peripherals} @price:[0 60]" RETURN 2 name price
# 2 results: Webcam (59), USB-C hub (39)

FT.SEARCH idx:product "keyboard"                     # full-text
FT.AGGREGATE idx:product "*" GROUPBY 1 @category REDUCE AVG 1 @price AS avg_price

This answers "find products in a category under $60" without maintaining your own sets of IDs per category and price band. It's useful, but it doesn't make Redis a relational database: indexes cost memory, and there are no joins.

Time series

TS.CREATE app:temp RETENTION 86400000 LABELS room kitchenkeep 1 day of dataTS.ADD app:temp * 21.5* = nowTS.RANGE app:temp - + AGGREGATION avg 600001-minute averagesTS.CREATERULE app:temp app:temp:hourly AGGREGATION avg 3600000automatic downsamplingTS.MRANGE - + FILTER room=kitchenquery across series by label

Good for application-level metrics or IoT readings that need sub-millisecond writes. For infrastructure metrics, Prometheus is the right tool.

Bloom and Cuckoo filters

A Bloom filter answers "have I seen this?" with no false negatives and a tunable false-positive rate, in a fraction of the memory a set would take:

BF.RESERVE app:seen 0.01 1000001% false-positive rate, for 100k itemsBF.ADD app:seen user:1BF.EXISTS app:seen user:11: probably seenBF.EXISTS app:seen user:20: definitely not seen

Classic uses: "has this user already seen this article?", "is this username taken?" before hitting the database, skipping cache lookups for keys that can't exist. Cuckoo filters (CF.*) also support deletion. There are also Count-Min Sketch (CMS.*, approximate frequencies) and Top-K (TOPK.*, heavy hitters).

Vector sets

Redis 8 adds vector sets for similarity search over embeddings:

VADD app:vec VALUES 3 0.1 0.2 0.3 item:1
VADD app:vec VALUES 3 0.9 0.1 0.0 item:2
VSIM app:vec VALUES 3 0.1 0.2 0.25 COUNT 1      # nearest neighbour: item:1

Real embeddings have hundreds of dimensions. The query engine's VECTOR fields offer the same capability inside a search index, combined with filters.

Lab

  1. PFCOUNT app:visitors:today and MEMORY USAGE app:visitors:today. Compare with what a set of the same IDs would cost.
  2. Build the idx:product index and search for @category:{audio}. Then HSET app:product:1007 name Speaker category audio price 49 and search again. The new product is indexed immediately.
  3. Create a Bloom filter for 1,000 items at 1%, add 1,000 items, then test 1,000 different items and count the false positives.

On this page