The error you're experiencing, where Redis is randomly evicting keys, often arises because of memory limitations. Redis stores all data in memory for fast read and writes operations. However, when the memory limit is reached, Redis starts to evict keys to make room for new ones. The default eviction policy used by Redis is volatile-lru (Least Recently Used), which evicts keys with an expire set. If no keys are expired or no expiration is set, it can seem like Redis is "randomly" evicting keys.
There are several approaches you can take to resolve this issue. First, you could increase your server's memory so that more keys can be stored in Redis. However, this might not be a sustainable solution, especially if the amount of data you need to store continues to grow. Second, you could change your key eviction policy. Redis supports multiple key eviction policies, which determine how keys are selected for eviction when the maxmemory limit is reached. You could switch to "allkeys-lru" or "volatile-random", depending on your use case. Third, consider structuring your data more efficiently or deleting unnecessary data. For instance, consider using Redis' data types like hashes, lists, sets, and sorted sets to optimize memory usage. Lastly, review your application code to ensure keys have appropriate expiration times set, where applicable.