I am trying to read data from the key(e.g. "user") from my Redis DB with two APIs parallelly. One of the API is getting the expected value and other is getting NULL in response. what is the reason and solution for this?
We have a single Redis database, we storing user information in a key named as user_idNumber(user_01)
but when accessing this key from two different APIs at the same time. Redis putting a read lock on the key and one of the API getting null as the response.
export const getUserDataFromRedis = async (key: string) => {
return new Promise((resolve, reject) => {
redisConnection.select(UserConfig.REDIS_DB, (err) => {
if (err) reject(err)
console.log("this is the key : ", key);
redisConnection.get(key, (error, data: any) => {
if (err) reject(error)
console.log("this is data : ", data);
if (_.isNull(data)) {
console.log("NULL CONDITION MATCHED:",data);
setTimeout(() => {
redisConnection.get(key, (error, data: any) => {
if (err) reject(error)
console.log(`this is key and data if first time found NULL:${key},${data}`);
resolve(JSON.parse(data));
});
}, 1000);
} else {
resolve(JSON.parse(data));
}
});
})
})
}
question from:
https://stackoverflow.com/questions/66061382/when-i-am-trying-to-read-data-from-same-redis-key-with-two-different-api-at-same 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…