Redis how to reset timeout on cache hit? - redis

I am using redis for storing some data like a cache.
I am using EX 60 to store data only for 60s.
My question is:
Is there any way how to reset timeout on cache hit?
Example:
I create cache item at 12:00:00, key will expired in 12:01:00.
I will call get at 12:00:30, the timeout will be restarted to 12:01:30.
Iam usin node JS.
Thank you.

There's no such a command. But you can achieve it via multi or lua script.
Feature new command GETEX

Related

Redis taking too much time to execute simple commands during concurrent access

Background of problem
Hello all , i have made a project in golang gin , and I have integrated the redis Clusterclient in it using "github.com/go-redis/redis/v7"
P.S. Redis that I am using is a redis cluster hosted on AWS
the redis commands that I am using are simply redis.Get and redis.Set only.
Now I have made one API and used caching in it, and when I run it locally, response times are around 200 to 300ms, which is awesome (thanks to Redis)
Main Problem
now when I start doing the load testing on the same API with around 100 concurrent users , response time gets significantly increased ( around 4 seconds). I used spans to monitor the time taken by a different part of the code, and I got this
Getting from primary, getting from secondary are for the redis.Get command
Setting the primary , setting the secondary are for redis.Set
both commands are taking around 1 sec to execute, which is unacceptable,
can anyone please tell me some way, so that I can tackle this problem
and reduce the time for the redis commands to execute
Ok so I have solved this somehow.
Firstly I have updated my golang redis client library from go-redis/v7 to go-redis/v8 . And it made a significant improvement. I will advise everyone to do the same.
But still I am suffering from high response time , so the next step for me wa sto change the redis infra. Earlier I was using a redis cluster which only had 1 shard, but now I have moved to another redis having 4 shards..
And it made a huge difference , my response goes from 1200ms to 180ms. Kindly note that these response time are coming when I am doing a load testing with 100 concurrent users with an average of about 130rps
So in short upgrade your redis client , upgrade your redis infra

Do we need to reload the cache when the redis server is reconnection

I am new to Redis cache and this cache we are using in my node API application. On the start-up of this application, we are setting the values in the cache. Do we need to reload the values when the Redis server is restarting? Please help on this.
Thanks in advance
Redis has a configuration option that writes the content of the database to disk, and when it restarts, load the data from disk into the database. The details on this option are in the docs here: https://redis.io/topics/persistence
If you need some data to always be present in Redis, then you'll either need to implement persistence above, or do something like this in your app:
# when retrieving something from Redis cache
if (item_is_in_cache('my_key') { #inexpensive operation
retrieve_item_from_cache('my_key'); #inexpensive operation
} else {
store_important_data_in_cache(); #expensive operation
}
What this pseudo-code does is first check that the required data is in the cache, and retrieve it if it is. Checking and retrieving data from a Redis cache is an inexpensive operation, meaning the resources required are low. If the data isn't in the cache (ie, the Redis server recently started), we have to put the important data in the cache. This can be an expensive operation (more resources used than checking/retrieving data) depending on the amount of data required.

Redis - decrease TTL by single command

Let's say I have redis record with TTL = 1 hour.
Then some event occurs and I want to reset the TTL of this item to min(current-TTL, 5min). So it would decrease the TTL to 5 mins. if it is not already lower.
The underlying use-case is, there can be too frequent invalidation of the cache and the "old" cache is almost as good as the fresh, if it is not older then 5mins. from the first change.
I know I can fetch the TTL in one command and update it with second, but would prefer to set it by single command for various reasons. Is there a way?
Edit: There will be many keys I would need to decrease by single command. So I would like to avoid data round-trips between redis and client library for each record.
There is no single command to do that, but you can wrap the logic in a server-side Lua script and invoke that with a single command. Refer to the EVAL command for more information.

Is Redis data volatile?

I am trying to figure out something and I've been searching for a while with no results.
What happens if a Redis server loses power or gets shut down or something that would wipe the RAM? Does it keep a backup somewhere?
I am wanting to use Redis for a SaaS style app so if I go to app.com/usernamesapp it would use redis to verify usernamesapp exists and get the ID... At which point it would use MySQL for all the rest of the stuff... Reasons being I want to begin showing the page ASAP and most of the stuff is javascript so all the MySQL would happen after the fact.
Thanks
Redis can be configured to write to disk at regular intervals so if the server fails you wont lose your data.
http://redis.io/topics/persistence
From the Redis FAQ
Redis is an in-memory but persistent on disk database
So a critical failure should not result in data loss. Read more at http://redis.io/topics/faq

Records inserted in REDIS are not saved

When we add any records(in the form of hashes or sets), do we need to COMMIT in order to save them.
Is there a similar provision in REDIS?
I have created a virtual machine and I added records in form of hashes to my REDIS Cache on my m/c.
However,when I restart my Redis client and query for my records,they do not exist!
Sincerely appreciate anyone's reply on an urgent basis.
Thanks!
You can use AOF feature for better results
You can use SAVE as you apparently already discovered or you can use BGSAVE to run the saving task in the background and continue operating.
To see what happens "online" with your storage you can use "monitor" command. Just type it to console after redis-cli entering:
The sample:
user#user:~/Projects$ redis-cli
redis 127.0.0.1:6379> monitor
OK
1361101579.987123 "monitor"
1361102054.206754 "set" "keySample" "valSample" // in another console window I run "set keySample valSample"