zulip/puppet/zulip_ops/files
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
..
apache puppet: Move nagios to behind teleport. 2021-06-02 18:38:38 -07:00
apt/apt.conf.d puppet: Set APT::Periodic::Unattended-Upgrade in apt config. 2021-02-12 08:59:19 -08:00
certs
cron.d cron: Remove unused STATE_FILE environment variable. 2022-06-22 12:07:38 -07:00
grafana grafana: Enable auto-sign-up. 2022-07-19 17:52:17 -07:00
iptables puppet: Configure ip6tables in parallel to ipv4. 2021-08-24 16:05:46 -07:00
munin puppet: Configure munin and nagios under apache with puppet. 2020-07-13 13:23:11 -07:00
munin-plugins munin: Update to use NAGIOS_BOT_HOST. 2021-01-27 12:07:09 -08:00
nagios4 nagios: Switch check_user_zephyr_mirror_liveness to run via cron. 2022-06-22 12:07:38 -07:00
nagios_plugins/zulip_zephyr_mirror ruff: Fix RUF004 exit() is only available in the interpreter. 2022-12-04 22:11:24 -08:00
needrestart puppet: Tell needrestart to not default to restarting core services. 2022-07-19 17:51:18 -07:00
nginx/sites-available uploads: Serve S3 uploads directly from nginx. 2023-01-09 18:23:58 -05:00
postgresql puppet: Add a database teleport server. 2021-06-08 22:21:21 -07:00
prometheus puppet: Only fetch from running hosts in Grafana ec2 discovery. 2021-12-09 08:12:03 -08:00
supervisor/conf.d puppet: Move prometheus to external_dep. 2021-12-29 16:35:15 -08:00
chrony.conf puppet: Configure chrony to use AWS-local NTP sources. 2022-03-25 17:07:53 -07:00
common-session
dot_emacs.el cleanup: Delete leading newlines. 2019-08-06 23:29:11 -07:00
krb5.conf puppet: Replace debathena krb5 package with equivalent puppet file. 2022-01-18 14:13:28 -08:00
nagios_ssh_config puppet: Use existing autossh tunnels as OpenSSH "master" sockets. 2022-11-01 22:24:40 -07:00
sshd_config
teleport_app.yaml puppet: Only include "app_service" section if there are apps. 2022-04-26 16:36:13 -07:00
teleport_node.yaml puppet: Only include "app_service" section if there are apps. 2022-04-26 16:36:13 -07:00
teleport_server.yaml teleport: Add explicit WebAuthn config, not just U2F. 2022-07-18 11:41:00 -07:00
tornado_process_exporter.yaml puppet: Add a prometheus monitor for tornado processes. 2022-03-20 16:12:11 -07:00
zephyr-clients puppet: Replace debathena zephyr package with equivalent puppet file. 2022-01-18 14:13:28 -08:00