Redis hashes

Introduction to Redis hashes

Redis hashes are record types structured as collections of field-value pairs. You can use hashes to represent basic objects and to store groupings of counters, among other things.

Examples

  • Represent a basic user profile as a hash:
> HSET user:123 username martina firstName Martina lastName Elisa country GB
(integer) 4
> HGET user:123 username
"martina"
> HGETALL user:123
1) "username"
2) "martina"
3) "firstName"
4) "Martina"
5) "lastName"
6) "Elisa"
7) "country"
8) "GB"
  • Store counters for the number of times device 777 had pinged the server, issued a request, or sent an error:
> HINCRBY device:777:stats pings 1
(integer) 1
> HINCRBY device:777:stats pings 1
(integer) 2
> HINCRBY device:777:stats pings 1
(integer) 3
> HINCRBY device:777:stats errors 1
(integer) 1
> HINCRBY device:777:stats requests 1
(integer) 1
> HGET device:777:stats pings
"3"
> HMGET device:777:stats requests errors
1) "1"
2) "1"

Basic commands

  • HSET sets the value of one or more fields on a hash.
  • HGET returns the value at a given field.
  • HMGET returns the values at one or more given fields.
  • HINCRBY increments the value at a given field by the integer provided.

See the complete list of hash commands.

Performance

Most Redis hash commands are O(1).

A few commands - such as HKEYS, HVALS, and HGETALL - are O(n), where n is the number of field-value pairs.

Limits

Every hash can store up to 4,294,967,295 (2^32 - 1) field-value pairs. In practice, your hashes are limited only by the overall memory on the VMs hosting your Redis deployment.

Learn more

Feedback

If you've found issues on this page, or have suggestions for improvement, please submit a request to merge or open an issue in the repository.