Database Replication and Clustering

ObjectDB supports master-slave replication, also known as clustering. With replication,
the same database is managed on multiple computers, or nodes, which can be in different geographic locations. This configuration helps achieve high availability, fault tolerance, and prompt disaster recovery.

In a master-slave replication model, the master node manages the primary database, which supports both read and write operations. The other nodes in the cluster, known as slave nodes, manage identical, read-only copies of the database. Updates to the master database are automatically propagated to the slave databases, keeping them synchronized.

Setting a master satabase

A master ObjectDB database is a standard database that is managed on a standard ObjectDB database server. Any ObjectDB database on a server can function as a master database in a cluster, but databases in embedded mode cannot. You must enable recording, but no other preparations or settings are required.

Setting slave databases

Setting up slave databases is straightforward. You only need to run an ObjectDB database server with the appropriate <replication> elements in its configuration file:

<server>
    <connection port="6001" max="100" />
    <data path="$objectdb/db-files" />
    <replication url="objectdb://localhost:6000/my.odb;user=a;password=a" />
</server>

The url attribute of the <replication> element specifies the master database. As shown in the example, you must specify a full URL, including the user and password attributes. The slave server uses these credentials to connect to the master server and listen for updates. The updates are then automatically applied to the slave database, keeping it synchronized with the master.

A single ObjectDB server can manage different types of databases, including master databases, slave databases (by using one or more <replication> elements), and databases that are not part of any cluster.

The replicated databases on the slave server are automatically generated in a special root directory, $replication, which is located under the server's data root directory. To start a new replication of an existing master database, you must first copy the master database to the slave server.

Connecting to the database cluster

The configuration example shows a master database managed by a server on localhost:6000 and a slave database managed by a server on localhost:6001.

In this case, you can access the master database as follows:

EntityManagerFactory emf = Persistence.createEntityManagerFactory(
    "objectdb://localhost:6000/my.odb;user=a;password=a");

Accessing the slave database requires a slightly different URL:

EntityManagerFactory emf = Persistence.createEntityManagerFactory(
    "objectdb://localhost:6001//localhost:6000/my.odb;user=b;password=b");

This URL specifies the location of the slave server (port 6001) and the master server (port 6000). Note that the user and password attributes must be for a user on the slave server, not the master server.

Finally, you can also use a composite URL:

EntityManagerFactory emf = Persistence.createEntityManagerFactory(
    "objectdb://localhost:6000/my.odb;user=a;password=a|" +
    "objectdb://localhost:6001//localhost:6000/my.odb;user=b;password=b"
);

A composite URL contains two or more database URLs separated by a pipe character (|). Typically, the application uses only the first URL, which is the master database in this example. However, if the first URL becomes unavailable, the connection automatically and temporarily switches to the next available URL in the list (the slave database in this case) until the first URL is available again.