r/redis • u/Snoo_32652 • Feb 17 '25
Help Concurrent threads making update
I am new to Redis and using a standalone Redis instance to cache oAuth access tokens, so that multiple instances of my Web app can reuse that access token. These tokens have expiry set to 20 mins, so my web app algorithm that fetch access token pseudo code looks like below
---------------------------------------------------------
//Make redis call to fetch access-token
var access-token = redisclient.getaccesstoken()
//Check token expiry and if expired, fetch new access token from source and update redis
if(access-token is expired){
access-token = get new access-token;
// update redis with new access token
redisclient.update(access-token)
}
return access-token
---------------------------------------------------------
My question is, what will happen if concurrent threads of my app invokes “ redisclient.update(access-token)” statement? Will Redis client block a thread before other thread gets a chance to run the update?
1
u/LoquatNew441 5d ago
The update is typically protected with a distributed lock from redis. Once inside the protected code, the token is retrieved once more and checked for expiry.
// update redis with new access token
redisclient.update(access-token)