question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ioredis does not detect dead connections

See original GitHub issue

I am using redis as a cache in a production environment. My redis-server is running on a different VM than my client running ioredis. My problem is that ioredis does not properly handle connections that are “lost” (by which I mean connections that are dead, but have not been properly closed). When this happens, the call-backs of all my .get, .set, etc… calls are only reached after about 11 minutes.

For example:

var Redis = require('ioredis');
var redis = new Redis();

redis.set('foo', 'bar');
// connection dies here.
redis.get('foo', function (err, result) {
  // Will only get here after ~11 minutes.
});

As you can imagine, this is unacceptable behavior for my cache as all the code accessing the cache will get stuck and all requests to my server will timeout.

The ‘close’, ‘end’ and ‘error’ events of ioredis are not called, because the tcp connection is not properly closed (no “FIN” tcp packets are sent). This is a common occurrence in a production environment and occurs in the following situations, among others:

  • the VM running redis-server is shut down
  • the NAT running between the client and the redis-server (e.g. a firewall or docker’s native NATing) eliminates the connection because it has been inactive for some time.

This does not happen if you run the client and redis-server on the same machine and just kill (either SIGINT or SIGKILL) the redis-server, as the OS will perform some clean-up and properly close the tcp connections between the redis-server about to be turned off and all connected clients (the ioredis object receives a ‘close’ event).

Coming back to the first situation where the connection is dead because of e.g. a failure of the VM running redis-server. All ioredis get() requests hang. After about 11 minutes, the OS detects that the connection looks dead, and kills it (seems to be related to the TCP keepalive parameters). At that point, the ‘close’ event will indeed be fired, and the callbacks of the .get requests will be reached.

What I would like to be able to do is to set a parameter that tells ioredis to give up on the redis.get after a certain number of milliseconds and return an error if not successful. E.g.

var redis = new Redis({requestTimeout: 100});
redis.get('foo', function (err, result) {
  // Will get here after at most 100ms, even if the connection died.
});

This would allow me to quickly rely on a fall-back solution (avoid accessing the cache). However, as far as I can tell, this is currently not possible with ioredis.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
luincommented, Aug 27, 2015

Haven’t come across this issue before. I’m going to look into it the weekend. Currently you can set the timeout of a request by:

redis.get('foo').timeout(100).then(function() {}).catch(function() {});

Possibly related issue #61.

0reactions
luincommented, Sep 9, 2015

Closing this issue since the keepAlive option is added in 1.8.0. Refer to #61 for any further updates about per-operation timeout.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redis (ioredis) - Unable to catch connection error in order to ...
Connection errors are reported as an error event on the client Redis object. According to the "Auto-reconnect" section of the docs, ioredis ......
Read more >
luin/ioredis - Gitter
I know that ioredis have a auto reconnect feature. How does that detect that the connection is broken? The application won't have that...
Read more >
Make sure Redis client library reconnects after Redis failures ...
Make sure Redis client library we use (see package.json) reconnects after Redis failures (recovery) The webapp/ws servers need to reconnect ...
Read more >
[ioredis] unhandled error event: error: connect econnrefused ...
7/site-packages/pycti/api/openctiapi_client.py", line 144, in init "OpenCTI API is not reachable. Waiting for OpenCTI API to start or check your configuration…" ...
Read more >
Redis client handling
When Redis can't accept a new client connection because the maximum number of ... and does not need iterating a second time to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found