Debian Codegraph @

docs/limits.rst

raw
1 .. _Limits:
2
3 Limits
4 ------
5
6 High rate of database modifications
7 ...................................
8
9 Do not use django-cachalot if your project has more than 50 database
10 modifications per minute on most of its tables. There will be no problem,
11 but django-cachalot will become inefficient and will end up slowing
12 your project instead of speeding it.
13 Read :ref:`the introduction <Introduction>` for more details.
14
15 Redis
16 .....
17
18 By default, Redis will not evict persistent cache keys (those with a ``None``
19 timeout) when the maximum memory has been reached. The cache keys created
20 by django-cachalot are persistent by default, so if Redis runs out of memory,
21 django-cachalot and all other ``cache.set`` will raise
22 ``ResponseError: OOM command not allowed when used memory > 'maxmemory'.``
23 because Redis is not allowed to delete persistent keys.
24
25 To avoid this, 2 solutions:
26
27 - If you only store disposable data in Redis, you can change
28 ``maxmemory-policy`` to ``allkeys-lru`` in your Redis configuration.
29 Be aware that this setting is global; all your Redis databases will use it.
30 **If you don’t know what you’re doing, use the next solution or use
31 another cache backend.**
32 - Increase ``maxmemory`` in your Redis configuration.
33 You can start by setting it to a high value (for example half of your RAM)
34 then decrease it by looking at the Redis database maximum size using
35 ``redis-cli info memory``.
36
37 For more information, read
38 `Using Redis as a LRU cache <http://redis.io/topics/lru-cache>`_.
39
40 Memcached
41 .........
42
43 By default, memcached is configured for small servers.
44 The maximum amount of memory used by memcached is 64 MB,
45 and the maximum memory per cache key is 1 MB. This latter limit can lead to
46 weird unhandled exceptions such as
47 ``Error: error 37 from memcached_set: SUCCESS``
48 if you execute queries returning more than 1 MB of data.
49
50 To increase these limits, set the ``-I`` and ``-m`` arguments when starting
51 memcached. If you use Ubuntu and installed the package, you can modify
52 `/etc/memcached.conf`, add ``-I 10m`` on a newline to set the limit
53 per cache key to 10 MB, and if you want increase the already existing ``-m 64``
54 to something like ``-m 1000`` to set the maximum cache size to 1 GB.
55
56 .. _Locmem:
57
58 Locmem
59 ......
60
61 Locmem is a just a ``dict`` stored in a single Python process.
62 It’s not shared between processes, so don’t use locmem with django-cachalot
63 in a multi-processes project, if you use RQ or Celery for instance.
64
65 Filebased
66 .........
67
68 Filebased, a simple persistent cache implemented in Django, had a small bug
69 (`#25501 <https://code.djangoproject.com/ticket/25501>`_):
70 it cannot cache some objects, like psycopg2 ranges. This bug was fixed in 2015, if you sill use an affected Django version and you use range fields from `django.contrib.postgres`, you need to add the tables using range fields
71 to :ref:`CACHALOT_UNCACHABLE_TABLES`.
72
73 .. _MySQL:
74
75 MySQL
76 .....
77
78 This database software already provides by default something like
79 django-cachalot:
80 `MySQL query cache <http://dev.mysql.com/doc/refman/5.7/en/query-cache.html>`_.
81 Unfortunately, this built-in query cache has no significant effect
82 since at least MySQL 5.7. However, in MySQL 5.5 it was working so well that
83 django-cachalot was not improving performance.
84 So depending on the MySQL version, django-cachalot may be useless.
85 See the current :ref:`django-cachalot benchmark <Benchmark>` and compare it with
86 `an older run of the same benchmark <http://django-cachalot.readthedocs.io/en/1.2.0/benchmark.html>`_
87 to see the clear difference: MySQL became 4 × slower since then!
88
89 .. _Raw SQL queries:
90
91 Raw SQL queries
92 ...............
93
94 .. note::
95 Don’t worry if you don’t understand what follow. That probably means you
96 don’t use raw queries, and therefore are not directly concerned by
97 those potential issues.
98
99 By default, django-cachalot tries to invalidate its cache after a raw query.
100 It detects if the raw query contains ``UPDATE``, ``INSERT``, ``DELETE``,
101 ``ALTER``, ``CREATE`` or ``DROP`` and then invalidates the tables contained
102 in that query by comparing with models registered by Django.
103
104 This is quite robust, so if a query is not invalidated automatically
105 by this system, please :ref:`send a bug report <Reporting>`.
106 In the meantime, you can use :ref:`the API <Api>` to manually invalidate
107 the tables where data has changed.
108
109 However, this simple system can be too efficient in some very rare cases
110 and lead to unwanted extra invalidations.
111
112 .. _Multiple servers:
113
114 Multiple servers clock synchronisation
115 ......................................
116
117 Django-cachalot relies on the computer clock to handle invalidation.
118 If you deploy the same Django project on multiple machines,
119 but with a centralised cache server, all the machines serving Django need
120 to have their clocks as synchronised as possible.
121 Otherwise, invalidations will happen with a latency from one server to another.
122 A difference of even a few seconds can be harmful, so double check this!
123
124 To get a rough idea of the clock synchronisation of two servers, simply run
125 ``python -c 'import time; print(time.time())'`` on both servers at the same
126 time. This will give you a number of seconds, and it should be almost the same,
127 with a difference inferior to 1 second. This number is independent
128 of the time zone.
129
130 To keep your clocks synchronised, use the
131 `Network Time Protocol <http://en.wikipedia.org/wiki/Network_Time_Protocol>`_.
132
133 Replication server
134 ..................
135
136 If you use multiple databases where at least one is a replica of another,
137 django-cachalot has no way to know that the replica is modified
138 automatically, since it happens outside Django.
139 The SQL queries cached for the replica will therefore not be invalidated,
140 and you will see some stale queries results.
141
142 To fix this problem, you need to tell django-cachalot to also invalidate
143 the replica when the primary database is invalidated.
144 Suppose your primary database has the ``'default'`` database alias
145 in ``DATABASES``, and your replica has the ``'replica'`` alias.
146 Use :ref:`the signal <Signal>` and :meth:`cachalot.api.invalidate` this way:
147
148 .. code:: python
149
150 from cachalot.api import invalidate
151 from cachalot.signals import post_invalidation
152 from django.dispatch import receiver
153
154 @receiver(post_invalidation)
155 def invalidate_replica(sender, **kwargs):
156 if kwargs['db_alias'] == 'default':
157 invalidate(sender, db_alias='replica')
158
159 Multiple cache servers for the same database
160 ............................................
161
162 On large projects, we often end up having multiple Django servers on several
163 physical machines. For performance reasons, we generally decide to have a cache
164 per server, while the database stays on a single server. But the problem with
165 django-cachalot is that it only invalidates the cache configured using
166 ``CACHALOT_CACHE``. So all caches end up serving stale data.
167
168 To avoid this, you need inside each Django server to be able to communicate
169 with the rest of the servers in order to invalidate other caches when
170 an invalidation occurs. If this is not possible in your situation, you must not
171 use django-cachalot. But if you can, each Django server must also have all
172 other caches in the ``CACHES`` setting. Then, you need to manually invalidate
173 all other caches when an invalidation occurs. Add this to a `models.py` file
174 of an installed application:
175
176 .. code:: python
177
178 import threading
179
180 from cachalot.api import invalidate
181 from cachalot.signals import post_invalidation
182 from django.dispatch import receiver
183 from django.conf import settings
184
185 SIGNAL_INFO = threading.local()
186
187 @receiver(post_invalidation)
188 def invalidate_other_caches(sender, **kwargs):
189 if getattr(SIGNAL_INFO, 'was_called', False):
190 return
191 db_alias = kwargs['db_alias']
192 for cache_alias in settings.CACHES:
193 if cache_alias == settings.CACHALOT_CACHE:
194 continue
195 SIGNAL_INFO.was_called = True
196 try:
197 invalidate(sender, db_alias=db_alias, cache_alias=cache_alias)
198 finally:
199 SIGNAL_INFO.was_called = False