Search code examples
djangoamazon-web-servicesredisamazon-elastic-beanstalkamazon-elasticache

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.


Solution

  • 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
          }
        }
      }
    }