Replication applier

Replication configuration

The replication is turned off by default. In order to create a master-slave setup,the so-called replication applier needs to be enabled on the slave databases.

Replication is configured on a per-database level or (starting from v3.3.0) at server level.

The replication applier on the slave can be used to perform a one-time synchronizationwith the master (and then stop), or to perform an ongoing replication of changes. Toresume replication on slave restart, the autoStart attribute of the replication applier must be set to true.

setupReplication Command

To copy the initial data from the master to the slave and start thecontinuous replication, there is an all-in-one command setupReplication.

From Arangosh:

  1. require("@arangodb/replication").setupReplication(configuration);

The following example demonstrates how to use the command for setting up replicationfor the _system database. Note that it should be run on the slave and not the master:

  1. db._useDatabase("_system");
  2. require("@arangodb/replication").setupReplication({
  3. endpoint: "tcp://master.domain.org:8529",
  4. username: "myuser",
  5. password: "mypasswd",
  6. verbose: false,
  7. includeSystem: false,
  8. incremental: true,
  9. autoResync: true
  10. });

The command will return when the initial synchronization is finished and the continuous replicationis started, or in case the initial synchronization has failed.

If the initial synchronization is successful, the command will store the given configuration onthe slave. It also configures the continuous replication to start automatically if the slave is restarted, i.e. autoStart is set to true.

If the command is run while the slave’s replication applier is already running, it will firststop the running applier, drop its configuration and do a resynchronization of data with themaster. It will then use the provided configuration, overwriting any previously existing replicationconfiguration on the slave.

Starting and Stopping the replication applier

To manually start and stop the replication applier in the current database, the start and stop commands can be used like this:

  1. require("@arangodb/replication").applier.start(<tick>);
  2. require("@arangodb/replication").applier.stop();

Note: Starting a replication applier without setting up an initial configuration will fail. The replication applier will look for its configuration in a file named REPLICATION-APPLIER-CONFIG in the current database’s directory. If the file is not present, ArangoDB will use some default configuration, but it cannot guess the endpoint (the address of the master database) the applier should connect to. Thus starting the applier without configuration will fail.

Note that at the first time you start the applier, you should pass the value returned in thelastLogTick attribute of the initial sync operation.

Note: Starting a database’s replication applier via the start command will not necessarily start the applier on the next and following ArangoDB server restarts. Additionally, stopping adatabase’s replication applier manually will not necessarily prevent the applier from being started again on the next server start. All of this is configurable separately (hang on reading).

Note: when stopping and restarting the replication applier of database, it will resume where it last stopped. This is sensible because replication log events should be applied incrementally. If the replication applier of a database has never been started before, it needs some tick value from the master’s log from which to start fetching events.

There is one caveat to consider when stopping a replication on the slave: if there are stillongoing replicated transactions that are neither committed or aborted, stopping the replicationapplier will cause these operations to be lost for the slave. If these transactions commit on themaster later and the replication is resumed, the slave will not be able to commit these transactions,too. Thus stopping the replication applier on the slave manually should only be done if thereis certainty that there are no ongoing transactions on the master.

Replication applier Configuration

To configure the replication applier of a specific database, use the properties command. Using it without any arguments will return the applier’s current configuration:

  1. require("@arangodb/replication").applier.properties();

The result might look like this:

  1. {
  2. "requestTimeout" : 600,
  3. "connectTimeout" : 10,
  4. "ignoreErrors" : 0,
  5. "maxConnectRetries" : 10,
  6. "chunkSize" : 0,
  7. "autoStart" : false,
  8. "adaptivePolling" : true,
  9. "includeSystem" : true,
  10. "requireFromPresent" : false,
  11. "autoResync" : false,
  12. "autoResyncRetries" : 2,
  13. "verbose" : false
  14. }

Note: There is no endpoint attribute configured yet. The endpoint attribute is requiredfor the replication applier to be startable. You may also want to configure a username and passwordfor the connection via the username and password attributes.

  1. require("@arangodb/replication").applier.properties({
  2. endpoint: "tcp://master.domain.org:8529",
  3. username: "root",
  4. password: "secret",
  5. verbose: false
  6. });

This will re-configure the replication applier for the current database. The configuration will be used from the next start of the replication applier. The replication applier cannot be re-configured while it is running. It must be stopped first to be re-configured.

To make the replication applier of the current database start automatically when the ArangoDB server starts, use the autoStart attribute.

Setting the adaptivePolling attribute to true will make the replication applier poll the master database for changes with a variable frequency. The replication applier will then lower the frequency when the master is idle, and increase it when the master can provide new events).Otherwise the replication applier will poll the master database for changes with a constant frequency.

The idleMinWaitTime attribute controls the minimum wait time (in seconds) that the replication applier will intentionally idle before fetching more log data from the master in case the master has already sent all its log data. This wait time can be used to control the frequency with which the replication applier sends HTTP log fetch requests to the master in case there is no write activity on the master.

The idleMaxWaitTime attribute controls the maximum wait time (in seconds) that the replication applier will intentionally idle before fetching more log data from the master in case the master has already sent all its log data and there have been previous log fetch attempts that resulted in no more log data. This wait time can be used to control the maximum frequency with which the replication applier sends HTTP log fetch requests to the master in case there is no write activity on the master for longer periods. Note that this configuration value will only be used if the option adaptivePolling is set to true.

