Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

node.js - When I am trying to read data from same redis-key with two different API at same time one of them giving NULL

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...