zulip/zproject
Alex Vandiver 04cf68b45e uploads: Serve S3 uploads directly from nginx.
When file uploads are stored in S3, this means that Zulip serves as a
302 to S3.  Because browsers do not cache redirects, this means that
no image contents can be cached -- and upon every page load or reload,
every recently-posted image must be re-fetched.  This incurs extra
load on the Zulip server, as well as potentially excessive bandwidth
usage from S3, and on the client's connection.

Switch to fetching the content from S3 in nginx, and serving the
content from nginx.  These have `Cache-control: private, immutable`
headers set on the response, allowing browsers to cache them locally.

Because nginx fetching from S3 can be slow, and requests for uploads
will generally be bunched around when a message containing them are
first posted, we instruct nginx to cache the contents locally.  This
is safe because uploaded file contents are immutable; access control
is still mediated by Django.  The nginx cache key is the URL without
query parameters, as those parameters include a time-limited signed
authentication parameter which lets nginx fetch the non-public file.

This adds a number of nginx-level configuration parameters to control
the caching which nginx performs, including the amount of in-memory
index for he cache, the maximum storage of the cache on disk, and how
long data is retained in the cache.  The currently-chosen figures are
reasonable for small to medium deployments.

The most notable effect of this change is in allowing browsers to
cache uploaded image content; however, while there will be many fewer
requests, it also has an improvement on request latency.  The
following tests were done with a non-AWS client in SFO, a server and
S3 storage in us-east-1, and with 100 requests after 10 requests of
warm-up (to fill the nginx cache).  The mean and standard deviation
are shown.

|                   | Redirect to S3      | Caching proxy, hot  | Caching proxy, cold |
| ----------------- | ------------------- | ------------------- | ------------------- |
| Time in Django    | 263.0 ms ±  28.3 ms | 258.0 ms ±  12.3 ms | 258.0 ms ±  12.3 ms |
| Small file (842b) | 586.1 ms ±  21.1 ms | 266.1 ms ±  67.4 ms | 288.6 ms ±  17.7 ms |
| Large file (660k) | 959.6 ms ± 137.9 ms | 609.5 ms ±  13.0 ms | 648.1 ms ±  43.2 ms |

The hot-cache performance is faster for both large and small files,
since it saves the client the time having to make a second request to
a separate host.  This performance improvement remains at least 100ms
even if the client is on the same coast as the server.

Cold nginx caches are only slightly slower than hot caches, because
VPC access to S3 endpoints is extremely fast (assuming it is in the
same region as the host), and nginx can pool connections to S3 and
reuse them.

However, all of the 648ms taken to serve a cold-cache large file is
occupied in nginx, as opposed to the only 263ms which was spent in
nginx when using redirects to S3.  This means that to overall spend
less time responding to uploaded-file requests in nginx, clients will
need to find files in their local cache, and skip making an
uploaded-file request, at least 60% of the time.  Modeling shows a
reduction in the number of client requests by about 70% - 80%.

The `Content-Disposition` header logic can now also be entirely shared
with the local-file codepath, as can the `url_only` path used by
mobile clients.  While we could provide the direct-to-S3 temporary
signed URL to mobile clients, we choose to provide the
served-from-Zulip signed URL, to better control caching headers on it,
and greater consistency.  In doing so, we adjust the salt used for the
URL; since these URLs are only valid for 60s, the effect of this salt
change is minimal.
2023-01-09 18:23:58 -05:00
..
jinja2 requirements: Upgrade Python requirements. 2022-07-05 17:54:17 -07:00
__init__.py
backends.py ruff: Fix ISC003 Explicitly concatenated string. 2023-01-04 16:25:07 -08:00
computed_settings.py uploads: Add LOCAL_AVATARS_DIR / LOCAL_FILES_DIR computed settings. 2023-01-09 18:23:58 -05:00
config.py config: Remove Django base class from ZulipSettingsError. 2022-09-26 12:42:36 -07:00
configured_settings.py ruff: Fix M001 Unused `noqa` directive. 2022-11-03 12:10:15 -07:00
default_settings.py uploads: Add LOCAL_AVATARS_DIR / LOCAL_FILES_DIR computed settings. 2023-01-09 18:23:58 -05:00
dev_settings.py uploads: Set X-Accel-Redirect manually, without using django-sendfile2. 2023-01-09 18:23:58 -05:00
dev_urls.py avatars: Serve /user_avatars/ through Django, which offloads to nginx. 2023-01-09 18:23:58 -05:00
email_backends.py email_backends: Fix type annotation of EmailLogBackEnd. 2022-07-29 14:58:11 -07:00
legacy_urls.py legacy_urls: Add type annotation to legacy_urls. 2022-08-12 17:08:04 -07:00
prod_settings.pyi lint: Replace pycodestyle and pyflakes with ruff. 2022-11-03 12:10:15 -07:00
prod_settings_template.py rate_limit: Add interface to override rate limiting rules. 2022-11-08 08:56:42 -08:00
sentry.py requirements: Upgrade Python requirements. 2022-06-01 16:01:54 -07:00
settings.py ruff: Fix M001 Unused `noqa` directive. 2022-11-03 12:10:15 -07:00
settings_types.py settings_type: Extract new module for types used in settings. 2022-09-26 12:42:36 -07:00
template_loaders.py template_loaders: Extract TwoFactorLoader to new module. 2022-09-26 12:42:36 -07:00
test_extra_settings.py settings_type: Extract new module for types used in settings. 2022-09-26 12:42:36 -07:00
test_settings.py ruff: Fix M001 Unused `noqa` directive. 2022-11-03 12:10:15 -07:00
urls.py uploads: Serve S3 uploads directly from nginx. 2023-01-09 18:23:58 -05:00
wsgi.py python: Normalize quotes with Black. 2021-02-12 13:11:19 -08:00