🔴 Redis Beginner

What are HSET and HGET in Redis?

Answer

HSET key field value [field value ...] sets one or more field-value pairs in a Redis Hash. HGET key field retrieves the value of one field. HGETALL key returns all field-value pairs as a flat list. Hashes are ideal for representing objects: instead of serializing a user object to JSON (a String), you store each attribute as a Hash field — HSET user:42 name "Alice" email "alice@example.com" age 30. This lets you read or update individual fields without fetching and re-serializing the entire object. HINCRBY key field amount atomically increments a numeric field, useful for per-user counters. Redis internally uses a compact encoding (listpack) for small hashes, making them very memory-efficient for caching object data.