To set a timeout for connection and following request attempts, use the connectTimeout and requestTimeout values. The maxConnectRetries attribute configures after how many failed connection attempts in a row the replication applier will give up and turn itself off. You may want to set this to a high value so that temporary network outages do not lead to the replication applier stopping itself.The connectRetryWaitTime attribute configures how long the replication applier will waitbefore retrying the connection to the master in case of connection problems.

The chunkSize attribute can be used to control the approximate maximum size of a master’sresponse (in bytes). Setting it to a low value may make the master respond faster (less data isassembled before the master sends the response), but may require more request-response roundtrips.Set it to 0 to use ArangoDB’s built-in default value.

The includeSystem attribute controls whether changes to system collections (such as graphs_ orusers) should be applied. If set to _true, changes in these collections will be replicated, otherwise, they will not be replicated. It is often not necessary to replicate data from systemcollections, especially because it may lead to confusion on the slave because the slave needs to have its own system collections in order to start and keep operational.

There is a separate option includeFoxxQueues for controlling whether Foxx queue jobs from the systemcollections _jobs and _queues collections should be replicated. Documents from these collections are not replicated by default in order to avoid execution of Foxx queue jobs on the slave.

The requireFromPresent attribute controls whether the applier will start synchronizing in caseit detects that the master cannot provide data for the initial tick value provided by the slave. This may be the case if the master does not have a big enough backlog of historic WAL logfiles,and when the replication is re-started after a longer pause. When requireFromPresent is set to true, then the replication applier will check at start whether the start tick from which it starts or resumes replication is still present on the master. If not, then there would be data loss. If requireFromPresent is true, the replication applier will abort with an appropriate error message. If set to false, then the replication applier will still start, and ignore the data loss.

The autoResync option can be used in conjunction with the requireFromPresent option as follows:when both requireFromPresent and autoResync are set to true and the master cannot provide the log data the slave requests, the replication applier will stop as usual. But due to the factthat autoResync is set to true, the slave will automatically trigger a full resync of all data with the master. After that, the replication applier will go into continuous replication mode again.Additionally, setting autoResync to true will trigger a full re-synchronization of data whenthe continuous replication is started and detects that there is no start tick value.

Automatic re-synchronization may transfer a lot of data from the master to the slave and can be expensive. It is therefore turned off by default. When turned off, the slave will never perform an automatic re-synchronization with the master.

The autoResyncRetries option can be used to control the number of resynchronization retries that will be performed in a row when automatic resynchronization is enabled and kicks in. Setting this to 0 will effectively disable autoResync. Setting it to some other value will limit the number of retries that are performed. This helps preventing endless retries in case resynchronizations always fail.

The verbose attribute controls the verbosity of the replication logger. Setting it to true willmake the replication applier write a line to the log for every operation it performs. This shouldonly be used for diagnosing replication problems.

The following example will set most of the discussed properties for the current database’s applier:

  1. require("@arangodb/replication").applier.properties({
  2. endpoint: "tcp://master.domain.org:8529",
  3. username: "root",
  4. password: "secret",
  5. adaptivePolling: true,
  6. connectTimeout: 15,
  7. maxConnectRetries: 100,
  8. chunkSize: 262144,
  9. autoStart: true,
  10. includeSystem: true,
  11. autoResync: true,
  12. autoResyncRetries: 2,
  13. });

After the applier is now fully configured, it could theoretically be started. However, wemay first need an initial synchronization of all collections and their data from the master beforewe start the replication applier.

The only safe method for doing a full synchronization (or re-synchronization) is thus to

  • stop the replication applier on the slave (if currently running)
  • perform an initial full sync with the master database
  • note the master database’s lastLogTick value and
  • start the continuous replication applier on the slave using this tick value.

The initial synchronization for the current database is executed with the sync command:

  1. require("@arangodb/replication").sync({
  2. endpoint: "tcp://master.domain.org:8529",
  3. username: "root",
  4. password: "secret",
  5. includeSystem: true
  6. });

The includeSystem option controls whether data from system collections (such as graphs_ andusers_) shall be synchronized.

The initial synchronization can optionally be configured to include or exclude specific collections using the restrictType and restrictCollection parameters.

The following command only synchronizes collection foo and bar:

  1. require("@arangodb/replication").sync({
  2. endpoint: "tcp://master.domain.org:8529",
  3. username: "root",
  4. password: "secret",
  5. restrictType: "include",
  6. restrictCollections: [ "foo", "bar" ]
  7. });

Using a restrictType of exclude, all collections but the specified will be synchronized.

Warning: sync will do a full synchronization of the collections in the current database withcollections present in the master database.Any local instances of the collections and all their data are removed! Only execute thiscommand if you are sure you want to remove the local data!

As sync does a full synchronization, it might take a while to execute.When sync completes successfully, it returns an array of collections it has synchronized in itscollections attribute. It will also return the master database’s last log tick value at the time the sync was started on the master. The tick value is contained in the lastLogTick_attribute of the _sync command:

  1. {
  2. "lastLogTick" : "231848833079705",
  3. "collections" : [ ... ]
  4. }

Now you can start the continuous synchronization for the current database on the slavewith the command

  1. require("@arangodb/replication").applier.start("231848833079705");

Note: The tick values should be treated as strings. Using numeric data types for tickvalues is unsafe because they might exceed the 32 bit value and the IEEE754 double accuracyranges.