Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up ElastiCache Redis with Elastic BeanStalk + Django

Another stackoverflow answer says you need to set up a elasticache.config file to create Redis servers with ElastiCache automatically.

However, can I just create a Redis instance on AWS (Elasticache) and add its endpoint into Django settings? Eg, with Django-redis:

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://<REDIS AWS ENDPOINT AND PORT HERE>",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    }
}

I suspect the above could cause trouble with multiple beanstalk server instances. Given this, I am tempted to use MemCache and not Redis, given that there is a Django package written explicitly for interfacing with AWS Elasticache for Memcache: django-elasticache.

Thanks, Andy.

like image 354
andyw Avatar asked Mar 12 '17 11:03

andyw


1 Answers

Short answer: yes.

Long answer: I have not used Elastic Beanstalk, however I can confirm that if you create a Redis instance (that is: cluster mode disabled) in ElastiCache it will work fine with django-redis. Just insert the primary_endpoint into the Django config you posted.

N.B. If you plan to use read replicas, set it up like this:

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": [
            "redis://<MASTER ENDPOINT>",
            "redis://<SLAVE ENDPOINT>",
        ]
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    }
}

If you spin up a Redis cluster however, you cannot use vanilla django-redis. You'll have to use use redis-py-cluster with it as described in this post. Replicated here:

CACHES = {
  'default': {
    'BACKEND': 'django_redis.cache.RedisCache',
    'LOCATION': 'redis://XXX.YYY.ZZZ.cache.amazonaws.com/0',
    'OPTIONS': {
      'REDIS_CLIENT_CLASS': 'rediscluster.RedisCluster',
      'CONNECTION_POOL_CLASS': 'rediscluster.connection.ClusterConnectionPool',
      'CONNECTION_POOL_KWARGS': {
        'skip_full_coverage_check': True # AWS ElasticCache has disabled CONFIG commands
      }
    }
  }
}
like image 72
Rutger de Knijf Avatar answered Nov 18 '22 11:11

Rutger de Knijf