I want to make the key live longer if some condition met.
What I think is if I can query all the keys close to expire (like 10 minutes to expire), then I can do the query-check-activate intervally.
I seached but not found any clue, if you know how to achieve it, please tell me.
Any help will be highly appreciated!
Querying for keys based on their expiration time is not supported by Redis at the moment.
You can work around this by not using Redis' built-in expiration and managing it yourself, e.g. by using a Sorted Set to track expiration times and implementing passive and active expiration in your application.
Alternatively, you can use SCAN to crawl the keyspace, fetching TTLs and performing your evaluations.
Related
In terms of functionality, i can work around this limitation, but want to understand whether there an architectural reason why it is not supported out of the box?
PS: (this is not a question).
I have work around e.g.: call EXPIRE individually for each key which I am setting with mset. I don't know if that is any better than calling set individually for each key (in a pipeline if transaction does not matter), as set allows to specify TTL.
For anyone wondering, i did some benchmarks and calling set individually for each key is always faster. By approximately 15% for 2 keys and approximately 7% for 200000 keys. The benchmark was made with Go though and Redis calls were parallelized.
I'm getting over 10000 updates in 60 seconds in my Redis server and this triggers the background save which consumes resources.
I want to track the changed keys so that I can debug my app (which method causing this much change).
Is there a way to get updated keys?
While MONITOR is perfectly valid, it does include everything that gets sent to Redis. That means filtering read requests, pings, ...
Instead I recommend that you check the keyspace notifications documentation and configure your database the AK flags. By subscribing to the __keyspace:* pattern you'll be notified about every change to keys.
As I learned, it's only possible by using MONITOR command and figure out from output.
I'm looking for a way to maintain a hash map of values however I want the values (not the whole map) to expire individually after a a period of time.
The reason I'd like to accomplish this is because it drastically simplifies and minimizes my dependency on a database and time checking. Basically I'd like to define a list of resources to poll for 10 minutes after they are first requested.
So say my list is: ItemA, ItemB, ItemC.
ItemA is now older than 10 minutes, it should be knocked off the list so that the new list when I request it is only ItemB and ItemC.
I'm using a Node package with the standard redis library, so I'm looking for a way to do this easily with those packages. If not I'll fall back to the db method, but getting this working with Redis would be really great.
I'm already successfully using this to expire session tokens and from what I've read you can't do this directly with hashmap values. Just curious what some workarounds could look like.
Thanks.
I'm using MSETNX (http://redis.io/commands/msetnx) as a locking system, whereby all keys are locked only if no locks already exist.
If a machine holding a lock dies, that lock will be stuck locked - this is a problem.
My ideal answer would be that all keys expire in 15 seconds by default, so even if a machine dies it's held locks will auto-reset in a short time. This way I don't have to call expire on every key I set.
Is this possible in any way?
To build a reliable lock that is high available please check this document: http://redis.io/topics/distlock
The algorithm is still in beta but was stress-tested in a few sessions and is likely to be far more reliable than a single-instance approach anyway.
There are reference implementations for a few languages (linked in the doc).
Redis doesn't have a built-in way to do MSETNX and expire all keys together atomically. Nor can you set a default expiry tube for keys.
You could consider instead:
1. Using a WATCH/MULTI/EXEC block that wraps multiple 'SET key value EX 15 NX', or
2. Doing this using a Lua server-side script.
-User can prepare Post to be published for future.
So
Post.PostState is PostState.Scheduled.
Post.PublishDate is FutureDate
When futuredate comes PostState will be PostState.Published.
How can I implement this in Redis.
Sorry for duplication: I found that Delayed execution / scheduling with Redis?
Delayed execution / scheduling with Redis?
It seems an answer will be more related with code than db, so
c# reliable delayed/scheduled execution best practice
There is no scheduling as such, but you could set the values for both keys and put an expire on the scheduled date. Always lookup both keys and prefer the first. When the schedule expires, you will get back the actual as the first (and only) result.
You could also hide all that behind a lua script.
Sorry but using REDIS key expiration for scheduling won't work.
expiration can happen before, or very far in the future (eg. depending on available memory).
I think you might want to use another tool for delayed execution depending on you development platform. (eg. polling a REDIS queue, linux cron, timers, etc.)