| 1 |
Django Cachalot |
| 2 |
=============== |
| 3 |
|
| 4 |
Caches your Django ORM queries and automatically invalidates them. |
| 5 |
|
| 6 |
Documentation: http://django-cachalot.readthedocs.io |
| 7 |
|
| 8 |
---- |
| 9 |
|
| 10 |
Table of Contents: |
| 11 |
|
| 12 |
- Quickstart |
| 13 |
- Usage |
| 14 |
- Hacking |
| 15 |
- Benchmark |
| 16 |
- Third-Party Cache Comparison |
| 17 |
- Discussion |
| 18 |
|
| 19 |
Quickstart |
| 20 |
---------- |
| 21 |
|
| 22 |
Cachalot officially supports Python 3.8-3.14 and Django 4.2, 5.2, 6.0, 6.1 with the databases PostgreSQL, SQLite, and MySQL. |
| 23 |
|
| 24 |
Note: an upper limit on Django version is set for your safety. Please do not ignore it. |
| 25 |
|
| 26 |
Usage |
| 27 |
----- |
| 28 |
|
| 29 |
#. ``pip install django-cachalot`` |
| 30 |
#. Add ``'cachalot',`` to your ``INSTALLED_APPS`` |
| 31 |
#. If you use multiple servers with a common cache server, |
| 32 |
`double check their clock synchronisation <https://django-cachalot.readthedocs.io/en/latest/limits.html#multiple-servers>`_ |
| 33 |
#. If you modify data outside Django |
| 34 |
– typically after restoring a SQL database –, |
| 35 |
use the `manage.py command <https://django-cachalot.readthedocs.io/en/latest/quickstart.html#command>`_ |
| 36 |
#. Be aware of `the few other limits <https://django-cachalot.readthedocs.io/en/latest/limits.html#limits>`_ |
| 37 |
#. If you use |
| 38 |
`django-debug-toolbar <https://github.com/jazzband/django-debug-toolbar>`_, |
| 39 |
you can add ``'cachalot.panels.CachalotPanel',`` |
| 40 |
to your ``DEBUG_TOOLBAR_PANELS`` |
| 41 |
#. Enjoy! |
| 42 |
|
| 43 |
Hacking |
| 44 |
------- |
| 45 |
|
| 46 |
To start developing, install the requirements |
| 47 |
and run the tests via tox. |
| 48 |
|
| 49 |
Make sure you have the following services: |
| 50 |
|
| 51 |
* Memcached |
| 52 |
* Redis |
| 53 |
* PostgreSQL |
| 54 |
* MySQL |
| 55 |
|
| 56 |
For setup: |
| 57 |
|
| 58 |
#. Install: ``pip install -r requirements/hacking.txt`` |
| 59 |
#. For PostgreSQL: ``CREATE ROLE cachalot LOGIN SUPERUSER;`` |
| 60 |
#. Run: ``tox --current-env`` to run the test suite on your current Python version. |
| 61 |
#. You can also run specific databases and Django versions: ``tox -e py312-django5.2-postgresql-redis`` |
| 62 |
|
| 63 |
Benchmark |
| 64 |
--------- |
| 65 |
|
| 66 |
Currently, benchmarks are supported on Linux and Mac/Darwin. |
| 67 |
You will need a database called "cachalot" on MySQL and PostgreSQL. |
| 68 |
Additionally, on PostgreSQL, you will need to create a role |
| 69 |
called "cachalot." You can also run the benchmark, and it'll raise |
| 70 |
errors with specific instructions for how to fix it. |
| 71 |
|
| 72 |
#. Install: ``pip install -r requirements/benchmark.txt`` |
| 73 |
#. Run: ``python benchmark.py`` |
| 74 |
|
| 75 |
The output will be in benchmark/TODAY'S_DATE/ |
| 76 |
|
| 77 |
TODO Create Docker-compose file to allow for easier running of data. |
| 78 |
|
| 79 |
Third-Party Cache Comparison |
| 80 |
---------------------------- |
| 81 |
|
| 82 |
There are three main third party caches: cachalot, cache-machine, and cache-ops. Which do you use? We suggest a mix: |
| 83 |
|
| 84 |
TL;DR Use cachalot for cold or modified <50 times per minutes (Most people should stick with only cachalot since you |
| 85 |
most likely won't need to scale to the point of needing cache-machine added to the bowl). If you're an enterprise that |
| 86 |
already has huge statistics, then mixing cold caches for cachalot and your hot caches with cache-machine is the best |
| 87 |
mix. However, when performing joins with ``select_related`` and ``prefetch_related``, you can |
| 88 |
get a nearly 100x speed up for your initial deployment. |
| 89 |
|
| 90 |
Recall, cachalot caches THE ENTIRE TABLE. That's where its inefficiency stems from: if you keep updating the records, |
| 91 |
then the cachalot constantly invalidates the table and re-caches. Luckily caching is very efficient, it's just the cache |
| 92 |
invalidation part that kills all our systems. Look at Note 1 below to see how Reddit deals with it. |
| 93 |
|
| 94 |
Cachalot is more-or-less intended for cold caches or "just-right" conditions. If you find a partition library for |
| 95 |
Django (also authored but work-in-progress by `Andrew Chen Wang`_), then the caching will work better since sharding |
| 96 |
the cold/accessed-the-least records aren't invalidated as much. |
| 97 |
|
| 98 |
Cachalot is good when there are <50 modifications per minute on a hot cached table. This is mostly due to cache invalidation. It's the same with any cache, |
| 99 |
which is why we suggest you use cache-machine for hot caches. Cache-machine caches individual objects, taking up more in the memory store but |
| 100 |
invalidates those individual objects instead of the entire table like cachalot. |
| 101 |
|
| 102 |
Yes, the bane of our entire existence lies in cache invalidation and naming variables. Why does cachalot suck when |
| 103 |
stuck with a huge table that's modified rapidly? Since you've mixed your cold (90% of) with your hot (10% of) records, |
| 104 |
you're caching and invalidating an entire table. It's like trying to boil 1 ton of noodles inside ONE pot instead of |
| 105 |
100 pots boiling 1 ton of noodles. Which is more efficient? The splitting up of them. |
| 106 |
|
| 107 |
Note 1: My personal experience with caches stems from Reddit's: https://web.archive.org/web/20210803213621/https://redditblog.com/2017/01/17/caching-at-reddit/ |
| 108 |
|
| 109 |
Note 2: Technical comparison: https://django-cachalot.readthedocs.io/en/latest/introduction.html#comparison-with-similar-tools |
| 110 |
|
| 111 |
Discussion |
| 112 |
---------- |
| 113 |
|
| 114 |
Help? Technical chat? `It's here on Discord <https://discord.gg/WFGFBk8rSU>`_. |
| 115 |
|
| 116 |
Legacy chats: |
| 117 |
|
| 118 |
- https://gitter.im/django-cachalot/Lobby |
| 119 |
- https://join.slack.com/t/cachalotdjango/shared_invite/zt-dd0tj27b-cIH6VlaSOjAWnTG~II5~qw |
| 120 |
|
| 121 |
.. _Andrew Chen Wang: https://github.com/Andrew-Chen-Wang |
| 122 |
|
| •123 |
.. image:: //usr/share/doc/python-django-cachalot-doc/html/_images/django-cachalot.jpg |