Redis Hash Datatype are similar in C# world as Dictionary e.g Dictionary<string, string>. Just like in C# redis stores map of attributes using key value pair. One thing to note is in Redis a Hash both the field name and the value are strings. Therefore, a Hash Datatype is a mapping of a string to a string.
Memory Optimized
Redis Hashes are memory-optimized. Their optimization is based on the hash-max-ziplist-entries and hash-max-ziplist-value configurations. Internally in Redis, a Hash can be a ziplist or a hash table. So what does it mean for a ziplist? Basically it is designed to be memory efficient dually linked list and integers are stored as real integers rather than a sequence of characters.
Speed
Something to note is a hashtable in Redis has a constant time lookup, the down side is it is not memory optimized, while the ziplist is memory optimized but the lookup time are not constant. To read more on Redis Memory Optimization please view Redis documentation.
So which one is used when?
- The ziplist is by default when the number of fields do not exceed the configuration ones in hash-max-ziplist-entries
- The hashtable is used when a the size or any of its values exceeds
Redis Hash Datatype – Operations
- HSET: Sets the value of a field for a key O(1).
- HGET: Gets the value of a field for a key O(1).
- HLEN: Gets the number of fields for the key O(1).
- HMGET: Gets the values for the fields for a key O(N), where N is the number of fields and O (1) if N is small.
- HMSET: Sets multiple values for respective fields for a key O(N), where N is the number of fields and O (1) if N is small.
- HGETALL: Gets all the values and fields for a key O(N). Where N is the size of the hash
- HKEYS: Gets all the fields in the Hash for the key O(1).
- HEXISTS: Checks for the existence of a field for a key O(1).
- HVALS: Gets all the values in the Hash for the key O(N), where N is the number of fields and O(1) if N is small.
- HSETNX: Sets the value against the field for the key provided the field does not exist O(1).
- HDEL: Deletes the fields for a key O (N), where N is the number of fields and O(1) if N is small.
- HINCRBY: Increments the value (provided the value is an integer) of a field for a key O(1).
- HINCRBYFLOAT: Increments the value if value is a float of a field for a key O(1).
C# code using Redis Hash Datatype
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
var hashKey = “hashKey”; HashEntry[] redisBookHash = { new HashEntry(“title”, “Redis for .NET Developers”), new HashEntry(“year”, 2016), new HashEntry(“author”, “Taswar Bhatti”) }; redis.HashSet(hashKey, redisBookHash); if (redis.HashExists(hashKey, “year”)) { var year = redis.HashGet(hashKey, “year”); //year is 2016 } var allHash = redis.HashGetAll(hashKey); //get all the items foreach (var item in allHash) { //output //key: title, value: Redis for .NET Developers //key: year, value: 2016 //key: author, value: Taswar Bhatti Console.WriteLine(string.Format(“key : {0}, value : {1}”, item.Name, item.Value)); } //get all the values var values = redis.HashValues(hashKey); foreach (var val in values) { Console.WriteLine(val); //result = Redis for .NET Developers, 2016, Taswar Bhatti } //get all the keys var keys = redis.HashKeys(hashKey); foreach (var k in keys) { Console.WriteLine(k); //result = title, year, author } var len = redis.HashLength(hashKey); //result of len is 3 if (redis.HashExists(hashKey, “year”)) { var year = redis.HashIncrement(hashKey, “year”, 1); //year now becomes 2017 var year2 = redis.HashDecrement(hashKey, “year”, 1.5); //year now becomes 2015.5 } |
For the code please visit
https://github.com/taswar/RedisForNetDevelopers/tree/master/4.RedisHashes
So this covers the basic usage of Redish Hash Datatype, in the next blog post I will cover using List Datatype.
For previous Redis topics

Leave a Reply