Cerin
Cerin

Reputation: 64739

How to use Elasticache with Django's MemcachedCache Backend

What's the correct way to use Amazon's Elasticache service (with the Memcached engine) with Django's MemcachedCache backend?

I have a local Memcached service running locally, which works fine with the Django setting:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
    }
}

I thought using Elasticache would be as simple as creating the Memcached cluster instance and then changing my setting to:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': 'instance-name.abcdef.cfg.use1.cache.amazonaws.com:11211',
    }
}

However, when I test this locally, the cache silently fails and doesn't successfully store anything.

What am I doing wrong? How do I get the MemcachedCache backend to show a real error message? Do I need to use a Elasticache-specific Django backend like this?

Upvotes: 4

Views: 5119

Answers (1)

dannosaur
dannosaur

Reputation: 2639

You're unable to connect to ElastiCache instances from outside of AWS's network. Even though your security groups might have exceptions in to allow traffic from your IP address (or the entire internet), AWS's network will not accept any traffic to it that does not originate from within their network.

This configuration is fine, however will only work from an EC2 instance.

Alternatively you can follow this guide (which also confirms my answer above) which basically involves you spinning up an EC2 instance who's IP address you will use in your CACHES configuration instead. This instance is configured to do NAT between incoming traffic on port 11211 and forward it onto your ElastiCache node. This configuration is far from ideal, and shouldn't ever be used in production though.

Upvotes: 4

Related Questions