0%

Don't forget to set ignore_exc when using pymemcache

After upgrading Django to 4.2, we started replacing python-memcached with pymemcache.

Once the switch was done, we noticed that the default setup was no longer behaving well in a high-availability scenario. If multiple memcached backends were configured and one of them went down, any cache-related code could fail with an exception instead of degrading gracefully.

After checking the official documentation, the reason became clear: by default, pymemcache raises exceptions on connection failures unless you explicitly set ignore_exc = True.

So the fix is simply to add that option to your Django CACHES settings:

1
2
3
4
5
6
7
8
9
10
11
12
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.PyMemcacheCache',
'LOCATION': [
'host1:11211',
'host2:11211',
],
'OPTIONS': {
'ignore_exc': True,
}
}
}

If you expect memcached nodes to fail independently and still want your application to continue running, this setting is easy to miss but important to have.

如果我的文字帮到了您,那么可不可以请我喝罐可乐?