1
0

tcp_replication.html 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <!DOCTYPE HTML>
  2. <html lang="en" class="sidebar-visible no-js light">
  3. <head>
  4. <!-- Book generated using mdBook -->
  5. <meta charset="UTF-8">
  6. <title>TCP Replication - Synapse</title>
  7. <!-- Custom HTML head -->
  8. <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  9. <meta name="description" content="">
  10. <meta name="viewport" content="width=device-width, initial-scale=1">
  11. <meta name="theme-color" content="#ffffff" />
  12. <link rel="icon" href="favicon.svg">
  13. <link rel="shortcut icon" href="favicon.png">
  14. <link rel="stylesheet" href="css/variables.css">
  15. <link rel="stylesheet" href="css/general.css">
  16. <link rel="stylesheet" href="css/chrome.css">
  17. <link rel="stylesheet" href="css/print.css" media="print">
  18. <!-- Fonts -->
  19. <link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
  20. <link rel="stylesheet" href="fonts/fonts.css">
  21. <!-- Highlight.js Stylesheets -->
  22. <link rel="stylesheet" href="highlight.css">
  23. <link rel="stylesheet" href="tomorrow-night.css">
  24. <link rel="stylesheet" href="ayu-highlight.css">
  25. <!-- Custom theme stylesheets -->
  26. <link rel="stylesheet" href="docs/website_files/table-of-contents.css">
  27. <link rel="stylesheet" href="docs/website_files/remove-nav-buttons.css">
  28. <link rel="stylesheet" href="docs/website_files/indent-section-headers.css">
  29. <link rel="stylesheet" href="docs/website_files/version-picker.css">
  30. </head>
  31. <body>
  32. <!-- Provide site root to javascript -->
  33. <script type="text/javascript">
  34. var path_to_root = "";
  35. var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
  36. </script>
  37. <!-- Work around some values being stored in localStorage wrapped in quotes -->
  38. <script type="text/javascript">
  39. try {
  40. var theme = localStorage.getItem('mdbook-theme');
  41. var sidebar = localStorage.getItem('mdbook-sidebar');
  42. if (theme.startsWith('"') && theme.endsWith('"')) {
  43. localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
  44. }
  45. if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
  46. localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
  47. }
  48. } catch (e) { }
  49. </script>
  50. <!-- Set the theme before any content is loaded, prevents flash -->
  51. <script type="text/javascript">
  52. var theme;
  53. try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
  54. if (theme === null || theme === undefined) { theme = default_theme; }
  55. var html = document.querySelector('html');
  56. html.classList.remove('no-js')
  57. html.classList.remove('light')
  58. html.classList.add(theme);
  59. html.classList.add('js');
  60. </script>
  61. <!-- Hide / unhide sidebar before it is displayed -->
  62. <script type="text/javascript">
  63. var html = document.querySelector('html');
  64. var sidebar = 'hidden';
  65. if (document.body.clientWidth >= 1080) {
  66. try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
  67. sidebar = sidebar || 'visible';
  68. }
  69. html.classList.remove('sidebar-visible');
  70. html.classList.add("sidebar-" + sidebar);
  71. </script>
  72. <nav id="sidebar" class="sidebar" aria-label="Table of contents">
  73. <div class="sidebar-scrollbox">
  74. <ol class="chapter"><li class="chapter-item expanded affix "><li class="part-title">Introduction</li><li class="chapter-item expanded "><a href="welcome_and_overview.html">Welcome and Overview</a></li><li class="chapter-item expanded affix "><li class="part-title">Setup</li><li class="chapter-item expanded "><a href="setup/installation.html">Installation</a></li><li class="chapter-item expanded "><a href="postgres.html">Using Postgres</a></li><li class="chapter-item expanded "><a href="reverse_proxy.html">Configuring a Reverse Proxy</a></li><li class="chapter-item expanded "><a href="setup/forward_proxy.html">Configuring a Forward/Outbound Proxy</a></li><li class="chapter-item expanded "><a href="turn-howto.html">Configuring a Turn Server</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="setup/turn/coturn.html">coturn TURN server</a></li><li class="chapter-item expanded "><a href="setup/turn/eturnal.html">eturnal TURN server</a></li></ol></li><li class="chapter-item expanded "><a href="delegate.html">Delegation</a></li><li class="chapter-item expanded affix "><li class="part-title">Upgrading</li><li class="chapter-item expanded "><a href="upgrade.html">Upgrading between Synapse Versions</a></li><li class="chapter-item expanded affix "><li class="part-title">Usage</li><li class="chapter-item expanded "><a href="federate.html">Federation</a></li><li class="chapter-item expanded "><a href="usage/configuration/index.html">Configuration</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="usage/configuration/config_documentation.html">Configuration Manual</a></li><li class="chapter-item expanded "><a href="usage/configuration/homeserver_sample_config.html">Homeserver Sample Config File</a></li><li class="chapter-item expanded "><a href="usage/configuration/logging_sample_config.html">Logging Sample Config File</a></li><li class="chapter-item expanded "><a href="structured_logging.html">Structured Logging</a></li><li class="chapter-item expanded "><a href="templates.html">Templates</a></li><li class="chapter-item expanded "><a href="usage/configuration/user_authentication/index.html">User Authentication</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="usage/configuration/user_authentication/single_sign_on/index.html">Single-Sign On</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="openid.html">OpenID Connect</a></li><li class="chapter-item expanded "><a href="usage/configuration/user_authentication/single_sign_on/saml.html">SAML</a></li><li class="chapter-item expanded "><a href="usage/configuration/user_authentication/single_sign_on/cas.html">CAS</a></li><li class="chapter-item expanded "><a href="sso_mapping_providers.html">SSO Mapping Providers</a></li></ol></li><li class="chapter-item expanded "><a href="password_auth_providers.html">Password Auth Providers</a></li><li class="chapter-item expanded "><a href="jwt.html">JSON Web Tokens</a></li><li class="chapter-item expanded "><a href="usage/configuration/user_authentication/refresh_tokens.html">Refresh Tokens</a></li></ol></li><li class="chapter-item expanded "><a href="CAPTCHA_SETUP.html">Registration Captcha</a></li><li class="chapter-item expanded "><a href="application_services.html">Application Services</a></li><li class="chapter-item expanded "><a href="server_notices.html">Server Notices</a></li><li class="chapter-item expanded "><a href="consent_tracking.html">Consent Tracking</a></li><li class="chapter-item expanded "><a href="user_directory.html">User Directory</a></li><li class="chapter-item expanded "><a href="message_retention_policies.html">Message Retention Policies</a></li><li class="chapter-item expanded "><a href="modules/index.html">Pluggable Modules</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="modules/writing_a_module.html">Writing a module</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="modules/spam_checker_callbacks.html">Spam checker callbacks</a></li><li class="chapter-item expanded "><a href="modules/third_party_rules_callbacks.html">Third-party rules callbacks</a></li><li class="chapter-item expanded "><a href="modules/presence_router_callbacks.html">Presence router callbacks</a></li><li class="chapter-item expanded "><a href="modules/account_validity_callbacks.html">Account validity callbacks</a></li><li class="chapter-item expanded "><a href="modules/password_auth_provider_callbacks.html">Password auth provider callbacks</a></li><li class="chapter-item expanded "><a href="modules/background_update_controller_callbacks.html">Background update controller callbacks</a></li><li class="chapter-item expanded "><a href="modules/account_data_callbacks.html">Account data callbacks</a></li><li class="chapter-item expanded "><a href="modules/porting_legacy_module.html">Porting a legacy module to the new interface</a></li></ol></li></ol></li><li class="chapter-item expanded "><a href="workers.html">Workers</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="synctl_workers.html">Using synctl with Workers</a></li><li class="chapter-item expanded "><a href="systemd-with-workers/index.html">Systemd</a></li></ol></li></ol></li><li class="chapter-item expanded "><a href="usage/administration/index.html">Administration</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="usage/administration/admin_api/index.html">Admin API</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="admin_api/account_validity.html">Account Validity</a></li><li class="chapter-item expanded "><a href="usage/administration/admin_api/background_updates.html">Background Updates</a></li><li class="chapter-item expanded "><a href="admin_api/event_reports.html">Event Reports</a></li><li class="chapter-item expanded "><a href="admin_api/media_admin_api.html">Media</a></li><li class="chapter-item expanded "><a href="admin_api/purge_history_api.html">Purge History</a></li><li class="chapter-item expanded "><a href="admin_api/register_api.html">Register Users</a></li><li class="chapter-item expanded "><a href="usage/administration/admin_api/registration_tokens.html">Registration Tokens</a></li><li class="chapter-item expanded "><a href="admin_api/room_membership.html">Manipulate Room Membership</a></li><li class="chapter-item expanded "><a href="admin_api/rooms.html">Rooms</a></li><li class="chapter-item expanded "><a href="admin_api/server_notices.html">Server Notices</a></li><li class="chapter-item expanded "><a href="admin_api/statistics.html">Statistics</a></li><li class="chapter-item expanded "><a href="admin_api/user_admin_api.html">Users</a></li><li class="chapter-item expanded "><a href="admin_api/version_api.html">Server Version</a></li><li class="chapter-item expanded "><a href="usage/administration/admin_api/federation.html">Federation</a></li></ol></li><li class="chapter-item expanded "><a href="manhole.html">Manhole</a></li><li class="chapter-item expanded "><a href="metrics-howto.html">Monitoring</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="usage/administration/monitoring/reporting_homeserver_usage_statistics.html">Reporting Homeserver Usage Statistics</a></li></ol></li><li class="chapter-item expanded "><a href="usage/administration/monthly_active_users.html">Monthly Active Users</a></li><li class="chapter-item expanded "><a href="usage/administration/understanding_synapse_through_grafana_graphs.html">Understanding Synapse Through Grafana Graphs</a></li><li class="chapter-item expanded "><a href="usage/administration/useful_sql_for_admins.html">Useful SQL for Admins</a></li><li class="chapter-item expanded "><a href="usage/administration/database_maintenance_tools.html">Database Maintenance Tools</a></li><li class="chapter-item expanded "><a href="usage/administration/state_groups.html">State Groups</a></li><li class="chapter-item expanded "><a href="usage/administration/request_log.html">Request log format</a></li><li class="chapter-item expanded "><a href="usage/administration/admin_faq.html">Admin FAQ</a></li><li class="chapter-item expanded "><div>Scripts</div></li></ol></li><li class="chapter-item expanded "><li class="part-title">Development</li><li class="chapter-item expanded "><a href="development/contributing_guide.html">Contributing Guide</a></li><li class="chapter-item expanded "><a href="code_style.html">Code Style</a></li><li class="chapter-item expanded "><a href="development/reviews.html">Reviewing Code</a></li><li class="chapter-item expanded "><a href="development/releases.html">Release Cycle</a></li><li class="chapter-item expanded "><a href="development/git.html">Git Usage</a></li><li class="chapter-item expanded "><div>Testing</div></li><li><ol class="section"><li class="chapter-item expanded "><a href="development/demo.html">Demo scripts</a></li></ol></li><li class="chapter-item expanded "><a href="opentracing.html">OpenTracing</a></li><li class="chapter-item expanded "><a href="development/database_schema.html">Database Schemas</a></li><li class="chapter-item expanded "><a href="development/experimental_features.html">Experimental features</a></li><li class="chapter-item expanded "><a href="development/dependencies.html">Dependency management</a></li><li class="chapter-item expanded "><div>Synapse Architecture</div></li><li><ol class="section"><li class="chapter-item expanded "><a href="development/synapse_architecture/cancellation.html">Cancellation</a></li><li class="chapter-item expanded "><a href="log_contexts.html">Log Contexts</a></li><li class="chapter-item expanded "><a href="replication.html">Replication</a></li><li class="chapter-item expanded "><a href="tcp_replication.html" class="active">TCP Replication</a></li></ol></li><li class="chapter-item expanded "><a href="development/internal_documentation/index.html">Internal Documentation</a></li><li><ol class="section"><li class="chapter-item expanded "><div>Single Sign-On</div></li><li><ol class="section"><li class="chapter-item expanded "><a href="development/saml.html">SAML</a></li><li class="chapter-item expanded "><a href="development/cas.html">CAS</a></li></ol></li><li class="chapter-item expanded "><a href="development/room-dag-concepts.html">Room DAG concepts</a></li><li class="chapter-item expanded "><div>State Resolution</div></li><li><ol class="section"><li class="chapter-item expanded "><a href="auth_chain_difference_algorithm.html">The Auth Chain Difference Algorithm</a></li></ol></li><li class="chapter-item expanded "><a href="media_repository.html">Media Repository</a></li><li class="chapter-item expanded "><a href="room_and_user_statistics.html">Room and User Statistics</a></li></ol></li><li class="chapter-item expanded "><div>Scripts</div></li><li class="chapter-item expanded affix "><li class="part-title">Other</li><li class="chapter-item expanded "><a href="deprecation_policy.html">Dependency Deprecation Policy</a></li><li class="chapter-item expanded "><a href="other/running_synapse_on_single_board_computers.html">Running Synapse on a Single-Board Computer</a></li></ol>
  75. </div>
  76. <div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
  77. </nav>
  78. <div id="page-wrapper" class="page-wrapper">
  79. <div class="page">
  80. <div id="menu-bar-hover-placeholder"></div>
  81. <div id="menu-bar" class="menu-bar sticky bordered">
  82. <div class="left-buttons">
  83. <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
  84. <i class="fa fa-bars"></i>
  85. </button>
  86. <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
  87. <i class="fa fa-paint-brush"></i>
  88. </button>
  89. <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
  90. <li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
  91. <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
  92. <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
  93. <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
  94. <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
  95. </ul>
  96. <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
  97. <i class="fa fa-search"></i>
  98. </button>
  99. <div class="version-picker">
  100. <div class="dropdown">
  101. <div class="select">
  102. <span></span>
  103. <i class="fa fa-chevron-down"></i>
  104. </div>
  105. <input type="hidden" name="version">
  106. <ul class="dropdown-menu">
  107. <!-- Versions will be added dynamically in version-picker.js -->
  108. </ul>
  109. </div>
  110. </div>
  111. </div>
  112. <h1 class="menu-title">Synapse</h1>
  113. <div class="right-buttons">
  114. <a href="print.html" title="Print this book" aria-label="Print this book">
  115. <i id="print-button" class="fa fa-print"></i>
  116. </a>
  117. <a href="https://github.com/matrix-org/synapse" title="Git repository" aria-label="Git repository">
  118. <i id="git-repository-button" class="fa fa-github"></i>
  119. </a>
  120. <a href="https://github.com/matrix-org/synapse/edit/develop/docs/tcp_replication.md" title="Suggest an edit" aria-label="Suggest an edit">
  121. <i id="git-edit-button" class="fa fa-edit"></i>
  122. </a>
  123. </div>
  124. </div>
  125. <div id="search-wrapper" class="hidden">
  126. <form id="searchbar-outer" class="searchbar-outer">
  127. <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
  128. </form>
  129. <div id="searchresults-outer" class="searchresults-outer hidden">
  130. <div id="searchresults-header" class="searchresults-header"></div>
  131. <ul id="searchresults">
  132. </ul>
  133. </div>
  134. </div>
  135. <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
  136. <script type="text/javascript">
  137. document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
  138. document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
  139. Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
  140. link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
  141. });
  142. </script>
  143. <div id="content" class="content">
  144. <main>
  145. <!-- Page table of contents -->
  146. <div class="sidetoc">
  147. <nav class="pagetoc"></nav>
  148. </div>
  149. <h1 id="tcp-replication"><a class="header" href="#tcp-replication">TCP Replication</a></h1>
  150. <h2 id="motivation"><a class="header" href="#motivation">Motivation</a></h2>
  151. <p>Previously the workers used an HTTP long poll mechanism to get updates
  152. from the master, which had the problem of causing a lot of duplicate
  153. work on the server. This TCP protocol replaces those APIs with the aim
  154. of increased efficiency.</p>
  155. <h2 id="overview"><a class="header" href="#overview">Overview</a></h2>
  156. <p>The protocol is based on fire and forget, line based commands. An
  157. example flow would be (where '&gt;' indicates master to worker and
  158. '&lt;' worker to master flows):</p>
  159. <pre><code>&gt; SERVER example.com
  160. &lt; REPLICATE
  161. &gt; POSITION events master 53 53
  162. &gt; RDATA events master 54 [&quot;$foo1:bar.com&quot;, ...]
  163. &gt; RDATA events master 55 [&quot;$foo4:bar.com&quot;, ...]
  164. </code></pre>
  165. <p>The example shows the server accepting a new connection and sending its identity
  166. with the <code>SERVER</code> command, followed by the client server to respond with the
  167. position of all streams. The server then periodically sends <code>RDATA</code> commands
  168. which have the format <code>RDATA &lt;stream_name&gt; &lt;instance_name&gt; &lt;token&gt; &lt;row&gt;</code>, where
  169. the format of <code>&lt;row&gt;</code> is defined by the individual streams. The
  170. <code>&lt;instance_name&gt;</code> is the name of the Synapse process that generated the data
  171. (usually &quot;master&quot;).</p>
  172. <p>Error reporting happens by either the client or server sending an ERROR
  173. command, and usually the connection will be closed.</p>
  174. <p>Since the protocol is a simple line based, its possible to manually
  175. connect to the server using a tool like netcat. A few things should be
  176. noted when manually using the protocol:</p>
  177. <ul>
  178. <li>The federation stream is only available if federation sending has
  179. been disabled on the main process.</li>
  180. <li>The server will only time connections out that have sent a <code>PING</code>
  181. command. If a ping is sent then the connection will be closed if no
  182. further commands are receieved within 15s. Both the client and
  183. server protocol implementations will send an initial PING on
  184. connection and ensure at least one command every 5s is sent (not
  185. necessarily <code>PING</code>).</li>
  186. <li><code>RDATA</code> commands <em>usually</em> include a numeric token, however if the
  187. stream has multiple rows to replicate per token the server will send
  188. multiple <code>RDATA</code> commands, with all but the last having a token of
  189. <code>batch</code>. See the documentation on <code>commands.RdataCommand</code> for
  190. further details.</li>
  191. </ul>
  192. <h2 id="architecture"><a class="header" href="#architecture">Architecture</a></h2>
  193. <p>The basic structure of the protocol is line based, where the initial
  194. word of each line specifies the command. The rest of the line is parsed
  195. based on the command. For example, the RDATA command is defined as:</p>
  196. <pre><code>RDATA &lt;stream_name&gt; &lt;instance_name&gt; &lt;token&gt; &lt;row_json&gt;
  197. </code></pre>
  198. <p>(Note that &lt;row_json&gt; may contains spaces, but cannot contain
  199. newlines.)</p>
  200. <p>Blank lines are ignored.</p>
  201. <h3 id="keep-alives"><a class="header" href="#keep-alives">Keep alives</a></h3>
  202. <p>Both sides are expected to send at least one command every 5s or so, and
  203. should send a <code>PING</code> command if necessary. If either side do not receive
  204. a command within e.g. 15s then the connection should be closed.</p>
  205. <p>Because the server may be connected to manually using e.g. netcat, the
  206. timeouts aren't enabled until an initial <code>PING</code> command is seen. Both
  207. the client and server implementations below send a <code>PING</code> command
  208. immediately on connection to ensure the timeouts are enabled.</p>
  209. <p>This ensures that both sides can quickly realize if the tcp connection
  210. has gone and handle the situation appropriately.</p>
  211. <h3 id="start-up"><a class="header" href="#start-up">Start up</a></h3>
  212. <p>When a new connection is made, the server:</p>
  213. <ul>
  214. <li>Sends a <code>SERVER</code> command, which includes the identity of the server,
  215. allowing the client to detect if its connected to the expected
  216. server</li>
  217. <li>Sends a <code>PING</code> command as above, to enable the client to time out
  218. connections promptly.</li>
  219. </ul>
  220. <p>The client:</p>
  221. <ul>
  222. <li>Sends a <code>NAME</code> command, allowing the server to associate a human
  223. friendly name with the connection. This is optional.</li>
  224. <li>Sends a <code>PING</code> as above</li>
  225. <li>Sends a <code>REPLICATE</code> to get the current position of all streams.</li>
  226. <li>On receipt of a <code>SERVER</code> command, checks that the server name
  227. matches the expected server name.</li>
  228. </ul>
  229. <h3 id="error-handling"><a class="header" href="#error-handling">Error handling</a></h3>
  230. <p>If either side detects an error it can send an <code>ERROR</code> command and close
  231. the connection.</p>
  232. <p>If the client side loses the connection to the server it should
  233. reconnect, following the steps above.</p>
  234. <h3 id="congestion"><a class="header" href="#congestion">Congestion</a></h3>
  235. <p>If the server sends messages faster than the client can consume them the
  236. server will first buffer a (fairly large) number of commands and then
  237. disconnect the client. This ensures that we don't queue up an unbounded
  238. number of commands in memory and gives us a potential oppurtunity to
  239. squawk loudly. When/if the client recovers it can reconnect to the
  240. server and ask for missed messages.</p>
  241. <h3 id="reliability"><a class="header" href="#reliability">Reliability</a></h3>
  242. <p>In general the replication stream should be considered an unreliable
  243. transport since e.g. commands are not resent if the connection
  244. disappears.</p>
  245. <p>The exception to that are the replication streams, i.e. RDATA commands,
  246. since these include tokens which can be used to restart the stream on
  247. connection errors.</p>
  248. <p>The client should keep track of the token in the last RDATA command
  249. received for each stream so that on reconneciton it can start streaming
  250. from the correct place. Note: not all RDATA have valid tokens due to
  251. batching. See <code>RdataCommand</code> for more details.</p>
  252. <h3 id="example"><a class="header" href="#example">Example</a></h3>
  253. <p>An example iteraction is shown below. Each line is prefixed with '&gt;'
  254. or '&lt;' to indicate which side is sending, these are <em>not</em> included on
  255. the wire:</p>
  256. <pre><code>* connection established *
  257. &gt; SERVER localhost:8823
  258. &gt; PING 1490197665618
  259. &lt; NAME synapse.app.appservice
  260. &lt; PING 1490197665618
  261. &lt; REPLICATE
  262. &gt; POSITION events master 1 1
  263. &gt; POSITION backfill master 1 1
  264. &gt; POSITION caches master 1 1
  265. &gt; RDATA caches master 2 [&quot;get_user_by_id&quot;,[&quot;@01register-user:localhost:8823&quot;],1490197670513]
  266. &gt; RDATA events master 14 [&quot;$149019767112vOHxz:localhost:8823&quot;,
  267. &quot;!AFDCvgApUmpdfVjIXm:localhost:8823&quot;,&quot;m.room.guest_access&quot;,&quot;&quot;,null]
  268. &lt; PING 1490197675618
  269. &gt; ERROR server stopping
  270. * connection closed by server *
  271. </code></pre>
  272. <p>The <code>POSITION</code> command sent by the server is used to set the clients
  273. position without needing to send data with the <code>RDATA</code> command.</p>
  274. <p>An example of a batched set of <code>RDATA</code> is:</p>
  275. <pre><code>&gt; RDATA caches master batch [&quot;get_user_by_id&quot;,[&quot;@test:localhost:8823&quot;],1490197670513]
  276. &gt; RDATA caches master batch [&quot;get_user_by_id&quot;,[&quot;@test2:localhost:8823&quot;],1490197670513]
  277. &gt; RDATA caches master batch [&quot;get_user_by_id&quot;,[&quot;@test3:localhost:8823&quot;],1490197670513]
  278. &gt; RDATA caches master 54 [&quot;get_user_by_id&quot;,[&quot;@test4:localhost:8823&quot;],1490197670513]
  279. </code></pre>
  280. <p>In this case the client shouldn't advance their caches token until it
  281. sees the the last <code>RDATA</code>.</p>
  282. <h3 id="list-of-commands"><a class="header" href="#list-of-commands">List of commands</a></h3>
  283. <p>The list of valid commands, with which side can send it: server (S) or
  284. client (C):</p>
  285. <h4 id="server-s"><a class="header" href="#server-s">SERVER (S)</a></h4>
  286. <p>Sent at the start to identify which server the client is talking to</p>
  287. <h4 id="rdata-s"><a class="header" href="#rdata-s">RDATA (S)</a></h4>
  288. <p>A single update in a stream</p>
  289. <h4 id="position-s"><a class="header" href="#position-s">POSITION (S)</a></h4>
  290. <p>On receipt of a POSITION command clients should check if they have missed any
  291. updates, and if so then fetch them out of band. Sent in response to a
  292. REPLICATE command (but can happen at any time).</p>
  293. <p>The POSITION command includes the source of the stream. Currently all streams
  294. are written by a single process (usually &quot;master&quot;). If fetching missing
  295. updates via HTTP API, rather than via the DB, then processes should make the
  296. request to the appropriate process.</p>
  297. <p>Two positions are included, the &quot;new&quot; position and the last position sent respectively.
  298. This allows servers to tell instances that the positions have advanced but no
  299. data has been written, without clients needlessly checking to see if they
  300. have missed any updates.</p>
  301. <h4 id="error-s-c"><a class="header" href="#error-s-c">ERROR (S, C)</a></h4>
  302. <p>There was an error</p>
  303. <h4 id="ping-s-c"><a class="header" href="#ping-s-c">PING (S, C)</a></h4>
  304. <p>Sent periodically to ensure the connection is still alive</p>
  305. <h4 id="name-c"><a class="header" href="#name-c">NAME (C)</a></h4>
  306. <p>Sent at the start by client to inform the server who they are</p>
  307. <h4 id="replicate-c"><a class="header" href="#replicate-c">REPLICATE (C)</a></h4>
  308. <p>Asks the server for the current position of all streams.</p>
  309. <h4 id="user_sync-c"><a class="header" href="#user_sync-c">USER_SYNC (C)</a></h4>
  310. <p>A user has started or stopped syncing on this process.</p>
  311. <h4 id="clear_user_sync-c"><a class="header" href="#clear_user_sync-c">CLEAR_USER_SYNC (C)</a></h4>
  312. <p>The server should clear all associated user sync data from the worker.</p>
  313. <p>This is used when a worker is shutting down.</p>
  314. <h4 id="federation_ack-c"><a class="header" href="#federation_ack-c">FEDERATION_ACK (C)</a></h4>
  315. <p>Acknowledge receipt of some federation data</p>
  316. <h3 id="remote_server_up-s-c"><a class="header" href="#remote_server_up-s-c">REMOTE_SERVER_UP (S, C)</a></h3>
  317. <p>Inform other processes that a remote server may have come back online.</p>
  318. <p>See <code>synapse/replication/tcp/commands.py</code> for a detailed description and
  319. the format of each command.</p>
  320. <h3 id="cache-invalidation-stream"><a class="header" href="#cache-invalidation-stream">Cache Invalidation Stream</a></h3>
  321. <p>The cache invalidation stream is used to inform workers when they need
  322. to invalidate any of their caches in the data store. This is done by
  323. streaming all cache invalidations done on master down to the workers,
  324. assuming that any caches on the workers also exist on the master.</p>
  325. <p>Each individual cache invalidation results in a row being sent down
  326. replication, which includes the cache name (the name of the function)
  327. and they key to invalidate. For example:</p>
  328. <pre><code>&gt; RDATA caches master 550953771 [&quot;get_user_by_id&quot;, [&quot;@bob:example.com&quot;], 1550574873251]
  329. </code></pre>
  330. <p>Alternatively, an entire cache can be invalidated by sending down a <code>null</code>
  331. instead of the key. For example:</p>
  332. <pre><code>&gt; RDATA caches master 550953772 [&quot;get_user_by_id&quot;, null, 1550574873252]
  333. </code></pre>
  334. <p>However, there are times when a number of caches need to be invalidated
  335. at the same time with the same key. To reduce traffic we batch those
  336. invalidations into a single poke by defining a special cache name that
  337. workers understand to mean to expand to invalidate the correct caches.</p>
  338. <p>Currently the special cache names are declared in
  339. <code>synapse/storage/_base.py</code> and are:</p>
  340. <ol>
  341. <li><code>cs_cache_fake</code> ─ invalidates caches that depend on the current
  342. state</li>
  343. </ol>
  344. </main>
  345. <nav class="nav-wrapper" aria-label="Page navigation">
  346. <!-- Mobile navigation buttons -->
  347. <a rel="prev" href="replication.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
  348. <i class="fa fa-angle-left"></i>
  349. </a>
  350. <a rel="next" href="development/internal_documentation/index.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
  351. <i class="fa fa-angle-right"></i>
  352. </a>
  353. <div style="clear: both"></div>
  354. </nav>
  355. </div>
  356. </div>
  357. <nav class="nav-wide-wrapper" aria-label="Page navigation">
  358. <a rel="prev" href="replication.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
  359. <i class="fa fa-angle-left"></i>
  360. </a>
  361. <a rel="next" href="development/internal_documentation/index.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
  362. <i class="fa fa-angle-right"></i>
  363. </a>
  364. </nav>
  365. </div>
  366. <script type="text/javascript">
  367. window.playground_copyable = true;
  368. </script>
  369. <script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
  370. <script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
  371. <script src="searcher.js" type="text/javascript" charset="utf-8"></script>
  372. <script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
  373. <script src="highlight.js" type="text/javascript" charset="utf-8"></script>
  374. <script src="book.js" type="text/javascript" charset="utf-8"></script>
  375. <!-- Custom JS scripts -->
  376. <script type="text/javascript" src="docs/website_files/table-of-contents.js"></script>
  377. <script type="text/javascript" src="docs/website_files/version-picker.js"></script>
  378. <script type="text/javascript" src="docs/website_files/version.js"></script>
  379. </body>
  380. </html>