django doesnt work out of the box with multiple gunicorn/uwsgi workers 🤯

This is really incredible but django has an extremely unfortunate default CACHE setting that is not ok for production environments, it defaults to a local memory cache that is not shared between different gunicorn or uwsgi workers. As a result, each worker can have a different state, even database values might differ in a django form!

Read here https://stackoverflow.com/questions/25052248/django-default-cache and here https://stackoverflow.com/questions/6422440/django1-3-multiple-gunicorn-workers-caching-problems

Solution: Set up memcached (apt get install memcached also you need to enable sockets if you want to use unix sockets, otherwise change the below config to use tcp instead) and set up django to use that cache backend in production environments:

CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': 'unix:/var/run/memcached/memcached.sock',
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *