开发者

Jedis Storage Confusion, Is this issue from Redis or Jedis?

开发者 https://www.devze.com 2023-04-11 06:59 出处:网络
The following java code inserts a million integer pairs into redis. public class JedisInsertion { public static byte[] fromInt(int v) {

The following java code inserts a million integer pairs into redis.

 public class JedisInsertion { 
        public static byte[] fromInt(int v) { 
                return ByteBuffer.allocate(4).putInt(v).array(); 
        } 
        public static void main(String args[]) { 
                Jedis j = new Jedis("localhost"); 
                for (int i = 0;i<1000*1000;i++){ 
                   j.set(fromInt(i),fromInt(i)); 
                } 
        } 
开发者_Go百科} 

here is redis info output

... 
used_memory:89319664 
arch_bits:64 
... 

89319664 implies ~ 89 bytes per key value pair.

I was expecting something around 8 MB instead (4 bytes for key + 4 bytes value).

I also compiled redis in 32 bit mode (still running the test on a 64 bit machine).

The results for 32-bit version of redis:

used_memory: 68831664 => 68 bytes per key value pair.

Both results are several times higher than what I was expecting.

When I look at redis using CLI, I see a typical key would look like this: "\x00\x00\xc2\xff"

I appreciate your feedback

PS - I am using Redis 2.2.14 and Jedis 2.0 on a 64-bit machine

PSS - I also tried to store all the values in one hash as suggested by one of the comments, here is the code:

for (int i = 0;i<1000*1000;i++){
    j.hset("my-hash".getBytes(),fromInt(i),fromInt(i));
}

And here is the results:

used_memory_rss:84676608 (for 32bit build)
used_memory:105319712 (for 64bit build)

The results are even worse when I use a single hash.


68 byes per key/value type are correct. Redis does not internally store things as a plain text file, otherwise it would not be able to lookup it in a fast way, to have different types of objects, and so forth. There is an associated overhead. There are more informations about that in the documentation at Redis.io.


The fact that your key is a non-readable binary value is a decision made by Jedis, since Redis can use pretty much anything as a key.

Redis keys have expirations and other meta-data that takes up additional space. If you are concerned about space, consider storing all your values within a single Redis hash. There is significantly less overhead for key/value pairs within a hash than for standard key/value pairs.

http://redis.io/topics/memory-optimization

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号