This section explains that for simple key value pairs we can use a hash to get more memory efficiency. As an example, using the same approach mentioned in the redis topic in the link above, say we have 2,000 key value objects:
key value
object:0 {val}
object:1 {val}
...
object:1234 {val}
object:1999 {val}
The above data structure uses 2,000 keys, but we can reduce that to only 20 using hashes that can contain up to 100 fields:
key field value field value ... field value
object: 0 {val} 1 {val} ... 99 {val}
object:1 00 {val} 01 {val} ... 99 {val}
object:2 00 {val} 01 {val} ... 99 {val}
...
object:19 00 {val} 01 {val} ... 99 {val}
This can be achieved by splitting the key in two:
original-key value
object:1 "cat"
object:2 "dog"
...
object:103 "whale"
new-hash-key hash-field hash-value hash-field hash-value
object: 1 "cat" 2 "dog"
object:1 03 "whale"
Note that this approach requires you to split the id in your code to store and get the value. For example, i you want to get the value of object:103, you would ask redis for the value of field 03
in the key object:1
. To add a new object, say object:1024
, you have to make sure your code stores the value in field 24
of key object:10
.