Mention the 2.6.8 release.
[rsync-web.git] / how-rsync-works.html
1 <html>
2 <head>
3 <title>How Rsync Works</title>
4 </head>
5 <body>
6 <h1 align="center">How Rsync Works<br>A Practical Overview</h1>
7 <h2 align="center">Foreword</h2>
8 <p>
9 The original
10 <a href="http://rsync.samba.org/tech_report/">Rsync technical report</a>
11 and
12 Andrew Tridgell's
13 <a href="http://samba.org/%7Etridge/phd_thesis.pdf">Phd thesis (pdf)</a>
14 Are both excellent documents for understanding the
15 theoretical mathematics and some of the mechanics of the rsync algorithm.
16 Unfortunately they are more about the theory than the
17 implementation of the rsync utility (hereafter referred to as
18 Rsync).
19 <p>
20 In this document I hope to describe...
21 <ul>
22         <li>A non-mathematical overview of the rsync algorithm.
23         <li>How that algorithm is implemented in the rsync utility.
24         <li>The protocol, in general terms, used by the rsync utility.
25         <li>The identifiable roles the rsync processes play.
26 </ul>
27 <p>
28 This document be able to serve as a guide for programmers
29 needing something of an entr&eacute; into the source code but the
30 primary purpose is to give the reader a foundation from
31 which he may understand
32 <ul>
33         <li>Why rsync behaves as it does.
34         <li>The limitations of rsync.
35         <li>Why a requested feature is unsuited to the code-base.
36 </ul>
37 <p>
38 This document describes in general terms the construction
39 and behaviour of Rsync.  In some cases details and exceptions
40 that would contribute to specific accuracy have
41 been sacrificed for the sake meeting the broader goals.
42 <h2 align="center">Processes and Roles</h2>
43 <p>
44 When we talk about Rsync we use specific terms to refer to
45 various processes and their roles in the task performed by
46 the utility.  For effective communication it is important that we
47 all be speaking the same language; likewise it is important
48 that we mean the same things when we use certain terms in a
49 given context.  On the rsync mailing list there is often
50 some confusion with regards to role and processes.  For
51 these reasons I will define a few terms
52 used in the role and process contexts that will be used henceforth.
53
54 <table cellspacing="20"><tr valign="top">
55 <td>client
56 </td><td>role
57 </td><td>
58         The client initiates the synchronisation.
59 </td></tr><tr valign="top">
60 <td>server
61 </td><td>role
62 </td><td>
63         The remote rsync process or system to which the
64         client connects either within a local transfer, via
65         a remote shell or via a network socket.
66         <p>
67         This is a general term and should not be confused with the daemon.
68 </td></tr><tr valign="top">
69 <td>
70 </td><td>
71 </td><td bgcolor="#dddddd">
72         Once the connection between the client and server is established
73         the distinction between them is superseded by the
74         sender and receiver roles.
75 </td></tr><tr valign="top">
76 <td>daemon
77 </td><td>Role and process
78 </td><td>
79         An Rsync process that awaits connections from
80         clients.  On a certain platform this would be called a
81         service.
82 </td></tr><tr valign="top">
83 <td>remote&nbsp;shell
84 </td><td>role and set of processes
85 </td><td>
86         One or more processes that provide connectivity
87         between an Rsync client and an Rsync server on a
88         remote system.
89 </td></tr><tr valign="top">
90 <td>sender
91 </td><td>role and process
92 </td><td>
93         The Rsync process that has access to the source
94         files being synchronised.
95 </td></tr><tr valign="top">
96 <td>receiver
97 </td><td>role and process
98 </td><td>
99         As a role the receiver is the destination system.
100         As a process the receiver is the process that
101         receives update data and writes it to disk.
102 </td></tr><tr valign="top">
103 <td>generator
104 </td><td>process
105 </td><td>
106         The generator process identifies changed files and
107         manages the file level logic.
108 </td></tr></table>
109 <p>
110 <h2 align="center">Process Startup</h2>
111 <p>
112 When an Rsync client is started it will first establish a
113 connection with a server process.  This connection may be
114 through pipes or over a network socket.
115 <p>
116 When Rsync communicates with a remote non-daemon server via
117 a remote shell the startup method is to fork the remote
118 shell which will start an Rsync server on the remote system.
119 Both the Rsync client and server are communicating via pipes
120 through the remote shell.  As far as the rsync processes are
121 concerned there is no network.
122 In this mode the rsync options for the server process are
123 passed on the command-line that is used to start the remote
124 shell.
125 <p>
126 When Rsync is communicating with a daemon it is
127 communicating directly with a network socket.  This is the
128 only sort of Rsync communication that could be called
129 network aware.
130 In this mode the rsync options must be sent over the socket, as
131 described below.
132 <p>
133 At the very start of the communication between the client
134 and the server, they each send the maximum protocol version
135 they support to the other side.
136 Each side then uses the minimum value as the the protocol
137 level for the transfer.
138 If this is a daemon-mode connection, rsync options are sent
139 from the client to the server.  Then, the exclude list is
140 transmitted.  From this point onward the
141 client-server relationship is relevant only with regards
142 to error and log message delivery.
143 <p>
144 Local Rsync jobs (when the source and destination are both on locally
145 mounted filesystems) are done exactly like a push.  The
146 client, which becomes the sender, forks a server process to
147 fulfill the receiver role.  The client/sender and
148 server/receiver communicate with each other over pipes.
149 <h2 align="center">The File List</h2>
150 The file list includes not only the pathnames but also
151 ownership, mode, permissions, size and modtime.
152 If the --checksum option has been specified it also includes
153 the file checksums.
154 <p>
155 The first thing that happens once the startup has completed
156 is that the sender will create the file list.
157 While it is being built, each entry is transmitted to the
158 receiving side in a network-optimised way.
159 <p>
160 When this is done, each side sorts the file list lexicographically by path
161 relative to the base directory of the transfer.
162 (The exact sorting algorithm varies depending on what protocol
163 version is in effect for the transfer.)
164 Once that has happened all references to files
165 will be done by their index in the file list.
166 <p>
167 If necessary the sender follows the file list with id&rarr;name
168 tables for users and groups which the receiver will use to
169 do a id&rarr;name&rarr;id translation for every file in the file
170 list.
171 <p>
172 After the file list has been received by the receiver, it
173 will fork to become the generator and receiver pair
174 completing the pipeline.
175 <h2 align="center">The Pipeline</h2>
176 Rsync is heavily pipelined.  This means that it is a set of
177 processes that communicate in a (largely) unidirectional
178 way.  Once the file list has been shared the pipeline
179 behaves like this:
180 <blockquote>
181         generator &rarr; sender &rarr; receiver
182 </blockquote>
183 <p>
184 The output of the generator is input for the sender and the
185 output of the sender is input for the receiver.
186 Each process runs independently and is delayed only when the
187 pipelines stall or when waiting for disk I/O or CPU resources.
188 <h2 align="center">The Generator</h2>
189 <p>
190 The generator process compares the file list with its local
191 directory tree.  Prior to beginning its primary function, if
192 --delete has been specified, it will first identify local
193 files not on the sender and delete them on the receiver.
194 <p>
195 The generator will then start walking the file list.  Each
196 file will be checked to see if it can be skipped.  In the
197 most common mode of operation files are not skipped if the
198 modification time or size differ.  If --checksum was
199 specified a file-level checksum will be created and
200 compared.  Directories, device nodes and symlinks are not
201 skipped.  Missing directories will be created.  
202 <p>
203 If a file is not to be skipped, block checksums are created
204 for it and sent with the file index number to the sender.
205 An empty block checksum set is sent for new files and if
206 --whole-file was specified.
207 <p>
208 The block size and, in later versions, the size of the
209 block checksum are calculated on a per file basis according
210 to the size of that file.
211 <h2 align="center">The Sender</h2>
212 The sender process reads the file index numbers and associated
213 block checksum sets one at a time from the generator.
214 <p>
215 For each file id the generator sends it will store the
216 block checksums and build a hash index of them for rapid lookup.
217 <p>
218 Then the local file is read and a checksum is
219 generated for the block beginning with the first byte of the
220 local file.  This block checksum is looked for in the
221 set that was sent by the generator, and if no match is found,
222 the non-matching byte will be appended to the non-matching data
223 and the block starting at the next byte will be compared.
224 This is what
225 is referred to as the &ldquo;rolling checksum&rdquo;
226 <p>
227 If a block checksum match is found it is considered a
228 matching block and any accumulated non-matching data will be
229 sent to the receiver followed by the offset and length in
230 the receiver's file of the matching block and the block
231 checksum generator will be advanced to the next byte after
232 the matching block.
233 <p>
234 Matching blocks can be identified in this way even if
235 the blocks are reordered or at different offsets.  
236 This process is the very heart of the rsync algorithm.
237 <p>
238 In this way the generator will send to the receiver a
239 sequence of non-matched data interspersed with matching block
240 information.  At the end of each file's processing a whole-file
241 checksum is sent and the sender proceeds with the next
242 file.
243 <p>
244 Generating the rolling checksums and searching for matches
245 in the checksum set sent by the generator require a good
246 deal of CPU power.  Of all the rsync processes it is the
247 sender that is the most CPU intensive.
248 <h2 align="center">The Receiver</h2>
249 <p>
250 The receiver will read from the sender data for each file
251 identified by the file index number.  It will open the local
252 file (called the basis) and will create a temporary file.
253 <p>
254 The receiver will expect to read non-matched data and/or to match
255 records all in sequence for the final file contents.  When
256 non-matched data is read it will be written to the
257 temp-file.  When a block match record is received the
258 receiver will seek to the block offset in the basis file
259 and copy the block to the temp-file.  In this way the
260 temp-file is built from beginning to end.
261 <p>
262 The file's checksum is generated as the temp-file is built.
263 At the end of the file, this checksum is compared with the
264 file checksum from the sender.  If the file checksums do not
265 match the temp-file is deleted.  If the file fails once it
266 will be reprocessed in a second phase, and if it fails twice
267 an error is reported.
268 <p>
269 After the temp-file has been completed, its ownership and
270 permissions and modification time are set.  It is then
271 renamed to replace the basis file.
272 <p>
273 Copying data from the basis file to the temp-file make the
274 receiver the most disk intensive of all the rsync processes.
275 Small files may still be in disk cache mitigating this but
276 for large files the cache may thrash as the generator has
277 moved on to other files and there is further latency caused
278 by the sender.  As
279 data is read possibly at random from one file and written to
280 another, if the working set is larger than the disk cache,
281 then what is called a seek storm can occur, further
282 hurting performance.
283 <h2 align="center">The Daemon</h2>
284 The daemon process, like many daemons, forks for every
285 connection.  On startup, it parses the rsyncd.conf file
286 to determine what modules exist and to set the global options.
287 <p>
288 When a connection is received for a defined module the
289 daemon forks a new child process to handle the connection.
290 That child process then reads the rsyncd.conf file to set
291 the options for the requested module, which may chroot to the
292 module path and may drop setuid and setgid for the
293 process.  After that it will behave just like any other
294 rsync server process adopting either a sender or receiver
295 role.
296 <h2 align="center">The Rsync Protocol</h2>
297 <p>
298 A well-designed communications protocol has a number of
299 characteristics.
300 <ul>
301         <li>Everything is sent in well defined packets with
302                 a header and an optional body or data payload.
303         <li>In each packet's header a type and or command
304                 specified.
305         <li>Each packet has a definite length.
306 </ul>
307 <p>
308 In addition to these characteristics, protocols have varying degrees of
309 statefulness, inter-packet independence, human readability,
310 and the ability to reestablish a disconnected session.
311 <p>
312 Rsync's protocol has none of these good characteristics.  The data is
313 transferred as an unbroken stream of bytes.  With the
314 exception of the unmatched file-data, there are no length
315 specifiers nor counts.  Instead the meaning of each byte is
316 dependent on its context as defined by the protocol level.
317 <p>
318 As an example, when the sender is sending  the file list it
319 simply sends each file list entry and terminates the list
320 with a null byte.  Within the file list entries, a bitfield
321 indicates which fields of the structure to expect and those
322 that are variable length strings are simply null terminated.
323 The generator sending file numbers and block checksum sets
324 works the same way.
325 <p>
326 This method of communication works quite well on reliable
327 connections and it certainly has less data overhead than the
328 formal protocols.  It unfortunately makes the protocol
329 extremely difficult to document, debug or extend.
330 Each version of the protocol will have subtle differences on
331 the wire that can only be anticipated by knowing the exact
332 protocol version.
333 <h2 align="center">notes</h2>
334 This document is a work in progress.  The author expects
335 that it has some glaring oversights and some portions that may be
336 more confusing than enlightening for some readers.  It is
337 hoped that this could evolve into a useful reference.
338 <p>
339 Specific suggestions for improvement are welcome, as would be a
340 complete rewrite.
341 </body>
342 </html>