Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to store a small key-value list in Redis

Tags:

redis

I'm trying to use Redis as a primary database for a small game I'm making (mostly to mess around with programming and using Redis).

However I came across a scenario that I couldn't find an answer to:

I wish to store a list of the names of different maps that people can be on (not many of them) along with their id. Note: I never need to get the ID from the name.

The two ways I believe this can be done are either storing the information as a string or as a hash.

i.e:

1) String based:

  • set maps:0 "Main"
  • set maps:1 "Island"
  • etc (and maybe a maps:id to store an auto increment value)

2) Hash based:

  • hset maps "0" "Main"
  • hset maps "1" "Island"
  • etc

My question is which way seems the best. Given that there will never be that many maps I'm leaning towards the single hashed object. Partially because this provides a nice method to return all the maps in existence. But is there any particular reason that the string based queries would be more useful.

Hopefully you can give me some clear information.

Thank you, Pluckerpluck

like image 661
Pluckerpluck Avatar asked Oct 28 '25 04:10

Pluckerpluck


1 Answers

The String based values are actually discouraged because it consumes a lot more memory than a hash.

Redis optimizes small hashes and encodes them in a memory efficient manner. This encoding is called zipmap (or ziplist in redis 2.6). See http://redis.io/topics/memory-optimization, specially the section "Use hashes when possible".

like image 148
Sripathi Krishnan Avatar answered Oct 29 '25 21:10

Sripathi Krishnan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!