Redis: Is there a limit on the number of keys that I can store?

First context, I'm trying to use Redis as a storage in memory supported with persistence. I need to store a large number of objects (millions) in Redis Hash.

At the same time, I do not want my redis instance consuming too much memory. So I set the maxmemory property in redis.conf to 100 MB. I set the maxmemory policy as allkeys-random. Persistece mode is AOF, and fysnc is every second.

Now the iam problem is encountered, every time I try to store more than two hundred thousand objects in a hash, the hash gets reset (i.e. all existing key values ​​in the hash disappear). I confirm this using the hlen command in the hash in redis-cli.

Find below im object trying to save

public class Employee implements Serializable { private static final long serialVersionUID = 1L; int id; String name; String department; String address; /* Getters and Setters */ /* Hashcode - Generates hashcode (key) for each object */ public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((address == null) ? 0 : address.hashCode()); result = prime * result + ((department == null) ? 0 : department.hashCode()); result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } } 

Also, find below the code stored in redis (Im using Jedis to interact with Redis)

  JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "localhost"); Jedis jedis = (Jedis) jedisPool.getResource(); System.out.println("Starting...."); for(int i=0;i<1000000;i++) { /* Converting object to byte array */ Employee employee = new Employee(i, "Arun Jolly", "IT", "SomeCompany"); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(employee); byte[] value = byteArrayOutputStream.toByteArray(); /* Creating key in byte array format using hashCode() */ ByteBuffer buffer = ByteBuffer.allocate(128); buffer.putInt(employee.hashCode()); byte[] field = buffer.array(); /* Specyfying the Redis Hash in byte array format */ String tableName = "Employee_Details"; byte[] key = tableName.getBytes(); jedis.hset(key, field, value); System.out.println("Stored Employee "+i); } 

Am I missing something?

Does this mean that redis does not switch to disk after reaching maxmemory (does it try to save all the key values ​​in memory?) Does this mean that I should gradually increase the maxmemory limit in accordance with the increase in the number of key-value pairs that I should keep?

+7
redis jedis
source share
1 answer

Am I missing something?

Yes. Redis is pure memory in memory, with persistence settings. Everything should fit into memory.

Does this mean that redis does not switch to disk after reaching maxmemory.

Right

Is he trying to save all key values ​​in memory?

Keys and meanings, yes.

Does this mean that I need to gradually increase the maxmemory limit in accordance with the increase in the number of key-value pairs that I have to store?

You need to decide in advance how much memory you will allocate to Redis, yes.

If you are limited in memory, you will be better off working with disk storage.

+8
source share

All Articles