What will happen if key expires when some process is reading the key? Does redis allows to read the key in this scenario? what will be its behavior/return value if key is deleted when process is reading the key?
Redis is single-threaded, so only one request is being processed at a time. If you're able to read it, the read will complete before the delete/expiration is performed. Any expiration, deletion, or alteration operations will happen sequentially after.
Related
I have a key (hash type) in redis
Key is
service_status:cluster_1
Value is like below
{
service_1: normal,
service_2: normal,
service_3: normal,
service_4: normal,
service_5: down
...
}
The system is a monitor system. This data is used to store the services status of one cluster.
There are thousands of services in the cluster, so thousands of update request may hit redis to update the same key at the same time.
My concern is how redis handle this? Will there be some lock since these update pointing to the same data?
Redis is single-threaded so there are no "parallel" updates, and therefore no need for locking. Operations in general and updates to a specific hash key, in particular, are executed one at a time.
Is there anyway to create a Redis database where keys HAVE TO expire after a certain time? I know I can expire an individual key using the EXPIRE command but since I am expiring every key after a certain time anyways, it would be nice to have this behavior specified in the Redis config file.
No, Redis (up to and including v3.2) does not provide the means for automatically setting the TTL of newly-created keys. You have to set it explicitly for each key you create.
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.
The documentation about Redis Keyspace Notifications http://redis.io/topics/notifications says near its end, that a key with timeout is removed from the database
"When the key is accessed by a command and is found to be expired."
..
Question: Is it enough to retrieve the very key, e.g. via KEYS *, or do I have to access the content the key refers to?
Background: The second process I omitted (the .. above) is a probabilistic process, and the real deletion of an expired key may be delayed, and thus the delivery of the EXPIRED event. I want to ensure the notification is given to a subscriber, so just accessing the keys would be easiest.
Redis implements a logic of periodic checking of keys for expiry and picks a number (100) of keys and checks them for their expiry.
What I understand is that your concerned with the fact that with above logic there would exist events which belong to expired keys which have not been deleted.
To avoid such a case checking the keys just for existence would delete them. Cost of REDIS calls should be kept in mind and hence a LUA script or bulk command should be designed which is invoked periodically and iterates a list of keys and run EXISTS command on them and cause automatic delete if they are expired.
To test this you would need a large dataset.
While executing the info command in redis-cli, all the information related to that server is listed. What is the purpose of "evicted_keys"?
Redis can be configured to automatically purge keys as necessary. If so configured, redis will only use a maximum amount of memory, and if it nears the limit, remove keys per some criteria. See Redis as an LRU cache by antirez.
It can be configured to only remove keys that have an expiry time, or all keys. It can remove keys soon to be expired, last recently used keys, or random keys.
evicted_keys in the info is the number of keys that have been evicted (removed).