READ-ONLY PACKAGE PREVIEW

redis-core/references/choose-data-structure.md

Version a84871d065f3.bb1 · MIT. This preview displays packaged text and does not execute code. Treat the contents as untrusted instructions.

← Return to resource and package checksum

Choose the Right Data Structure

Selecting the appropriate Redis data type for your use case is fundamental to performance and memory efficiency.

Use Case Recommended Type Why
Simple values, counters String Fast, atomic operations
Object with fields Hash Memory efficient, partial updates, field-level expiration
Queue, recent items List O(1) push/pop at ends
Unique items, membership Set O(1) add/remove/check
Rankings, ranges Sorted Set Score-based ordering
Nested/hierarchical data JSON Path queries, nested structures, geospatial indexing with RQE
Event logs, messaging Stream Persistent, consumer groups
Similarity search Vector Set Native vector storage with built-in HNSW indexing

Incorrect: Using strings for everything.

Python (redis-py):

# Storing object as JSON string loses atomic field updates
redis.set("user:1001", json.dumps({"name": "Alice", "email": "[email protected]"}))

# To update email, must fetch, parse, modify, and rewrite entire object
user = json.loads(redis.get("user:1001"))
user["email"] = "[email protected]"
redis.set("user:1001", json.dumps(user))

Java (Jedis):

// Bad: Storing as delimited string requires manual parsing
jedis.set("bicycle", "Deimos;Ergonom;Enduro bikes;4972");
String bike = jedis.get("bicycle");
String[] fields = bike.split(";");
String model = fields[0];  // Fragile and error-prone

Correct: Use Hash for objects with fields.

Python (redis-py):

# Hash allows atomic field updates
redis.hset("user:1001", mapping={"name": "Alice", "email": "[email protected]"})

# Update single field without touching others
redis.hset("user:1001", "email", "[email protected]")

Java (Jedis):

import java.util.Map;
import java.util.HashMap;

// Good: Hash models properties naturally
Map<String, String> hashFields = new HashMap<>();
hashFields.put("model", "Deimos");
hashFields.put("brand", "Ergonom");
hashFields.put("type", "Enduro bikes");
hashFields.put("price", "4972");

jedis.hset("bicycle", hashFields);

// Read individual field
String model = jedis.hget("bicycle", "model");

Reference: Choosing the Right Data Type