redis-py: timeout connecting to redis hosted on aws ElastiCache

Hi,

I have a redis server hosted by aws using aws ElastiCache. When I try to connect to it using a lambda and on my local machine, it gives ‘timeout connecting to server’ error. However, my colleague could connect to this redis using another client in PHP. I wonder what I did wrong.

try:
        redisClient = redis.Redis(host='***.use1.cache.amazonaws.com', port=6379, db=0, socket_timeout=10)
        print(redisClient)
        try:
            print(redisClient.ping())
            print(redisClient.set('foo','bar'))
            # r.get('foo')
        except Exception as e:
            print('get set error: ', e)
    except Exception as e:
        print('redis err: ', e)

// redisClient Redis<ConnectionPool<Connection<host=***.use1.cache.amazonaws.com,port=6379,db=0>>>

// get set error:  Timeout connecting to server for ping or set

Without setting the socket_timeout parameter, it will time out based on redis-py operation timeout: Error 60 connecting to ***.use1.cache.amazonaws.com:6379. Operation timed out.

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Comments: 17 (3 by maintainers)

Most upvoted comments

I ran into this issue as well, but from a Lambda. For me, there were a few problems that had to be ironed out

  • The lambda needs VPC permissions.
  • The ElastiCache security group needs an inbound rule from the Lambda security group that allows communication on the Redis port. I thought they could just be in the same security group.
  • And the real kicker: I had turned on encryption in-transit. This meant that I needed to pass redis.RedisClietn(... ssl=True). The redis-py page mentions that ssl_cert_reqs needs to be set to None for use with ElastiCache, but that didn’t seem to be true in my case. I did however need to pass ssl=True.

It makes sense that ssl=True needed to be set but the connection was just timing out so I went round and round trying to figure out what the problem with the permissions/VPC/SG setup was.

When I try to connect to it using a lambda and on my local machine

  • You can only connect to elasticache via a machine running in the same region. It’s in the docs somewhere.

This is happening randomly. Is there are way to tell redis-py to reconnect on connection timeouts with some backoff strategy?

@chayim , do you by any chance know how to make Celery use retry_on_timeout setting by any chance?

Yes you need to be in the same region. Also test with the redis-cli too, to rule out firewall rules, routing etc.