NOAUTH Authentication required
Environment
- Node version: v14.16.0
- Redis Server version: 6.0.14
- Redis protocol version: 2
- Camaro Redis version: 2.4.0
Describe the bug
From time to time, I'm getting this error:
(node:5876) UnhandledPromiseRejectionWarning:
Redis Reply Error: NOAUTH Authentication required.
at Parser.parseSimpleError (C:\home\site\wwwroot\deps.js:382:14)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
To Reproduce
Code to reproduce the behavior:
const { ClientV2: Client } = require('@camaro/redis')
const client = new Client({
host: "<remote redis server host>",
password: "testpass"
});
async function test() {
await client.SET('bar', 'bar')
const reply = await client.GET('bar')
console.log(reply) // 'bar'
}
test().then(() => process.exit(0));
Additional context
I think, what happens is:
- I initialize the client - it starts connecting to the Redis server
- I start doing my stuff, and run some redis commands (before connection is established) - these go to the buffer
- Connection to server is established, authentication command is issued
- Client starts sending the commands to the server - but first commands are those from the buffer
It doesn't happen always, so probably it depends on the connection to redis server. Also, I am using SSL connection, SSL handshake also takes time.
some additional information:
- I tried adding a delay before sending commands, but that didn't help, so my assumption above is probably incorrect
- I think it might be related to the reconnects, but I am not sure
- I am now trying to periodically send
PINGcommand so that connection doesn't idle out (I am using Azure Redis Cache, and it is configured to 10 minutes idle timeout)
Just an update: adding PING didn't help, still getting same error from time to time (couple times per day). NOAUTH error always seems to happen on the first command sent to redis (although cannot tell with 100% certainty because stack trace is not showing where the error originates).
It seems the solution is, after all, to wait until the client connects and initializes. Maybe when I tried to solve this problem by adding a delay, delay was not big enough (it was 600ms).
Currently, I am using the following function to connect to Redis:
export function connectToCache() {
return new Promise((resolve, reject) => {
let connected = false;
const client = new ClientV2({ ... });
client.on("error", (e) => {
if (connected)
console.error("Redis error", e);
else
reject(e);
});
client.on("connect", () => {
connected = true;
resolve(client)
});
});
}
usage is like this:
const client = await connectToCache();
// do stuff with client
and the "NOAUTH" error doesn't happen anymore for me.
Even though the problem is now solved for me, I still think this issue needs to be addressed, since currently it is quite easy to use the client in incorrect way and start sending commands without waiting the client to connect.