Can different keys have the same value?

434 views
Skip to first unread message

Peter

unread,
Jul 27, 2021, 11:17:10 AM7/27/21
to Redis DB
Hi, 

What will happens if I set multiple keys to the same value in Redis? For example, SET user01 kv, SET user02 kv. Will user01 and user02 points to the same kv, or two different kv? How many kv are in Redis? 

Thankd

Greg Andrews

unread,
Jul 27, 2021, 1:40:44 PM7/27/21
to Redis DB

I'm not a Redis Labs employee, but I've been on this list for a long time and can give you an answer.  In the reverse order that you asked them,

  • See this Google Groups post that gives the answer to the maximum number of keys that can be stored in Redis (more than 4 billion).
  • When two keys have the same value, there are two key names and two key values stored in Redis.
A long time ago there was a post to this group by a Redis developer suggesting that, for values that are integer numbers (0, 1, 2, etc.) keys containing the same value would "share" the value.  I.e., after 'SET KEYA 1', 'SET KEYB 1', Redis would store the two key names "KEYA" and "KEYB" but only one value "1", and both key names would point to the shared value.  This was done as a way to economize on memory.  However, it was only true for that one particular data type, and it's not a very important optimization because simple integer values are a small part of Redis use cases.  Also, as soon as one of the keys is changed (incremented, decremented, or a new value written), Redis must store a separate value for updated key, and now the optimization disappears.

Redis has seen major feature changes in the years since that post was made, so I would not be surprised if that small memory optimization is no longer present.

Reply all
Reply to author
Forward
0 new messages