Skip to content
Redis

Hashes

Store object fields and values efficiently.

By EZ4Code Team
hashobjectfield

Code

# Set and get individual fields
HSET user:1 name "Alice" age 30 role "admin"
HGET user:1 name
HGETALL user:1

# Update fields atomically
HINCRBY user:1 age 1
HSETNX user:1 email "[email protected]"

# Multi-field get and delete
HMGET user:1 name age role
HDEL user:1 role

# Inspect
HKEYS user:1
HVALS user:1
HLEN user:1

# Iterate large hashes
HSCAN user:1 0 COUNT 10

Explanation

Redis hashes store multiple field-value pairs under a single key, perfect for representing objects without JSON encoding. HSET sets one or more fields atomically, and HINCRBY updates numeric fields. HSCAN iterates large hashes incrementally to avoid blocking the server with HGETALL.

More Redis Snippets