replication.rst 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. Replication Architecture
  2. ========================
  3. Motivation
  4. ----------
  5. We'd like to be able to split some of the work that synapse does into multiple
  6. python processes. In theory multiple synapse processes could share a single
  7. postgresql database and we'd scale up by running more synapse processes.
  8. However much of synapse assumes that only one process is interacting with the
  9. database, both for assigning unique identifiers when inserting into tables,
  10. notifying components about new updates, and for invalidating its caches.
  11. So running multiple copies of the current code isn't an option. One way to
  12. run multiple processes would be to have a single writer process and multiple
  13. reader processes connected to the same database. In order to do this we'd need
  14. a way for the reader process to invalidate its in-memory caches when an update
  15. happens on the writer. One way to do this is for the writer to present an
  16. append-only log of updates which the readers can consume to invalidate their
  17. caches and to push updates to listening clients or pushers.
  18. Synapse already stores much of its data as an append-only log so that it can
  19. correctly respond to /sync requests so the amount of code changes needed to
  20. expose the append-only log to the readers should be fairly minimal.
  21. Architecture
  22. ------------
  23. The Replication API
  24. ~~~~~~~~~~~~~~~~~~~
  25. Synapse will optionally expose a long poll HTTP API for extracting updates. The
  26. API will have a similar shape to /sync in that clients provide tokens
  27. indicating where in the log they have reached and a timeout. The synapse server
  28. then either responds with updates immediately if it already has updates or it
  29. waits until the timeout for more updates. If the timeout expires and nothing
  30. happened then the server returns an empty response.
  31. However unlike the /sync API this replication API is returning synapse specific
  32. data rather than trying to implement a matrix specification. The replication
  33. results are returned as arrays of rows where the rows are mostly lifted
  34. directly from the database. This avoids unnecessary JSON parsing on the server
  35. and hopefully avoids an impedance mismatch between the data returned and the
  36. required updates to the datastore.
  37. This does not replicate all the database tables as many of the database tables
  38. are indexes that can be recovered from the contents of other tables.
  39. The format and parameters for the api are documented in
  40. ``synapse/replication/resource.py``.
  41. The Slaved DataStore
  42. ~~~~~~~~~~~~~~~~~~~~
  43. There are read-only version of the synapse storage layer in
  44. ``synapse/replication/slave/storage`` that use the response of the replication
  45. API to invalidate their caches.