workers.rst 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. Scaling synapse via workers
  2. ===========================
  3. Synapse has experimental support for splitting out functionality into
  4. multiple separate python processes, helping greatly with scalability. These
  5. processes are called 'workers', and are (eventually) intended to scale
  6. horizontally independently.
  7. All of the below is highly experimental and subject to change as Synapse evolves,
  8. but documenting it here to help folks needing highly scalable Synapses similar
  9. to the one running matrix.org!
  10. All processes continue to share the same database instance, and as such, workers
  11. only work with postgres based synapse deployments (sharing a single sqlite
  12. across multiple processes is a recipe for disaster, plus you should be using
  13. postgres anyway if you care about scalability).
  14. The workers communicate with the master synapse process via a synapse-specific
  15. TCP protocol called 'replication' - analogous to MySQL or Postgres style
  16. database replication; feeding a stream of relevant data to the workers so they
  17. can be kept in sync with the main synapse process and database state.
  18. Configuration
  19. -------------
  20. To make effective use of the workers, you will need to configure an HTTP
  21. reverse-proxy such as nginx or haproxy, which will direct incoming requests to
  22. the correct worker, or to the main synapse instance. Note that this includes
  23. requests made to the federation port. The caveats regarding running a
  24. reverse-proxy on the federation port still apply (see
  25. https://github.com/matrix-org/synapse/blob/master/README.rst#reverse-proxying-the-federation-port).
  26. To enable workers, you need to add two replication listeners to the master
  27. synapse, e.g.::
  28. listeners:
  29. # The TCP replication port
  30. - port: 9092
  31. bind_address: '127.0.0.1'
  32. type: replication
  33. # The HTTP replication port
  34. - port: 9093
  35. bind_address: '127.0.0.1'
  36. type: http
  37. resources:
  38. - names: [replication]
  39. Under **no circumstances** should these replication API listeners be exposed to
  40. the public internet; it currently implements no authentication whatsoever and is
  41. unencrypted.
  42. (Roughly, the TCP port is used for streaming data from the master to the
  43. workers, and the HTTP port for the workers to send data to the main
  44. synapse process.)
  45. You then create a set of configs for the various worker processes. These
  46. should be worker configuration files, and should be stored in a dedicated
  47. subdirectory, to allow synctl to manipulate them.
  48. Each worker configuration file inherits the configuration of the main homeserver
  49. configuration file. You can then override configuration specific to that worker,
  50. e.g. the HTTP listener that it provides (if any); logging configuration; etc.
  51. You should minimise the number of overrides though to maintain a usable config.
  52. You must specify the type of worker application (``worker_app``). The currently
  53. available worker applications are listed below. You must also specify the
  54. replication endpoints that it's talking to on the main synapse process.
  55. ``worker_replication_host`` should specify the host of the main synapse,
  56. ``worker_replication_port`` should point to the TCP replication listener port and
  57. ``worker_replication_http_port`` should point to the HTTP replication port.
  58. Currently, only the ``event_creator`` worker requires specifying
  59. ``worker_replication_http_port``.
  60. For instance::
  61. worker_app: synapse.app.synchrotron
  62. # The replication listener on the synapse to talk to.
  63. worker_replication_host: 127.0.0.1
  64. worker_replication_port: 9092
  65. worker_replication_http_port: 9093
  66. worker_listeners:
  67. - type: http
  68. port: 8083
  69. resources:
  70. - names:
  71. - client
  72. worker_daemonize: True
  73. worker_pid_file: /home/matrix/synapse/synchrotron.pid
  74. worker_log_config: /home/matrix/synapse/config/synchrotron_log_config.yaml
  75. ...is a full configuration for a synchrotron worker instance, which will expose a
  76. plain HTTP ``/sync`` endpoint on port 8083 separately from the ``/sync`` endpoint provided
  77. by the main synapse.
  78. Obviously you should configure your reverse-proxy to route the relevant
  79. endpoints to the worker (``localhost:8083`` in the above example).
  80. Finally, to actually run your worker-based synapse, you must pass synctl the -a
  81. commandline option to tell it to operate on all the worker configurations found
  82. in the given directory, e.g.::
  83. synctl -a $CONFIG/workers start
  84. Currently one should always restart all workers when restarting or upgrading
  85. synapse, unless you explicitly know it's safe not to. For instance, restarting
  86. synapse without restarting all the synchrotrons may result in broken typing
  87. notifications.
  88. To manipulate a specific worker, you pass the -w option to synctl::
  89. synctl -w $CONFIG/workers/synchrotron.yaml restart
  90. Available worker applications
  91. -----------------------------
  92. ``synapse.app.pusher``
  93. ~~~~~~~~~~~~~~~~~~~~~~
  94. Handles sending push notifications to sygnal and email. Doesn't handle any
  95. REST endpoints itself, but you should set ``start_pushers: False`` in the
  96. shared configuration file to stop the main synapse sending these notifications.
  97. Note this worker cannot be load-balanced: only one instance should be active.
  98. ``synapse.app.synchrotron``
  99. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  100. The synchrotron handles ``sync`` requests from clients. In particular, it can
  101. handle REST endpoints matching the following regular expressions::
  102. ^/_matrix/client/(v2_alpha|r0)/sync$
  103. ^/_matrix/client/(api/v1|v2_alpha|r0)/events$
  104. ^/_matrix/client/(api/v1|r0)/initialSync$
  105. ^/_matrix/client/(api/v1|r0)/rooms/[^/]+/initialSync$
  106. The above endpoints should all be routed to the synchrotron worker by the
  107. reverse-proxy configuration.
  108. It is possible to run multiple instances of the synchrotron to scale
  109. horizontally. In this case the reverse-proxy should be configured to
  110. load-balance across the instances, though it will be more efficient if all
  111. requests from a particular user are routed to a single instance. Extracting
  112. a userid from the access token is currently left as an exercise for the reader.
  113. ``synapse.app.appservice``
  114. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  115. Handles sending output traffic to Application Services. Doesn't handle any
  116. REST endpoints itself, but you should set ``notify_appservices: False`` in the
  117. shared configuration file to stop the main synapse sending these notifications.
  118. Note this worker cannot be load-balanced: only one instance should be active.
  119. ``synapse.app.federation_reader``
  120. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  121. Handles a subset of federation endpoints. In particular, it can handle REST
  122. endpoints matching the following regular expressions::
  123. ^/_matrix/federation/v1/event/
  124. ^/_matrix/federation/v1/state/
  125. ^/_matrix/federation/v1/state_ids/
  126. ^/_matrix/federation/v1/backfill/
  127. ^/_matrix/federation/v1/get_missing_events/
  128. ^/_matrix/federation/v1/publicRooms
  129. The above endpoints should all be routed to the federation_reader worker by the
  130. reverse-proxy configuration.
  131. ``synapse.app.federation_sender``
  132. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  133. Handles sending federation traffic to other servers. Doesn't handle any
  134. REST endpoints itself, but you should set ``send_federation: False`` in the
  135. shared configuration file to stop the main synapse sending this traffic.
  136. Note this worker cannot be load-balanced: only one instance should be active.
  137. ``synapse.app.media_repository``
  138. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  139. Handles the media repository. It can handle all endpoints starting with::
  140. /_matrix/media/
  141. You should also set ``enable_media_repo: False`` in the shared configuration
  142. file to stop the main synapse running background jobs related to managing the
  143. media repository.
  144. Note this worker cannot be load-balanced: only one instance should be active.
  145. ``synapse.app.client_reader``
  146. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  147. Handles client API endpoints. It can handle REST endpoints matching the
  148. following regular expressions::
  149. ^/_matrix/client/(api/v1|r0|unstable)/publicRooms$
  150. ``synapse.app.user_dir``
  151. ~~~~~~~~~~~~~~~~~~~~~~~~
  152. Handles searches in the user directory. It can handle REST endpoints matching
  153. the following regular expressions::
  154. ^/_matrix/client/(api/v1|r0|unstable)/user_directory/search$
  155. ``synapse.app.frontend_proxy``
  156. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  157. Proxies some frequently-requested client endpoints to add caching and remove
  158. load from the main synapse. It can handle REST endpoints matching the following
  159. regular expressions::
  160. ^/_matrix/client/(api/v1|r0|unstable)/keys/upload
  161. It will proxy any requests it cannot handle to the main synapse instance. It
  162. must therefore be configured with the location of the main instance, via
  163. the ``worker_main_http_uri`` setting in the frontend_proxy worker configuration
  164. file. For example::
  165. worker_main_http_uri: http://127.0.0.1:8008
  166. ``synapse.app.event_creator``
  167. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  168. Handles non-state event creation. It can handle REST endpoints matching::
  169. ^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/send
  170. It will create events locally and then send them on to the main synapse
  171. instance to be persisted and handled.