If I use Redis as key - value storage and update value several time with the same key. Can I retrieve previous version of value.
For example change value 5 time but retrieve version that was on 3 update
Thanks
Think you might be able to do this by crawling the AOF file for changes to that key, but there isn't anything supported in Redis to get historical values for keys.
Related
Remodeling Redis structure as NoSQL for a find by value solution suggests to record a single key set object?
My initial problem as "find all keys by value" in Redis. And for my surprise, that's not possible as it sounds, or it not a good option considering Redis use.
Searching in SO, I found this question Find key by value the answer for it is a remodeling structure approach.
My scenario
I have keys which are random UUID generated in code which store a boolean value. The UUID represents an request-id for an application which will return the success of this request after being processed. Then my application will store this request-id until it's not processed - there are some republishing feature if a 2 minutes timeout no response occurs for this request-id.
So, the data stored in Redis seems like:
44f672a0-36da-4906-9fa0-d3ee0b0f1989 true
33749e7e-5e62-4340-8914-cb9f6eed6111 false
and so on...
In some point of my code I should find all keys not processed. Which is a problem with this structure, I should have to scan all keys and for each key find the associated value. It's like a 2 querying per key - not a good approach.
Solution scenario
So, according to this question and answer I should store a single key named false with a set of values which are my request-id. So it would looks like:
false [33749e7e-5e62-4340-8914-cb9f6eed6111, 36b1eb8f-1576-49e7-a95a-ab852cc2624d ...]
So here we solved the find by value problem. Since I just have a single key false I'll found all not keys not processed.
But now I have to update this set key all the time I receive a success processing request instead of deleting a single key.
Does this updating set scenario could be a performance problem?
The idea is not to have a large unprocessed request-id. This set aims to be small in size and values
I think this is a design problem not a driver or code problem, but I'm using Java with Jedis to communicate with Redis.
I'm using Redis implementation of HyperLogLog to count distinct values for given keys.
The keys are based on hour window. After the calendar hour changes, I want to reset the count of incoming values. I don't see any direct API for 'clearing' up the values through Jedis.
SET cannot be used here because it would corrupt the hash. Is there a way to correctly "reset" the count for a given key?
Use the DEL command to delete the key, which will effectively reset the count.
I'm new to Redis and I want to use the following scheme:
key: EMPLOYEE_*ID*
value: *EMPLOYEE DATA*
I was thinking of adding a time stamp to the end of the key, but I'm not sure if that'll even help. Basically I want to be able to get a list of employees who are the most stale ie having been updated. What's the best way to accomplish this in Redis?
Keep another key with the data about employees (key names) and the update's timestamp - the best candidate for that is a Sorted Set. To maintain that key's data integrity, you'll have update it with pertinent changes whenever you update one the employees' keys.
With that data structure in place, you can easily get the keys names of the recently-updated employees with the ZRANGE command.
Have you tried to filter by expiration time? You could set the same expiration to all keys and update the expiration each time the key is updated. Then with a LUA script you could iterate through the keys and filter by expiration time. Those with smaller expiration time are those who are not updated.
This would work with some assumptions, it depends on how your system works. Also the approach is O(N) with respect to the number of employees. So if on one side you can save space, it will not scale well with the number of entries and the frequency of scan.
I don't want to use keys * command because it is O(N).
Is it possible to keep the newest Objects in redis?
Not using KEYS is definitely the way to go. Use a Sorted Set to store key names when you create them and set the score to the time of creation. You can the fetch key names by their creation time with ZRANGEBYSCORE and don't forget trimming the older keys from it using ZREMRANGEBYSCORE.
In a key-value persistance api I'm porting to Redis, I'm trying to implement a function that updates the time to live for a key. The original code stores ttl as a timestamp and # of minutes; the ttl is updated by writing a new timestamp (the key expires after timestamp + delta).
I've noticed that Redis provides a TTL command, but that only provides the time remaining.
What I'm wondering is if there is a way to retrieve the original TTL from Redis (set with EXPIRE, etc), or if I need to add a TTL meta field to the values I'm storing (like the original code does).
Edit:
I'm using Redis Server v2.4.10
Internally, Redis stores converts the TTL into a unix timestamp. See function expireGenericCommand in db.c. So, Redis cannot return the TTL you specified, simply because it does not store it in that format.
You will need to add a TTL meta field if it is important for your application.