How to find the age of the oldest item in Redis? - redis

Memcached provides max_age but couldn't find something similar for Redis. Is it that Redis doesn't support this kind of metadata?

Redis doesn't save the age of the keys. If you set a fixed TTL on all keys, you can sample a few keys and by viewing their TTL you can know their age (since all objects have the same TTL). This way you can statistically estimate the oldest key.

Unfortunately, there is no kind of max_age functionality in Redis, except if you set a TTL for each key.

Related

How to flush Redis keyspace of expired keys?

Backstory: The keyspace of the Redis database in question reports a large amount of expired keys and memory usage is maxed out. The application using this database is experiencing (rare) intermittent timeouts and I thought (in my limited knowledge) perhaps it is because Redis is having to eject expired keys each time a new key is created.
So to my question: how do I tell Redis to remove all the expired keys?
Secondarily -- is it possible to access/see expired keys with redis-cli?
Here's a slice of the INFO I'm looking at:
maxmemory_policy:allkeys-lru
expired_keys:24326586
evicted_keys:134022997
keyspace_hits:2684031719
keyspace_misses:186380210
slave_expires_tracked_keys:0
active_defrag_key_hits:0
active_defrag_key_misses:0
db2:keys=12994468,expires=3193,avg_ttl=1891176
Answer for myself, posterity, and any other Redis newbies out there. I was looking at the wrong "database". I was under the WRONG impression that Redis only had single table but looking at my question you see "db2". I searched into that and found that Redis can have up to 16 databases identified by a zero-based index. In this case:
SELECT 2
That selects "db2" and now doing a DBSIZE gives a more accurate output.
Oye -- so the problem is that the keys are still there! Otherwise when Redis expires a key it deletes it.
Whoops! I'm leaving my question because someone else might think to ask the same thing and be on the wrong route.

Redis -How to Get Last 1hour Data from redis

How to get all the keys/data stored in "redis" in the last one hour. I searched to found it out, but couldn't find a way. Is there any way to get this.?
Redis does not have a direct way to do this.
Depending on your use case, in increasing order of complexity -
You can manually add newly created keys to a set. The name of the set can include the timestamp. You can then query this set to find keys that have been modified
You can use redis keyspace notification to get notified of keys when they are changed. However, be aware that pub/sub notifications are "fire and forget" - so if your connection drops - you will lose some of the keys that were updated.
You can look at the AOF file and identify keys that have been created / modified. If you are using a cloud provider for redis - they may not provide access to the AOF file. Also, the AOF file doesn't have the timestamp, but the commands are in the order they were processed by redis.

how to check the expiration for all keys in a production environment for a Redis database

some keys do not have expiration date. Best way I can think of is to use "get *" (but there are millions of keys), store that and then use the TTL to see if has an expiry. If it doesn't then you set it.
Would this be the way to go?
I see a similar question here, that's not a unix command. How would I implement this in unix or maybe C#(I am using SSH Nuget package)?
2 ways to do that:
through the redis database notification, you can know the op on redis key, and by subscribe those messages, you can run ttl on those keys.
run a task that scan the redis key set and run ttl on them.
The main idea is that do it quick and dont block the redis.

Cache eviction on hashes on Redis

If I have several hashes on Redis, each with keys that expire in 24 hours, if memory runs out while using an eviction policy like allkeys-lru, will Redis remove an entire hash or single keys?
Redis only supports expiration at the key's level. Therefore, once set with a TTL, your key that contains a hashmap will be expired entirely (all child fields will be gone with it). The same goes for the other Redis data types (e.g. Sets and Lists).
If you use Hashes for storing key names that need to expire, simply set the TTL for each such key name individually instead of for the Hash's key.

How to find Keys with specific member value?

I'm new in Redis and use Redis 2.8 with StackExchange.Redis Libarary.
How can I write a KEYS pattern to get all keys with specific Hashed member value?
As I use StackExchange.Redis and want to get Keys with a pattern like this (when username is a member for a key): KEYS "username:*AAA*".
database.HashKeys("suggest me a pattern :) ")
I will call this method many times on HTTP user request to find out user's session data stored in Redis database, do you suggest a better alternative solution for this approach?
This simply isn't a direct fit for any redis features. You certainly shouldn't use KEYS for this - in addition to being expensive (you should prefer SCAN, btw), that scans the keys, not the values.