how to limit number of entries in a db of redis - redis

I am running a redis instance and want to limit the number of keys in an DB of the instance.
I checke the documentation and I saw that we can limit the memory of a complete instance .Any way to do that on DB level?
Thanks in Advance.

No, Redis does not have a function that can limit the amount of keys in the database.
The closest thing I can think of is checking the key number in your script, and the preventing the query of there is too many keys in the databse

Related

Is it ok to Redis auto increment id as a unique key?

I want to generate a unique id in my application . Is it ok to use redis auto increment id for this purpose ? Will it be a unique id even when in a cluster ?
yes, as the INCR documentation states it is an atomic operation and thus provides this guarantee
Redis can do id generation based on INCR command but may not be a good solution.
As Redis will not guarantee ACID for update(INCR), it may lose it consistency when Redis restart of failover. Both RDB and AOF are doing persistence in async way and data can be lost. For ID generator, it may generate duplicate IDs after restart(or failover in Cluster/Sentinel.
In your case if you don't care this scenario, or you think manual recovery is acceptable, you can use Redis as generator since it fast enough for most case(compare with MySQL or other database).
Or there are still some useful ID generation algorithm like SnowFlake which will guarantee the global incremental and no duplicate exists.

How many keys can be deleted in a single redis del command?

I want to delete multiple redis keys using a single delete command on redis client.
Is there any limit in the number of keys to be deleted?
i will be using del key1 key2 ....
There's no hard limit on the number of keys, but the query buffer limit does provide a bound. Connections are closed when the buffer hits 1 GB, so practically speaking this is somewhat difficult to hit.
Docs:
https://redis.io/topics/clients
However! You may want to take into consideration that Redis is single-threaded: a time-consuming command will block all other commands until completed. Depending on your use-case this may make a good case for "chunking" up your deletes into groups of, say, 1000 at a time, because it allows other commands to squeeze in between. (Whether or not this is tolerable is something you'll need to determine based on your specific scenario.)

How to get ALL LFU (Least Frequently Used) keys from Redis?

Is it possible to retrieve all LFU keys from Redis database? If yes, please advice with a sample command-line.
Thanks a million,
Mughrabi
You can use OBJECT IDLETIME to get the number of seconds for which a key is not requested by a read or write operation. Then sort the idletimes for all keys to find LFU keys. Redis doesn't ship with a command which can print LFU keys as such. You probably have to write a script to do this.
https://redis.io/commands/OBJECT

What is keyspace in redis ?

I am new to redis, I do not know the meaning of "keyspace" and "key space" in redis terminology which I encountered in redis official website. Can someone help me to clear that? Thanks.
These terms refer to the internal dictionary that Redis manages, in which all keys are stored. The keyspace of a Redis database is managed by a single server in the case of a single instance deployment, and is divided to exclusive slot ranges managed by different nodes when using cluster mode.
In a key-value database, all keys can be in one node or divided in multiple nodes. Suppose I am storing telephone dictionary as key-value store with name as key and phone number as a value. If I store names A-L on one node and M-Z on another node, I divide my database into two key spaces. When I run query to search number of Smith, I need to search only second key space or node. This divides the query on multiple nodes and divide the work giving faster result. This could be shared-nothing model of working.

ServiceStack.Redis SearchKeys

I am using the ServiceStack.Redis client on C#.
I added about 5 million records of type1 using the following pattern a::name::1 and 11 million records of type2 using the pattern b::RecId::1.
Now I am using redis typed client as client = redis.As<String>. I want to retrieve all the keys of type2. I am using the following pattern:
var keys = client.SearchKeys("b::RecID::*");
But it takes forever (approximately 3-5 mins) to retrieve the keys.
Is there any faster and more efficient way to do this?
You should work hard to avoid the need to scan the keyspace. KYES is literally a server stopper, but even if you have SCAN available: don't do that. Now, you could choose to keep the keys of things you have available in a set somewhere, but there is no SRANGE etc - in 2. you'd have to use SMEMBERS, which is still going to need to return a few million records - but at least they will all be available. In later server versions, you have access to SCAN (think: KEYS) and SSCAN (think: SMEMBERS), but ultimately you simply have the issue of wanting millions of rows, which is never free.
If possible, you could mitigate the impact by using a master/slave pair, and running the expensive operations on the slave. At least other clients will be able to do something while you're killing the server.
The keys command in Redis is slow (well, not slow, but time consuming). It also blocks your server from accepting any other command while it's running.
If you really want to iterate over all of your keys take a look at the scan command instead- although I have no idea about ServiceStack for this
You can use the SCAN command, make a loop search, where each search is restricted to a smaller number of keys. For a complete example, refer to this article: http://blog.bossma.cn/csharp/nservicekit-redis-support-scan-solution/