REDIS waited for Delete appears to not be immediately effective
See original GitHub issueEnvironment:
- Node.js Version: v14.19.3
- Redis Server Version: redis-3.2.12-2.el7.x86_64
- Node Redis Version: ^2.8.0
- Platform: Centos 7
The code at issue runs every 5 minutes and about 7% of the time it would appear that the delete does not throw but is not effective since the key can still be obtained with a subsequent get.
I had thought that with this code the client and the server actions would be synchronous and therefore if the delete returned from the await then it must have been successful.
Any thoughts? - thanks.
Code at issue
` const redis = require(“./redis”); … await redis.setAsync(“HealthTestKey”, “HealthTestValue”);
const healthTestKey = await redis.getAsync("HealthTestKey");
if (healthTestKey !== "HealthTestValue") {
...
// not going here
} else {
// Delete key value pair
await redis.delAsync("HealthTestKey");
const healthTestKey = await redis.getAsync("HealthTestKey");
if (!healthTestKey) {
...
// not going here
} else {
...
// goes in here around 7% of the time - suggests the delete was not immediately effective
}
}
} catch (e) { … // does not go in here`
Client library code:
` const { promisify } = require(“util”);
const logger = require("./apiLogger");
const redis = require("redis");
require("dotenv").config();
const redisUrl =
process.env.REDIS_PASSWORD && process.env.REDIS_HOST
? `redis://:${process.env.REDIS_PASSWORD}@${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`
: undefined;
const redisClient = redis.createClient(redisUrl);
redisClient.on("error", async (e) => {
const { recordInternalError } = require("./healthCheck");
logger.error(`Failed to initialise REDIS Client: ${e.message} ${e.stack}`);
await recordInternalError(
{
email: "",
entryPoint: "/services/redis"
},
e
); });
module.exports = Object.assign(redisClient, {
getAsync: promisify(redisClient.get).bind(redisClient),
setAsync: async (key, value, flag, duration) => {
if (!key.length || !value) {
throw new Error(
REDIS setAsync has missing arguments: key: ${key} value: ${value}
);
}
return new Promise((resolve, reject) => {
try {
if (flag !== undefined && duration !== undefined) {
redisClient.set(key, value, flag, duration);
} else {
redisClient.set(key, value);
}
resolve(true);
} catch (err) {
reject(err);
}
});
},
delAsync: async (key) => {
return new Promise((resolve, reject) => {
redisClient.del(key, (err, result) => {
if (err) {
return reject(err);
}
return resolve(result);
});
});
},
expireAsync: promisify(redisClient.expire).bind(redisClient)
});
`
Issue Analytics
- State:
- Created a year ago
- Comments:6
This code dose not reproduce it:
Have you tried using the “MONITOR” command to check maybe there is a “SET” command from another client that gets executed in between the “DEL” and “GET” commands?
Huh, ran the same successfully. So hopefully it’s just a weird issue on my express flow that I can fix, but looking into it.
Thanks for the check!