Debian Codegraph @

docs/introduction.rst

raw
1 .. _Introduction:
2
3 Introduction
4 ------------
5
6 Should you use it?
7 ..................
8
9 Django-cachalot is the perfect speedup tool for most Django projects.
10 It will speedup a website of 100 000 visits per month without any problem.
11 In fact, **the more visitors you have, the faster the website becomes**.
12 That’s because every possible SQL query on the project ends up being cached.
13
14 Django-cachalot is especially efficient in the Django administration website
15 since it’s unfortunately badly optimised (use foreign keys in ``list_editable``
16 if you need to be convinced). One of the best suited is ``select_related`` and
17 ``prefetch_related`` operations.
18
19 However, it’s not suited for projects where there is **a high number
20 of modifications per minute** on each table, like a social network with
21 more than a 50 messages per minute. Django-cachalot may still give a small
22 speedup in such cases, but it may also slow things a bit
23 (in the worst case scenario, a 20% slowdown,
24 according to :ref:`the benchmark <Benchmark>`).
25 If you have a website like that, optimising your SQL database and queries
26 is the number one thing you have to do.
27
28 There is also an obvious case where you don’t need django-cachalot:
29 when the project is already fast enough (all pages load in less than 300 ms).
30 Like any other dependency, django-cachalot is a potential source of problems
31 (even though it’s currently bug free).
32 Don’t use dependencies you can avoid, a “future you” may thank you for that.
33
34 Features
35 ........
36
37 - **Saves in cache the results of any SQL query** generated by the Django ORM
38 that reads data. These saved results are then returned instead
39 of executing the same SQL query, which is faster.
40 - The first time a query is executed is about 10% slower, then the following
41 times are way faster (7× faster being the average).
42 - Automatically invalidates saved results,
43 so that **you never get stale results**.
44 - **Invalidates per table, not per object**: if you change an object,
45 all the queries done on other objects of the same model are also invalidated.
46 This is unfortunately technically impossible to make a reliable
47 per-object cache. Don’t be fooled by packages pretending having
48 that per-object feature, they are unreliable and dangerous for your data.
49 - **Handles everything in the ORM**. You can use the most advanced features
50 from the ORM without a single issue, django-cachalot is extremely robust.
51 - An easy control thanks to :ref:`settings` and :ref:`a simple API <Api>`.
52 But that’s only required if you have a complex infrastructure. Most people
53 will never use settings or the API.
54 - A few bonus features like
55 :ref:`a signal triggered at each database change <Signal>`
56 (including bulk changes) and
57 :ref:`a template tag for a better template fragment caching <Template utils>`.
58
59 .. _Comparison with similar tools:
60
61 Comparison with similar tools
62 .............................
63
64 This comparison was done in December 2015. It compares django-cachalot
65 to the other popular automatic ORM caches at the moment:
66 `django-cache-machine <https://github.com/django-cache-machine/django-cache-machine>`_
67 & `django-cacheops <https://github.com/Suor/django-cacheops>`_.
68
69 ===================================================== ========= ============= ==========
70 Feature cachalot cache-machine cacheops
71 ===================================================== ========= ============= ==========
72 Easy to install ✔ ✘ quite
73 Cache agnostic ✔ ✔ ✘
74 Type of invalidation per table per object per query
75 CPU performance excellent excellent excellent
76 Memory performance excellent good excellent
77 Reliable ✔ ✘ ✘
78 Useful for > 50 modifications per minute ✘ ✔ ✔
79 Handles transactions ✔ ✘ ✘
80 Handles Django admin save ✔ ✘ ✘
81 Handles multi-table inheritance ✔ ✔ ✘
82 Handles ``QuerySet.count`` ✔ ✘ ✔
83 Handles ``QuerySet.aggregate``/``annotate`` ✔ ✔ ✘
84 Handles ``QuerySet.update`` ✔ ✘ ✘
85 Handles ``QuerySet.select_related`` ✔ ✔ ✘
86 Handles ``QuerySet.extra`` ✔ ✘ ✘
87 Handles ``QuerySet.values``/``values_list`` ✔ ✘ ✔
88 Handles ``QuerySet.dates``/``datetimes`` ✔ ✘ ✔
89 Handles subqueries ✔ ✔ ✘
90 Handles querysets generating a SQL ``HAVING`` keyword ✔ ✔ ✘
91 Handles ``cursor.execute`` ✔ ✘ ✘
92 Handles the Django command ``flush`` ✔ ✘ ✘
93 ===================================================== ========= ============= ==========
94
95 Explanations
96 ''''''''''''
97
98 “Handles [a feature]” means that the package correctly invalidates SQL queries
99 using that feature. So if a package doesn’t handle a feature, you may get
100 stale query results when using this feature.
101 It does not mean that it caches a query with this feature, although
102 django-cachalot caches all queries except random queries
103 or those ran through ``cursor.execute``.
104
105 This comparison was done by running the test suite of cachalot against
106 cache-machine & cacheops. This test suite is indeed relevant for other
107 packages (such as cache-machine & cacheops) since most of it is written in
108 a cachalot-independent way.
109
110 Similarly, the performance comparison was done using our benchmark,
111 coupled with a memory measure.
112
113 To me, cache-machine & cacheops are not reliable because of these reasons:
114
115 - Neither cache-machine or cacheops handle transactions, which is critical.
116 **Transactions are used a lot in Django internals**: at least
117 in any Django admin save, many-to-many relations modification,
118 bulk creation or update, migrations, session save.
119 If an error occurs during one of these operations, good luck finding
120 if stale data is returned. The best you can do in this case is manually
121 clearing the cache.
122 - If you use a query that’s not handled, you may get stale data. It ends up
123 ruining your database since it lets you save modifications to stale data,
124 therefore overwriting the latest version that’s in the database.
125 And you always end up using queries that are not handled since there is no
126 list of unhandled queries in the documentation of each module.
127 - In the case of cache-machine, another issue is that it relies
128 on “flush lists”, which can’t work reliably when implemented in a cache
129 like this (see `cache-machine#107 <https://github.com/django-cache-machine/django-cache-machine/issues/107>`_).
130
131
132 Number of lines of code
133 ~~~~~~~~~~~~~~~~~~~~~~~
134
135 Django-cachalot tries to be as minimalist as possible, while handling most
136 use cases. Being minimalist is essential to create maintainable projects,
137 and having a large test suite is essential to get an excellent quality.
138 The statistics below speak for themselves…
139
140 ============ ======== ============= ========
141 Project part cachalot cache-machine cacheops
142 ============ ======== ============= ========
143 Application 743 843 1662
144 Tests 3023 659 1491
145 ============ ======== ============= ========