2018-07-31 23:07:42 +02:00
|
|
|
# Caching in Zulip
|
|
|
|
|
|
|
|
Like any product with good performance characteristics, Zulip makes
|
2021-08-20 21:53:28 +02:00
|
|
|
extensive use of caching. This article talks about our caching
|
2018-07-31 23:07:42 +02:00
|
|
|
strategy, focusing on how we use `memcached` (since it's the thing
|
|
|
|
people generally think about when they ask about how a server does
|
|
|
|
caching).
|
|
|
|
|
|
|
|
## Backend caching with memcached
|
|
|
|
|
|
|
|
On the backend, Zulip uses `memcached`, a popular key-value store, for
|
2021-08-20 21:53:28 +02:00
|
|
|
caching. Our `memcached` caching helps let us optimize Zulip's
|
2018-07-31 23:07:42 +02:00
|
|
|
performance and scalability, since most requests don't need to talk to
|
|
|
|
the database (which, even for a trivial query with everything on the
|
|
|
|
same machine, usually takes 3-10x as long as a memcached fetch).
|
|
|
|
|
|
|
|
We use Django's built-in caching integration to manage talking to
|
|
|
|
memcached, and then a small application-layer library
|
|
|
|
(`zerver/lib/cache.py`).
|
|
|
|
|
|
|
|
It's common for projects using a caching system like `memcached` to
|
|
|
|
either have the codebase littered with explicit requests to interact
|
|
|
|
with the cache (or flush data from a cache), or (worse) be littered
|
|
|
|
with weird bugs that disappear after you flush memcached.
|
|
|
|
|
|
|
|
Caching bugs are a pain to track down, because they generally require
|
|
|
|
an extra and difficult-to-guess step to reproduce (namely, putting the
|
|
|
|
wrong data into the cache).
|
|
|
|
|
|
|
|
So we've designed our backend to ensure that if we write a small
|
2018-08-03 21:52:14 +02:00
|
|
|
amount of Zulip's core caching code correctly, then the code most developers
|
|
|
|
naturally write will both benefit from caching and not create any cache
|
|
|
|
consistency problems.
|
2018-07-31 23:07:42 +02:00
|
|
|
|
|
|
|
The overall result of this design is that in the vast majority of
|
|
|
|
Zulip's Django codebase, all one needs to do is call the standard
|
|
|
|
accessor functions for data (like `get_user` or `get_stream` to fetch
|
|
|
|
user and stream objects, or for view code, functions like
|
|
|
|
`access_stream_by_id`, which checks permissions), and everything will
|
2021-08-20 21:53:28 +02:00
|
|
|
work great. The data fetches automatically benefit from `memcached`
|
2018-07-31 23:07:42 +02:00
|
|
|
caching, since those accessor methods have already been written to
|
|
|
|
transparently use Zulip's memcached caching system, and the developer
|
|
|
|
doesn't need to worry about whether the data returned is up-to-date:
|
2021-08-20 21:53:28 +02:00
|
|
|
it is. In the following sections, we'll talk about how we make this
|
2018-07-31 23:07:42 +02:00
|
|
|
work.
|
|
|
|
|
|
|
|
As a sidenote, the policy of using these accessor functions wherever
|
|
|
|
possible is a good idea, regardless of caching, because the functions
|
|
|
|
also generally take care of details you might not think about
|
|
|
|
(e.g. case-insensitive matching of stream names or email addresses).
|
|
|
|
It's amazing how slightly tricky logic that's duplicated in several
|
|
|
|
places invariably ends up buggy in some of those places, and in
|
|
|
|
aggregate we call these accessor functions hundreds of times in
|
2021-08-20 21:53:28 +02:00
|
|
|
Zulip. But the caching is certainly a nice bonus.
|
2018-07-31 23:07:42 +02:00
|
|
|
|
|
|
|
### The core implementation
|
|
|
|
|
|
|
|
The `get_user` function is a pretty typical piece of code using this
|
|
|
|
framework; as you can see, it's very little code on top of our
|
|
|
|
`cache_with_key` decorator:
|
|
|
|
|
2021-08-20 22:54:08 +02:00
|
|
|
```python
|
2018-07-31 23:07:42 +02:00
|
|
|
def user_profile_cache_key_id(email: str, realm_id: int) -> str:
|
|
|
|
return u"user_profile:%s:%s" % (make_safe_digest(email.strip()), realm_id,)
|
|
|
|
|
|
|
|
def user_profile_cache_key(email: str, realm: 'Realm') -> str:
|
|
|
|
return user_profile_cache_key_id(email, realm.id)
|
|
|
|
|
|
|
|
@cache_with_key(user_profile_cache_key, timeout=3600*24*7)
|
|
|
|
def get_user(email: str, realm: Realm) -> UserProfile:
|
|
|
|
return UserProfile.objects.select_related().get(
|
|
|
|
email__iexact=email.strip(), realm=realm)
|
|
|
|
```
|
|
|
|
|
|
|
|
This decorator implements a pretty classic caching paradigm:
|
2021-08-20 22:54:08 +02:00
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- The `user_profile_cache_key` function defines a unique map from a
|
2021-08-20 21:53:28 +02:00
|
|
|
canonical form of its arguments to a string. These strings are
|
2018-07-31 23:07:42 +02:00
|
|
|
namespaced (the `user_profile:` part) so that they won't overlap
|
|
|
|
with other caches, and encode the arguments so that two uses of this
|
2021-08-20 21:53:28 +02:00
|
|
|
cache won't overlap. In this case, a hash of the email address and
|
|
|
|
realm ID are those canonicalized arguments. (The `make_safe_digest`
|
2018-07-31 23:07:42 +02:00
|
|
|
is important to ensure we don't send special characters to
|
2021-08-20 21:53:28 +02:00
|
|
|
memcached). And we have two versions, depending whether the caller
|
2018-07-31 23:07:42 +02:00
|
|
|
has access to a `Realm` or just a `realm_id`.
|
2021-08-20 21:45:39 +02:00
|
|
|
- When `get_user` is called, `cache_with_key` will compute the key,
|
2018-07-31 23:07:42 +02:00
|
|
|
and do a Django `cache_get` query for the key (which goes to
|
2021-08-20 21:53:28 +02:00
|
|
|
memcached). If the key is in the cache, it just returns the value.
|
2018-07-31 23:07:42 +02:00
|
|
|
Otherwise, it fetches the value from the database (using the actual
|
|
|
|
code in the body of `get_user`), and then stores the value back to
|
|
|
|
that memcached key before returning the result to the caller.
|
2021-08-20 21:45:39 +02:00
|
|
|
- Cache entries expire after the timeout; in this case, a week.
|
2018-07-31 23:07:42 +02:00
|
|
|
Though in frequently deployed environments like chat.zulip.org,
|
|
|
|
often cache entries will stop being used long before that, because
|
|
|
|
`KEY_PREFIX` is rotated every time we deploy to production; see
|
|
|
|
below for details.
|
|
|
|
|
|
|
|
We use this decorator in more than 30 places in Zulip, and it saves a
|
|
|
|
huge amount of otherwise very self-similar caching code.
|
|
|
|
|
|
|
|
### Cautions
|
|
|
|
|
|
|
|
The one thing to be really careful with in using `cache_with_key` is
|
|
|
|
that if an item is in the cache, the body of `get_user` (above) is
|
2021-08-20 21:53:28 +02:00
|
|
|
never called. This means some things that might seem like clever code
|
|
|
|
reuse are actually a really bad idea. For example:
|
2018-07-31 23:07:42 +02:00
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- Don't add a `get_active_user` function that uses the same cache key
|
2020-05-17 14:14:57 +02:00
|
|
|
function as `get_user` (but with a different query that filters our
|
2021-08-20 21:53:28 +02:00
|
|
|
deactivated users). If one called `get_active_user` to access a
|
2020-05-17 14:14:57 +02:00
|
|
|
deactivated user, the right thing would happen, but if you called
|
2018-07-31 23:07:42 +02:00
|
|
|
`get_user` to access that user first, then the `get_active_user`
|
2020-05-17 14:14:57 +02:00
|
|
|
function would happily return the user from the cache, without ever
|
|
|
|
doing your more restrictive query.
|
2018-07-31 23:07:42 +02:00
|
|
|
|
|
|
|
So remember: Use separate cache key functions for different data sets,
|
|
|
|
even if they feature the same objects.
|
|
|
|
|
|
|
|
### Cache invalidation after writes
|
|
|
|
|
|
|
|
The caching strategy described above works pretty well for anything
|
2021-08-20 21:53:28 +02:00
|
|
|
where the state it's storing is immutable (i.e. never changes). With
|
2018-07-31 23:07:42 +02:00
|
|
|
mutable state, one needs to do something to ensure that the Python
|
|
|
|
processes don't end up fetching stale data from the cache after a
|
|
|
|
write to the database.
|
|
|
|
|
2020-05-17 14:18:23 +02:00
|
|
|
We handle this using Django's longstanding
|
2021-08-20 21:53:28 +02:00
|
|
|
[post_save signals][post-save-signals] feature. Django signals let
|
2018-07-31 23:07:42 +02:00
|
|
|
you configure some code to run every time Django does something (for
|
|
|
|
`post_save`, right after any write to the database using Django's
|
|
|
|
`.save()`).
|
|
|
|
|
|
|
|
There's a handful of lines in `zerver/models.py` like these that
|
|
|
|
configure this:
|
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```python
|
2018-07-31 23:07:42 +02:00
|
|
|
post_save.connect(flush_realm, sender=Realm)
|
|
|
|
post_save.connect(flush_user_profile, sender=UserProfile)
|
|
|
|
```
|
|
|
|
|
|
|
|
Once this `post_save` hook is registered, whenever one calls
|
|
|
|
`user_profile.save(...)` with a UserProfile object in our Django
|
2021-08-20 21:53:28 +02:00
|
|
|
project, Django will call the `flush_user_profile` function. Zulip is
|
2018-07-31 23:07:42 +02:00
|
|
|
systematic about using the standard Django `.save()` function for
|
|
|
|
modifying `user_profile` objects (and passing the `update_fields`
|
|
|
|
argument to `.save()` consistently, which encodes which fields on an
|
2021-08-20 21:53:28 +02:00
|
|
|
object changed). This means that all we have to do is write those
|
2018-07-31 23:07:42 +02:00
|
|
|
cache-flushing functions correctly, and people writing Zulip code
|
2018-08-03 21:52:14 +02:00
|
|
|
won't need to think about (or even know about!) the caching.
|
2018-07-31 23:07:42 +02:00
|
|
|
|
|
|
|
Each of those flush functions basically just computes the list of
|
|
|
|
cache keys that might contain data that was modified by the
|
|
|
|
`.save(...)` call (based on the object changed and the `update_fields`
|
|
|
|
data), and then sends a bulk delete request to `memcached` to remove
|
|
|
|
those keys from the cache (if present).
|
|
|
|
|
|
|
|
Maintaining these flush functions requires some care (every time we
|
|
|
|
add a new cache, we need to look through them), but overall it's a
|
|
|
|
pretty simple algorithm: If the changed data appears in any form in a
|
2021-08-20 21:53:28 +02:00
|
|
|
given cache key, that cache key needs to be cleared. E.g. the
|
2018-07-31 23:07:42 +02:00
|
|
|
`active_user_ids_cache_key` cache for a realm needs to be flushed
|
|
|
|
whenever a new user is created in that realm, or user is
|
|
|
|
deactivated/reactivated, even though it's just a list of IDs and thus
|
|
|
|
doesn't explicitly contain the `is_active` flag.
|
|
|
|
|
|
|
|
Once you understand how that works, it's pretty easy to reason about
|
|
|
|
when a particular flush function should clear a particular cache; so
|
|
|
|
the main thing that requires care is making sure we remember to reason
|
|
|
|
about that when changing cache semantics.
|
|
|
|
|
|
|
|
But the overall benefit of this cache system is that almost all the
|
|
|
|
code in Zulip just needs to modify Django model objects and call
|
|
|
|
`.save()`, and the caching system will do the right thing.
|
|
|
|
|
|
|
|
### Production deployments and database migrations
|
|
|
|
|
|
|
|
When upgrading a Zulip server, it's important to avoid having one
|
|
|
|
version of the code interact with cached objects from another version
|
2021-08-20 21:53:28 +02:00
|
|
|
that has a different data layout. In Zulip, we avoid this through
|
|
|
|
some clever caching strategies. Each "deployment directory" for Zulip
|
2018-07-31 23:07:42 +02:00
|
|
|
in production has inside it a `var/remote_cache_prefix` file,
|
|
|
|
containing a cache prefix (`KEY_PREFIX` in the code) that is
|
|
|
|
automatically appended to the start of any cache keys accessed by that
|
|
|
|
deployment directory (this is all handled internally by
|
|
|
|
`zerver/lib/cache.py`).
|
|
|
|
|
|
|
|
This completely solves the problem of potentially having contamination
|
|
|
|
from inconsistent versions of the source code / data formats in the cache.
|
|
|
|
|
|
|
|
### Automated testing and memcached
|
|
|
|
|
2021-08-20 21:53:28 +02:00
|
|
|
For Zulip's `test-backend` unit tests, we use the same strategy. In
|
2018-07-31 23:07:42 +02:00
|
|
|
particular, we just edit `KEY_PREFIX` before each unit test; this
|
|
|
|
means each of the thousands of test cases in Zulip has its own
|
2021-08-20 21:53:28 +02:00
|
|
|
independent memcached key namespace on each run of the unit tests. As
|
2018-07-31 23:07:42 +02:00
|
|
|
a result, we never have to worry about memcached caching causing
|
|
|
|
problems across multiple tests.
|
|
|
|
|
2021-08-20 21:53:28 +02:00
|
|
|
This is a really important detail. It makes it possible for us to do
|
2018-07-31 23:07:42 +02:00
|
|
|
assertions in our tests on the number of database queries or memcached
|
2018-08-03 21:52:14 +02:00
|
|
|
queries that are done as part of a particular function/route, and have
|
2018-07-31 23:07:42 +02:00
|
|
|
those checks consistently get the same result (those tests are great
|
|
|
|
for catching bugs where we accidentally do database queries in a
|
2021-08-20 21:53:28 +02:00
|
|
|
loop). And it means one can debug failures in the test suite without
|
2018-07-31 23:07:42 +02:00
|
|
|
having to consider the possibility that memcached is somehow confusing
|
|
|
|
the situation.
|
|
|
|
|
|
|
|
Further, this `KEY_PREFIX` model means that running the backend tests
|
|
|
|
won't potentially conflict with whatever you're doing in a Zulip
|
|
|
|
development environment on the same machine, which also saves a ton of
|
|
|
|
time when debugging, since developers don't need to think about things
|
|
|
|
like whether some test changed Hamlet's email address and that's why
|
|
|
|
login is broken.
|
|
|
|
|
2020-08-31 00:10:57 +02:00
|
|
|
More full-stack test suites like `test-js-with-puppeteer` or `test-api`
|
2018-07-31 23:07:42 +02:00
|
|
|
use a similar strategy (set a random `KEY_PREFIX` at the start of the
|
|
|
|
test run).
|
|
|
|
|
2020-12-20 21:02:35 +01:00
|
|
|
### Manual testing and memcached
|
|
|
|
|
|
|
|
Zulip's development environment will automatically flush (delete all
|
|
|
|
keys in) `memcached` when provisioning and when starting `run-dev.py`.
|
|
|
|
You can run the server with that behavior disabled using
|
|
|
|
`tools/run-dev.py --no-clear-memcached`.
|
|
|
|
|
2018-07-31 23:07:42 +02:00
|
|
|
### Performance
|
|
|
|
|
|
|
|
One thing be careful about with memcached queries is to avoid doing
|
2021-08-20 21:53:28 +02:00
|
|
|
them in loops (the same applies for database queries!). Instead, one
|
|
|
|
should use a bulk query. We have a fancy function,
|
2018-07-31 23:07:42 +02:00
|
|
|
`generate_bulk_cached_fetch`, which is super magical and handles this
|
|
|
|
for us, with support for a bunch of fancy features like marshalling
|
|
|
|
data before/after going into the cache (e.g. to compress `message`
|
|
|
|
objects to minimize data transfer between Django and memcached).
|
|
|
|
|
|
|
|
## In-process caching in Django
|
|
|
|
|
|
|
|
We generally try to avoid in-process backend caching in Zulip's Django
|
|
|
|
codebase, because every Zulip production installation involves
|
2021-08-20 21:53:28 +02:00
|
|
|
multiple servers. We do have a few, however:
|
2021-08-20 22:54:08 +02:00
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- `per_request_display_recipient_cache`: A cache flushed at the start
|
2018-07-31 23:07:42 +02:00
|
|
|
of every request; this simplifies correctly implementing our goal of
|
|
|
|
not repeatedly fetching the "display recipient" (e.g. stream name)
|
|
|
|
for each message in the `GET /messages` codebase.
|
2021-08-20 21:45:39 +02:00
|
|
|
- Caches of various data, like the `SourceMap` object, that are
|
2018-07-31 23:07:42 +02:00
|
|
|
expensive to construct, not needed for most requests, and don't
|
|
|
|
change once a Zulip server has been deployed in production.
|
|
|
|
|
|
|
|
## Browser caching of state
|
|
|
|
|
|
|
|
Zulip makes extensive use of caching of data in the browser and mobile
|
|
|
|
apps; details like which users exist, with metadata like names and
|
|
|
|
avatars, similar details for streams, recent message history, etc.
|
|
|
|
|
2021-02-04 17:34:34 +01:00
|
|
|
This data is fetched in the `/register` endpoint (or `page_params`
|
2021-08-20 21:53:28 +02:00
|
|
|
for the web app), and kept correct over time. The key to keeping these
|
2018-07-31 23:07:42 +02:00
|
|
|
state up to date is Zulip's
|
2019-09-30 19:37:56 +02:00
|
|
|
[real-time events system](../subsystems/events-system.md), which
|
2018-07-31 23:07:42 +02:00
|
|
|
allows the server to notify clients whenever state that might be
|
2021-08-20 21:53:28 +02:00
|
|
|
cached by clients is changed. Clients are responsible for handling
|
2018-07-31 23:07:42 +02:00
|
|
|
the events, updating their state, and rerendering any UI components
|
|
|
|
that might display the modified state.
|
|
|
|
|
2021-11-05 20:09:57 +01:00
|
|
|
[post-save-signals]: https://docs.djangoproject.com/en/3.2/ref/signals/#post-save
|