You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. # Scaling synapse via workers
  2. For small instances it is recommended to run Synapse in the default monolith mode.
  3. For larger instances where performance is a concern it can be helpful to split
  4. out functionality into multiple separate python processes. These processes are
  5. called 'workers', and are (eventually) intended to scale horizontally
  6. independently.
  7. Synapse's worker support is under active development and subject to change as
  8. we attempt to rapidly scale ever larger Synapse instances. However we are
  9. documenting it here to help admins needing a highly scalable Synapse instance
  10. similar to the one running `matrix.org`.
  11. All processes continue to share the same database instance, and as such,
  12. workers only work with PostgreSQL-based Synapse deployments. SQLite should only
  13. be used for demo purposes and any admin considering workers should already be
  14. running PostgreSQL.
  15. See also [Matrix.org blog post](https://matrix.org/blog/2020/11/03/how-we-fixed-synapses-scalability)
  16. for a higher level overview.
  17. ## Main process/worker communication
  18. The processes communicate with each other via a Synapse-specific protocol called
  19. 'replication' (analogous to MySQL- or Postgres-style database replication) which
  20. feeds streams of newly written data between processes so they can be kept in
  21. sync with the database state.
  22. When configured to do so, Synapse uses a
  23. [Redis pub/sub channel](https://redis.io/docs/manual/pubsub/) to send the replication
  24. stream between all configured Synapse processes. Additionally, processes may
  25. make HTTP requests to each other, primarily for operations which need to wait
  26. for a reply ─ such as sending an event.
  27. All the workers and the main process connect to Redis, which relays replication
  28. commands between processes.
  29. If Redis support is enabled Synapse will use it as a shared cache, as well as a
  30. pub/sub mechanism.
  31. See the [Architectural diagram](#architectural-diagram) section at the end for
  32. a visualisation of what this looks like.
  33. ## Setting up workers
  34. A Redis server is required to manage the communication between the processes.
  35. The Redis server should be installed following the normal procedure for your
  36. distribution (e.g. `apt install redis-server` on Debian). It is safe to use an
  37. existing Redis deployment if you have one.
  38. Once installed, check that Redis is running and accessible from the host running
  39. Synapse, for example by executing `echo PING | nc -q1 localhost 6379` and seeing
  40. a response of `+PONG`.
  41. The appropriate dependencies must also be installed for Synapse. If using a
  42. virtualenv, these can be installed with:
  43. ```sh
  44. pip install "matrix-synapse[redis]"
  45. ```
  46. Note that these dependencies are included when synapse is installed with `pip
  47. install matrix-synapse[all]`. They are also included in the debian packages from
  48. `matrix.org` and in the docker images at
  49. https://hub.docker.com/r/matrixdotorg/synapse/.
  50. To make effective use of the workers, you will need to configure an HTTP
  51. reverse-proxy such as nginx or haproxy, which will direct incoming requests to
  52. the correct worker, or to the main synapse instance. See
  53. [the reverse proxy documentation](reverse_proxy.md) for information on setting up a reverse
  54. proxy.
  55. When using workers, each worker process has its own configuration file which
  56. contains settings specific to that worker, such as the HTTP listener that it
  57. provides (if any), logging configuration, etc.
  58. Normally, the worker processes are configured to read from a shared
  59. configuration file as well as the worker-specific configuration files. This
  60. makes it easier to keep common configuration settings synchronised across all
  61. the processes.
  62. The main process is somewhat special in this respect: it does not normally
  63. need its own configuration file and can take all of its configuration from the
  64. shared configuration file.
  65. ### Shared configuration
  66. Normally, only a few changes are needed to make an existing configuration
  67. file suitable for use with workers:
  68. * First, you need to enable an
  69. ["HTTP replication listener"](usage/configuration/config_documentation.md#listeners)
  70. for the main process
  71. * Secondly, you need to enable
  72. [redis-based replication](usage/configuration/config_documentation.md#redis)
  73. * You will need to add an [`instance_map`](usage/configuration/config_documentation.md#instance_map)
  74. with the `main` process defined, as well as the relevant connection information from
  75. it's HTTP `replication` listener (defined in step 1 above).
  76. * Note that the `host` defined is the address the worker needs to look for the `main`
  77. process at, not necessarily the same address that is bound to.
  78. * If you are using Unix sockets for the `replication` resource, make sure to
  79. use a `path` to the socket file instead of a `port`.
  80. * Optionally, a [shared secret](usage/configuration/config_documentation.md#worker_replication_secret)
  81. can be used to authenticate HTTP traffic between workers. For example:
  82. ```yaml
  83. # extend the existing `listeners` section. This defines the ports that the
  84. # main process will listen on.
  85. listeners:
  86. # The HTTP replication port
  87. - port: 9093
  88. bind_address: '127.0.0.1'
  89. type: http
  90. resources:
  91. - names: [replication]
  92. # Add a random shared secret to authenticate traffic.
  93. worker_replication_secret: ""
  94. redis:
  95. enabled: true
  96. instance_map:
  97. main:
  98. host: 'localhost'
  99. port: 9093
  100. ```
  101. See the [configuration manual](usage/configuration/config_documentation.md)
  102. for the full documentation of each option.
  103. Under **no circumstances** should the replication listener be exposed to the
  104. public internet; replication traffic is:
  105. * always unencrypted
  106. * unauthenticated, unless [`worker_replication_secret`](usage/configuration/config_documentation.md#worker_replication_secret)
  107. is configured
  108. ### Worker configuration
  109. In the config file for each worker, you must specify:
  110. * The type of worker ([`worker_app`](usage/configuration/config_documentation.md#worker_app)).
  111. The currently available worker applications are listed [below](#available-worker-applications).
  112. * A unique name for the worker ([`worker_name`](usage/configuration/config_documentation.md#worker_name)).
  113. * If handling HTTP requests, a [`worker_listeners`](usage/configuration/config_documentation.md#worker_listeners) option
  114. with an `http` listener.
  115. * **Synapse 1.72 and older:** if handling the `^/_matrix/client/v3/keys/upload` endpoint, the HTTP URI for
  116. the main process (`worker_main_http_uri`). This config option is no longer required and is ignored when running Synapse 1.73 and newer.
  117. For example:
  118. ```yaml
  119. {{#include systemd-with-workers/workers/generic_worker.yaml}}
  120. ```
  121. ...is a full configuration for a generic worker instance, which will expose a
  122. plain HTTP endpoint on port 8083 separately serving various endpoints, e.g.
  123. `/sync`, which are listed below.
  124. Obviously you should configure your reverse-proxy to route the relevant
  125. endpoints to the worker (`localhost:8083` in the above example).
  126. ### Running Synapse with workers
  127. Finally, you need to start your worker processes. This can be done with either
  128. `synctl` or your distribution's preferred service manager such as `systemd`. We
  129. recommend the use of `systemd` where available: for information on setting up
  130. `systemd` to start synapse workers, see
  131. [Systemd with Workers](systemd-with-workers/). To use `synctl`, see
  132. [Using synctl with Workers](synctl_workers.md).
  133. ## Start Synapse with Poetry
  134. The following applies to Synapse installations that have been installed from source using `poetry`.
  135. You can start the main Synapse process with Poetry by running the following command:
  136. ```console
  137. poetry run synapse_homeserver --config-file [your homeserver.yaml]
  138. ```
  139. For worker setups, you can run the following command
  140. ```console
  141. poetry run synapse_worker --config-file [your homeserver.yaml] --config-file [your worker.yaml]
  142. ```
  143. ## Available worker applications
  144. ### `synapse.app.generic_worker`
  145. This worker can handle API requests matching the following regular expressions.
  146. These endpoints can be routed to any worker. If a worker is set up to handle a
  147. stream then, for maximum efficiency, additional endpoints should be routed to that
  148. worker: refer to the [stream writers](#stream-writers) section below for further
  149. information.
  150. # Sync requests
  151. ^/_matrix/client/(r0|v3)/sync$
  152. ^/_matrix/client/(api/v1|r0|v3)/events$
  153. ^/_matrix/client/(api/v1|r0|v3)/initialSync$
  154. ^/_matrix/client/(api/v1|r0|v3)/rooms/[^/]+/initialSync$
  155. # Federation requests
  156. ^/_matrix/federation/v1/event/
  157. ^/_matrix/federation/v1/state/
  158. ^/_matrix/federation/v1/state_ids/
  159. ^/_matrix/federation/v1/backfill/
  160. ^/_matrix/federation/v1/get_missing_events/
  161. ^/_matrix/federation/v1/publicRooms
  162. ^/_matrix/federation/v1/query/
  163. ^/_matrix/federation/v1/make_join/
  164. ^/_matrix/federation/v1/make_leave/
  165. ^/_matrix/federation/(v1|v2)/send_join/
  166. ^/_matrix/federation/(v1|v2)/send_leave/
  167. ^/_matrix/federation/(v1|v2)/invite/
  168. ^/_matrix/federation/v1/event_auth/
  169. ^/_matrix/federation/v1/timestamp_to_event/
  170. ^/_matrix/federation/v1/exchange_third_party_invite/
  171. ^/_matrix/federation/v1/user/devices/
  172. ^/_matrix/key/v2/query
  173. ^/_matrix/federation/v1/hierarchy/
  174. # Inbound federation transaction request
  175. ^/_matrix/federation/v1/send/
  176. # Client API requests
  177. ^/_matrix/client/(api/v1|r0|v3|unstable)/createRoom$
  178. ^/_matrix/client/(api/v1|r0|v3|unstable)/publicRooms$
  179. ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/joined_members$
  180. ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/context/.*$
  181. ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/members$
  182. ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/state$
  183. ^/_matrix/client/v1/rooms/.*/hierarchy$
  184. ^/_matrix/client/(v1|unstable)/rooms/.*/relations/
  185. ^/_matrix/client/v1/rooms/.*/threads$
  186. ^/_matrix/client/unstable/im.nheko.summary/rooms/.*/summary$
  187. ^/_matrix/client/(r0|v3|unstable)/account/3pid$
  188. ^/_matrix/client/(r0|v3|unstable)/account/whoami$
  189. ^/_matrix/client/(r0|v3|unstable)/devices$
  190. ^/_matrix/client/versions$
  191. ^/_matrix/client/(api/v1|r0|v3|unstable)/voip/turnServer$
  192. ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/event/
  193. ^/_matrix/client/(api/v1|r0|v3|unstable)/joined_rooms$
  194. ^/_matrix/client/v1/rooms/.*/timestamp_to_event$
  195. ^/_matrix/client/(api/v1|r0|v3|unstable/.*)/rooms/.*/aliases
  196. ^/_matrix/client/(api/v1|r0|v3|unstable)/search$
  197. ^/_matrix/client/(r0|v3|unstable)/user/.*/filter(/|$)
  198. ^/_matrix/client/(api/v1|r0|v3|unstable)/directory/room/.*$
  199. ^/_matrix/client/(r0|v3|unstable)/capabilities$
  200. ^/_matrix/client/(r0|v3|unstable)/notifications$
  201. # Encryption requests
  202. ^/_matrix/client/(r0|v3|unstable)/keys/query$
  203. ^/_matrix/client/(r0|v3|unstable)/keys/changes$
  204. ^/_matrix/client/(r0|v3|unstable)/keys/claim$
  205. ^/_matrix/client/(r0|v3|unstable)/room_keys/
  206. ^/_matrix/client/(r0|v3|unstable)/keys/upload/
  207. # Registration/login requests
  208. ^/_matrix/client/(api/v1|r0|v3|unstable)/login$
  209. ^/_matrix/client/(r0|v3|unstable)/register$
  210. ^/_matrix/client/(r0|v3|unstable)/register/available$
  211. ^/_matrix/client/v1/register/m.login.registration_token/validity$
  212. ^/_matrix/client/(r0|v3|unstable)/password_policy$
  213. # Event sending requests
  214. ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/redact
  215. ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/send
  216. ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/state/
  217. ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/(join|invite|leave|ban|unban|kick)$
  218. ^/_matrix/client/(api/v1|r0|v3|unstable)/join/
  219. ^/_matrix/client/(api/v1|r0|v3|unstable)/knock/
  220. ^/_matrix/client/(api/v1|r0|v3|unstable)/profile/
  221. # Account data requests
  222. ^/_matrix/client/(r0|v3|unstable)/.*/tags
  223. ^/_matrix/client/(r0|v3|unstable)/.*/account_data
  224. # Receipts requests
  225. ^/_matrix/client/(r0|v3|unstable)/rooms/.*/receipt
  226. ^/_matrix/client/(r0|v3|unstable)/rooms/.*/read_markers
  227. # Presence requests
  228. ^/_matrix/client/(api/v1|r0|v3|unstable)/presence/
  229. # User directory search requests
  230. ^/_matrix/client/(r0|v3|unstable)/user_directory/search$
  231. Additionally, the following REST endpoints can be handled for GET requests:
  232. ^/_matrix/client/(api/v1|r0|v3|unstable)/pushrules/
  233. Pagination requests can also be handled, but all requests for a given
  234. room must be routed to the same instance. Additionally, care must be taken to
  235. ensure that the purge history admin API is not used while pagination requests
  236. for the room are in flight:
  237. ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/messages$
  238. Additionally, the following endpoints should be included if Synapse is configured
  239. to use SSO (you only need to include the ones for whichever SSO provider you're
  240. using):
  241. # for all SSO providers
  242. ^/_matrix/client/(api/v1|r0|v3|unstable)/login/sso/redirect
  243. ^/_synapse/client/pick_idp$
  244. ^/_synapse/client/pick_username
  245. ^/_synapse/client/new_user_consent$
  246. ^/_synapse/client/sso_register$
  247. # OpenID Connect requests.
  248. ^/_synapse/client/oidc/callback$
  249. # SAML requests.
  250. ^/_synapse/client/saml2/authn_response$
  251. # CAS requests.
  252. ^/_matrix/client/(api/v1|r0|v3|unstable)/login/cas/ticket$
  253. Ensure that all SSO logins go to a single process.
  254. For multiple workers not handling the SSO endpoints properly, see
  255. [#7530](https://github.com/matrix-org/synapse/issues/7530) and
  256. [#9427](https://github.com/matrix-org/synapse/issues/9427).
  257. Note that a [HTTP listener](usage/configuration/config_documentation.md#listeners)
  258. with `client` and `federation` `resources` must be configured in the
  259. [`worker_listeners`](usage/configuration/config_documentation.md#worker_listeners)
  260. option in the worker config.
  261. #### Load balancing
  262. It is possible to run multiple instances of this worker app, with incoming requests
  263. being load-balanced between them by the reverse-proxy. However, different endpoints
  264. have different characteristics and so admins
  265. may wish to run multiple groups of workers handling different endpoints so that
  266. load balancing can be done in different ways.
  267. For `/sync` and `/initialSync` requests it will be more efficient if all
  268. requests from a particular user are routed to a single instance. This can
  269. be done in reverse proxy by extracting username part from the users access token.
  270. Admins may additionally wish to separate out `/sync`
  271. requests that have a `since` query parameter from those that don't (and
  272. `/initialSync`), as requests that don't are known as "initial sync" that happens
  273. when a user logs in on a new device and can be *very* resource intensive, so
  274. isolating these requests will stop them from interfering with other users ongoing
  275. syncs.
  276. Example `nginx` configuration snippet that handles the cases above. This is just an
  277. example and probably requires some changes according to your particular setup:
  278. ```nginx
  279. # Choose sync worker based on the existence of "since" query parameter
  280. map $arg_since $sync {
  281. default synapse_sync;
  282. '' synapse_initial_sync;
  283. }
  284. # Extract username from access token passed as URL parameter
  285. map $arg_access_token $accesstoken_from_urlparam {
  286. # Defaults to just passing back the whole accesstoken
  287. default $arg_access_token;
  288. # Try to extract username part from accesstoken URL parameter
  289. "~syt_(?<username>.*?)_.*" $username;
  290. }
  291. # Extract username from access token passed as authorization header
  292. map $http_authorization $mxid_localpart {
  293. # Defaults to just passing back the whole accesstoken
  294. default $http_authorization;
  295. # Try to extract username part from accesstoken header
  296. "~Bearer syt_(?<username>.*?)_.*" $username;
  297. # if no authorization-header exist, try mapper for URL parameter "access_token"
  298. "" $accesstoken_from_urlparam;
  299. }
  300. upstream synapse_initial_sync {
  301. # Use the username mapper result for hash key
  302. hash $mxid_localpart consistent;
  303. server 127.0.0.1:8016;
  304. server 127.0.0.1:8036;
  305. }
  306. upstream synapse_sync {
  307. # Use the username mapper result for hash key
  308. hash $mxid_localpart consistent;
  309. server 127.0.0.1:8013;
  310. server 127.0.0.1:8037;
  311. server 127.0.0.1:8038;
  312. server 127.0.0.1:8039;
  313. }
  314. # Sync initial/normal
  315. location ~ ^/_matrix/client/(r0|v3)/sync$ {
  316. proxy_pass http://$sync;
  317. }
  318. # Normal sync
  319. location ~ ^/_matrix/client/(api/v1|r0|v3)/events$ {
  320. proxy_pass http://synapse_sync;
  321. }
  322. # Initial_sync
  323. location ~ ^/_matrix/client/(api/v1|r0|v3)/initialSync$ {
  324. proxy_pass http://synapse_initial_sync;
  325. }
  326. location ~ ^/_matrix/client/(api/v1|r0|v3)/rooms/[^/]+/initialSync$ {
  327. proxy_pass http://synapse_initial_sync;
  328. }
  329. ```
  330. Federation and client requests can be balanced via simple round robin.
  331. The inbound federation transaction request `^/_matrix/federation/v1/send/`
  332. should be balanced by source IP so that transactions from the same remote server
  333. go to the same process.
  334. Registration/login requests can be handled separately purely to help ensure that
  335. unexpected load doesn't affect new logins and sign ups.
  336. Finally, event sending requests can be balanced by the room ID in the URI (or
  337. the full URI, or even just round robin), the room ID is the path component after
  338. `/rooms/`. If there is a large bridge connected that is sending or may send lots
  339. of events, then a dedicated set of workers can be provisioned to limit the
  340. effects of bursts of events from that bridge on events sent by normal users.
  341. #### Stream writers
  342. Additionally, the writing of specific streams (such as events) can be moved off
  343. of the main process to a particular worker.
  344. To enable this, the worker must have:
  345. * An [HTTP `replication` listener](usage/configuration/config_documentation.md#listeners) configured,
  346. * Have a [`worker_name`](usage/configuration/config_documentation.md#worker_name)
  347. and be listed in the [`instance_map`](usage/configuration/config_documentation.md#instance_map)
  348. config.
  349. * Have the main process declared on the [`instance_map`](usage/configuration/config_documentation.md#instance_map) as well.
  350. Note: The same worker can handle multiple streams, but unless otherwise documented,
  351. each stream can only have a single writer.
  352. For example, to move event persistence off to a dedicated worker, the shared
  353. configuration would include:
  354. ```yaml
  355. instance_map:
  356. main:
  357. host: localhost
  358. port: 8030
  359. event_persister1:
  360. host: localhost
  361. port: 8034
  362. stream_writers:
  363. events: event_persister1
  364. ```
  365. An example for a stream writer instance:
  366. ```yaml
  367. {{#include systemd-with-workers/workers/event_persister.yaml}}
  368. ```
  369. Some of the streams have associated endpoints which, for maximum efficiency, should
  370. be routed to the workers handling that stream. See below for the currently supported
  371. streams and the endpoints associated with them:
  372. ##### The `events` stream
  373. The `events` stream experimentally supports having multiple writer workers, where load
  374. is sharded between them by room ID. Each writer is called an _event persister_. They are
  375. responsible for
  376. - receiving new events,
  377. - linking them to those already in the room [DAG](development/room-dag-concepts.md),
  378. - persisting them to the DB, and finally
  379. - updating the events stream.
  380. Because load is sharded in this way, you *must* restart all worker instances when
  381. adding or removing event persisters.
  382. An `event_persister` should not be mistaken for an `event_creator`.
  383. An `event_creator` listens for requests from clients to create new events and does
  384. so. It will then pass those events over HTTP replication to any configured event
  385. persisters (or the main process if none are configured).
  386. Note that `event_creator`s and `event_persister`s are implemented using the same
  387. [`synapse.app.generic_worker`](#synapseappgeneric_worker).
  388. An example [`stream_writers`](usage/configuration/config_documentation.md#stream_writers)
  389. configuration with multiple writers:
  390. ```yaml
  391. stream_writers:
  392. events:
  393. - event_persister1
  394. - event_persister2
  395. ```
  396. ##### The `typing` stream
  397. The following endpoints should be routed directly to the worker configured as
  398. the stream writer for the `typing` stream:
  399. ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/typing
  400. ##### The `to_device` stream
  401. The following endpoints should be routed directly to the worker configured as
  402. the stream writer for the `to_device` stream:
  403. ^/_matrix/client/(r0|v3|unstable)/sendToDevice/
  404. ##### The `account_data` stream
  405. The following endpoints should be routed directly to the worker configured as
  406. the stream writer for the `account_data` stream:
  407. ^/_matrix/client/(r0|v3|unstable)/.*/tags
  408. ^/_matrix/client/(r0|v3|unstable)/.*/account_data
  409. ##### The `receipts` stream
  410. The following endpoints should be routed directly to the worker configured as
  411. the stream writer for the `receipts` stream:
  412. ^/_matrix/client/(r0|v3|unstable)/rooms/.*/receipt
  413. ^/_matrix/client/(r0|v3|unstable)/rooms/.*/read_markers
  414. ##### The `presence` stream
  415. The following endpoints should be routed directly to the worker configured as
  416. the stream writer for the `presence` stream:
  417. ^/_matrix/client/(api/v1|r0|v3|unstable)/presence/
  418. #### Restrict outbound federation traffic to a specific set of workers
  419. The
  420. [`outbound_federation_restricted_to`](usage/configuration/config_documentation.md#outbound_federation_restricted_to)
  421. configuration is useful to make sure outbound federation traffic only goes through a
  422. specified subset of workers. This allows you to set more strict access controls (like a
  423. firewall) for all workers and only allow the `federation_sender`'s to contact the
  424. outside world.
  425. ```yaml
  426. instance_map:
  427. main:
  428. host: localhost
  429. port: 8030
  430. federation_sender1:
  431. host: localhost
  432. port: 8034
  433. outbound_federation_restricted_to:
  434. - federation_sender1
  435. worker_replication_secret: "secret_secret"
  436. ```
  437. #### Background tasks
  438. There is also support for moving background tasks to a separate
  439. worker. Background tasks are run periodically or started via replication. Exactly
  440. which tasks are configured to run depends on your Synapse configuration (e.g. if
  441. stats is enabled). This worker doesn't handle any REST endpoints itself.
  442. To enable this, the worker must have a unique
  443. [`worker_name`](usage/configuration/config_documentation.md#worker_name)
  444. and can be configured to run background tasks. For example, to move background tasks
  445. to a dedicated worker, the shared configuration would include:
  446. ```yaml
  447. run_background_tasks_on: background_worker
  448. ```
  449. You might also wish to investigate the
  450. [`update_user_directory_from_worker`](#updating-the-user-directory) and
  451. [`media_instance_running_background_jobs`](#synapseappmedia_repository) settings.
  452. An example for a dedicated background worker instance:
  453. ```yaml
  454. {{#include systemd-with-workers/workers/background_worker.yaml}}
  455. ```
  456. #### Updating the User Directory
  457. You can designate one generic worker to update the user directory.
  458. Specify its name in the [shared configuration](usage/configuration/config_documentation.md#update_user_directory_from_worker)
  459. as follows:
  460. ```yaml
  461. update_user_directory_from_worker: worker_name
  462. ```
  463. This work cannot be load-balanced; please ensure the main process is restarted
  464. after setting this option in the shared configuration!
  465. User directory updates allow REST endpoints matching the following regular
  466. expressions to work:
  467. ^/_matrix/client/(r0|v3|unstable)/user_directory/search$
  468. The above endpoints can be routed to any worker, though you may choose to route
  469. it to the chosen user directory worker.
  470. This style of configuration supersedes the legacy `synapse.app.user_dir`
  471. worker application type.
  472. #### Notifying Application Services
  473. You can designate one generic worker to send output traffic to Application Services.
  474. Doesn't handle any REST endpoints itself, but you should specify its name in the
  475. [shared configuration](usage/configuration/config_documentation.md#notify_appservices_from_worker)
  476. as follows:
  477. ```yaml
  478. notify_appservices_from_worker: worker_name
  479. ```
  480. This work cannot be load-balanced; please ensure the main process is restarted
  481. after setting this option in the shared configuration!
  482. This style of configuration supersedes the legacy `synapse.app.appservice`
  483. worker application type.
  484. #### Push Notifications
  485. You can designate generic worker to sending push notifications to
  486. a [push gateway](https://spec.matrix.org/v1.5/push-gateway-api/) such as
  487. [sygnal](https://github.com/matrix-org/sygnal) and email.
  488. This will stop the main process sending push notifications.
  489. The workers responsible for sending push notifications can be defined using the
  490. [`pusher_instances`](usage/configuration/config_documentation.md#pusher_instances)
  491. option. For example:
  492. ```yaml
  493. pusher_instances:
  494. - pusher_worker1
  495. - pusher_worker2
  496. ```
  497. Multiple workers can be added to this map, in which case the work is balanced
  498. across them. Ensure the main process and all pusher workers are restarted after changing
  499. this option.
  500. These workers don't need to accept incoming HTTP requests to send push notifications,
  501. so no additional reverse proxy configuration is required for pusher workers.
  502. This style of configuration supersedes the legacy `synapse.app.pusher`
  503. worker application type.
  504. ### `synapse.app.pusher`
  505. It is likely this option will be deprecated in the future and is not recommended for new
  506. installations. Instead, [use `synapse.app.generic_worker` with the `pusher_instances`](#push-notifications).
  507. Handles sending push notifications to sygnal and email. Doesn't handle any
  508. REST endpoints itself, but you should set
  509. [`start_pushers: false`](usage/configuration/config_documentation.md#start_pushers) in the
  510. shared configuration file to stop the main synapse sending push notifications.
  511. To run multiple instances at once the
  512. [`pusher_instances`](usage/configuration/config_documentation.md#pusher_instances)
  513. option should list all pusher instances by their
  514. [`worker_name`](usage/configuration/config_documentation.md#worker_name), e.g.:
  515. ```yaml
  516. start_pushers: false
  517. pusher_instances:
  518. - pusher_worker1
  519. - pusher_worker2
  520. ```
  521. An example for a pusher instance:
  522. ```yaml
  523. {{#include systemd-with-workers/workers/pusher_worker.yaml}}
  524. ```
  525. ### `synapse.app.appservice`
  526. **Deprecated as of Synapse v1.59.** [Use `synapse.app.generic_worker` with the
  527. `notify_appservices_from_worker` option instead.](#notifying-application-services)
  528. Handles sending output traffic to Application Services. Doesn't handle any
  529. REST endpoints itself, but you should set `notify_appservices: False` in the
  530. shared configuration file to stop the main synapse sending appservice notifications.
  531. Note this worker cannot be load-balanced: only one instance should be active.
  532. ### `synapse.app.federation_sender`
  533. It is likely this option will be deprecated in the future and not recommended for
  534. new installations. Instead, [use `synapse.app.generic_worker` with the `federation_sender_instances`](usage/configuration/config_documentation.md#federation_sender_instances).
  535. Handles sending federation traffic to other servers. Doesn't handle any
  536. REST endpoints itself, but you should set
  537. [`send_federation: false`](usage/configuration/config_documentation.md#send_federation)
  538. in the shared configuration file to stop the main synapse sending this traffic.
  539. If running multiple federation senders then you must list each
  540. instance in the
  541. [`federation_sender_instances`](usage/configuration/config_documentation.md#federation_sender_instances)
  542. option by their
  543. [`worker_name`](usage/configuration/config_documentation.md#worker_name).
  544. All instances must be stopped and started when adding or removing instances.
  545. For example:
  546. ```yaml
  547. send_federation: false
  548. federation_sender_instances:
  549. - federation_sender1
  550. - federation_sender2
  551. ```
  552. An example for a federation sender instance:
  553. ```yaml
  554. {{#include systemd-with-workers/workers/federation_sender.yaml}}
  555. ```
  556. ### `synapse.app.media_repository`
  557. Handles the media repository. It can handle all endpoints starting with:
  558. /_matrix/media/
  559. ... and the following regular expressions matching media-specific administration APIs:
  560. ^/_synapse/admin/v1/purge_media_cache$
  561. ^/_synapse/admin/v1/room/.*/media.*$
  562. ^/_synapse/admin/v1/user/.*/media.*$
  563. ^/_synapse/admin/v1/media/.*$
  564. ^/_synapse/admin/v1/quarantine_media/.*$
  565. ^/_synapse/admin/v1/users/.*/media$
  566. You should also set
  567. [`enable_media_repo: False`](usage/configuration/config_documentation.md#enable_media_repo)
  568. in the shared configuration
  569. file to stop the main synapse running background jobs related to managing the
  570. media repository. Note that doing so will prevent the main process from being
  571. able to handle the above endpoints.
  572. In the `media_repository` worker configuration file, configure the
  573. [HTTP listener](usage/configuration/config_documentation.md#listeners) to
  574. expose the `media` resource. For example:
  575. ```yaml
  576. {{#include systemd-with-workers/workers/media_worker.yaml}}
  577. ```
  578. Note that if running multiple media repositories they must be on the same server
  579. and you must specify a single instance to run the background tasks in the
  580. [shared configuration](usage/configuration/config_documentation.md#media_instance_running_background_jobs),
  581. e.g.:
  582. ```yaml
  583. media_instance_running_background_jobs: "media-repository-1"
  584. ```
  585. Note that if a reverse proxy is used , then `/_matrix/media/` must be routed for both inbound client and federation requests (if they are handled separately).
  586. ### `synapse.app.user_dir`
  587. **Deprecated as of Synapse v1.59.** [Use `synapse.app.generic_worker` with the
  588. `update_user_directory_from_worker` option instead.](#updating-the-user-directory)
  589. Handles searches in the user directory. It can handle REST endpoints matching
  590. the following regular expressions:
  591. ^/_matrix/client/(r0|v3|unstable)/user_directory/search$
  592. When using this worker you must also set `update_user_directory: false` in the
  593. shared configuration file to stop the main synapse running background
  594. jobs related to updating the user directory.
  595. Above endpoint is not *required* to be routed to this worker. By default,
  596. `update_user_directory` is set to `true`, which means the main process
  597. will handle updates. All workers configured with `client` can handle the above
  598. endpoint as long as either this worker or the main process are configured to
  599. handle it, and are online.
  600. If `update_user_directory` is set to `false`, and this worker is not running,
  601. the above endpoint may give outdated results.
  602. ### Historical apps
  603. The following used to be separate worker application types, but are now
  604. equivalent to `synapse.app.generic_worker`:
  605. * `synapse.app.client_reader`
  606. * `synapse.app.event_creator`
  607. * `synapse.app.federation_reader`
  608. * `synapse.app.federation_sender`
  609. * `synapse.app.frontend_proxy`
  610. * `synapse.app.pusher`
  611. * `synapse.app.synchrotron`
  612. ## Migration from old config
  613. A main change that has occurred is the merging of worker apps into
  614. `synapse.app.generic_worker`. This change is backwards compatible and so no
  615. changes to the config are required.
  616. To migrate apps to use `synapse.app.generic_worker` simply update the
  617. `worker_app` option in the worker configs, and where worker are started (e.g.
  618. in systemd service files, but not required for synctl).
  619. ## Architectural diagram
  620. The following shows an example setup using Redis and a reverse proxy:
  621. ```
  622. Clients & Federation
  623. |
  624. v
  625. +-----------+
  626. | |
  627. | Reverse |
  628. | Proxy |
  629. | |
  630. +-----------+
  631. | | |
  632. | | | HTTP requests
  633. +-------------------+ | +-----------+
  634. | +---+ |
  635. | | |
  636. v v v
  637. +--------------+ +--------------+ +--------------+ +--------------+
  638. | Main | | Generic | | Generic | | Event |
  639. | Process | | Worker 1 | | Worker 2 | | Persister |
  640. +--------------+ +--------------+ +--------------+ +--------------+
  641. ^ ^ | ^ | | ^ | | ^ ^
  642. | | | | | | | | | | |
  643. | | | | | HTTP | | | | | |
  644. | +----------+<--|---|---------+<--|---|---------+ | |
  645. | | +-------------|-->+-------------+ |
  646. | | | |
  647. | | | |
  648. v v v v
  649. ======================================================================
  650. Redis pub/sub channel
  651. ```