What is a Redis hash?

A Redis hash is one of five basic data structures in Redis, along with strings, lists, sets, and sorted sets. But what is a Redis hash exactly, and how do Redis hashes work? Below, we'll discuss everything you need to know about Redis hashes.

What are Redis hashes?

Redis hashes are an implementation of the hash table or hash map data structure. Hash tables map unique keys to values. For speed and ease of access, each key has its own lookup value which is generated by a hash function.

For example, a Redis hash may represent a database of customers at a business. Each customer is identified by a unique key, such as a customer ID. The corresponding value may be an object storing information such as the customer's name, contact information, and recent purchases.

Redis hashes are also ideal for representing objects. For example, a User object can be represented with a Redis hash, where each key-value pair contains a different attribute (e.g. username, email address, age, etc.). Redis can store hashes with less than 100 key-value pairs in a very space-efficient manner.

How do Redis hashes work?

In Redis, hashes are maps between string keys and string values. Strings in Redis can be a maximum of 512 megabytes in size. In practice, this means that Redis strings can contain any type of data: text, numbers, images, serialized objects, and more. This makes Redis hashes a very flexible tool.

Redis hashes are capable of storing more than 4 billion key-value pairs, which gives most developers ample room to work. Despite this, hashes are considered a space-efficient data structure. Unlike other data structures such as arrays, which may have many unused entries, hashes only occupy the amount of space that is necessary to store the key-value pairs.

Some of the Redis hash commands are as follows:

  • HSET: Stores a given key-value pair in the hash.
  • HGET: Retrieves the value corresponding to the given hash key.
  • HDEL: Searches for a given hash entry and (if it exists) deletes it.
  • HEXISTS: Determines whether a given hash key already exists in the hash.
  • HGETALL: Returns the entire hash and its key-value pairs.
  • HKEYS: Returns all of the keys in the hash.
  • HVALS: Returns all of the values in the hash.

Below is a simple demonstration of creating and using a Redis hash. This first command creates a new hash:

hset hash-key sub-key1 value1

The hash-key field represents the unique key, the sub-key1 field represents the contents of the key, and the value1 field represents the contents of the value. If this initial command is successful (i.e. the key-value pair was created and did not already exist in the hash), Redis will return "(integer) 1".

Running the HGETALL command will now return the key-value pair:

hgetall hash-key

1) "sub-key1"
2) "value1"

Finally, the HDEL command will once again empty the hash:

hdel hash-key sub-key1

Hashing in Redis with Redisson 

Redis makes it easy to build hashes for large-scale systems and use cases. Despite the benefits of Redis, however, it's not immediately compatible with the Java programming language out of the box.

The good news: Java developers can lower their Redis learning curve by installing a third-party Redis Java client such as Redisson. Redisson includes many familiar Java objects and constructs, so that developers can spend less time learning the platform and more time actually building.

Among the many features of Redisson is the RMap interface, which is a distributed implementation of Java's Map and ConcurrentMap interfaces. Developers can use RMap to achieve all the functionality of Redis hashes within Java.

Below is an example of how to use RMap in Redisson to perform hashing in Redis:

RMap<String, SomeObject> map = redisson.getMap("anyMap");
SomeObject prevObject = map.put("123", new SomeObject());
SomeObject currentObject = map.putIfAbsent("323", new SomeObject());
SomeObject obj = map.remove("123");

// use fast* methods when previous value is not required
map.fastPut("a", new SomeObject());
map.fastPutIfAbsent("d", new SomeObject());
map.fastRemove("b");

RFuture<SomeObject> putAsyncFuture = map.putAsync("321");
RFuture<Void> fastPutAsyncFuture = map.fastPutAsync("321");

map.fastPutAsync("321", new SomeObject());
map.fastRemoveAsync("321");

Redisson includes several important features to improve the functionality and performance of RMaps:

  • Local caching: Redisson makes use of a "near cache" that can dramatically speed up read times and decrease network latency. The Redisson PRO edition performs read operations up to 45 times faster than the base version of Redisson.
  • Eviction policies: Users can set "time to live" and "max idle time" settings to ensure that the cache always contains up-to-date information.
  • Data partitioning: Data can be partitioned across multiple Redisson clusters, helping you scale database operations.
Similar terms