skip to Main Content

Is it possible to set size restriction for value in redis. For example, I want to store only values in which its size should be less than 2 MB to the redis key. value having size greater than 2 MB should be skipped.

keyName – sampleRedisKey_1

value for sampleRedisKey_1 – some value in which its size < 2 MB

keyName – sampleRedisKey_2

value for sampleRedisKey_2 – some value in which its size > 2 MB

In these two keys, sampleRedisKey_1 should be stored and sampleRedisKey_2 should not be stored.

Thanks in advance.

2

Answers


  1. If your redis version >= 4.0.0 then you may use memory usage, it returns the the value in bytes. In your application layer you may make comparison before setting the value, skip to another key if size exceeds.

    127.0.0.1:6379> set mykey mysmallvalue
    OK
    127.0.0.1:6379> memory usage mykey
    (integer) 61
    127.0.0.1:6379> set anotherkey 1
    OK
    127.0.0.1:6379> memory usage anotherkey
    (integer) 52
    127.0.0.1:6379>
    
    Login or Signup to reply.
  2. There is nothing like limits on size of individual Redis object or type, you have to handle that at application side. Otherwise use some Lua script to do that. However I prefer client side as it completely avoids sending data to Redis instance.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search