Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AlexandroPerez/b14c35b437cf1b477fbdbb41c73b5332 to your computer and use it in GitHub Desktop.
Save AlexandroPerez/b14c35b437cf1b477fbdbb41c73b5332 to your computer and use it in GitHub Desktop.
Explaining the code: Using hashes to abstract a very memory efficient plain key-value store on top of Redis

Explaining the code

Using hashes to abstract a very memory efficient plain key-value store on top of Redis

see: https://redis.io/topics/memory-optimization#using-hashes-to-abstract-a-very-memory-efficient-plain-key-value-store-on-top-of-redis

What I understood

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:

from this

original-key    value
object:1        "cat"
object:2        "dog"
...
object:103      "whale"

to this

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment