Handling replica sets in Java MongoDB

MongoDB replica sets are a cluster of MongoDB instances that store the same data, providing redundancy and high availability. In this blog post, we will explore how to handle replica sets in Java using the MongoDB Java Driver.

Table of Contents

Connecting to a replica set

To connect to a MongoDB replica set using the Java driver, you can use the MongoClient class and provide a list of ServerAddress objects representing the replica set members.

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.ServerAddress;

import java.util.Arrays;

public class ReplicaSetExample {
    public static void main(String[] args) {
        // Replica set members
        ServerAddress server1 = new ServerAddress("host1:27017");
        ServerAddress server2 = new ServerAddress("host2:27017");
        ServerAddress server3 = new ServerAddress("host3:27017");
        
        // Create the replica set connection URI
        String uri = "mongodb://" + server1 + "," + server2 + "," + server3;
        
        // Create the MongoDB client
        MongoClientURI connectionString = new MongoClientURI(uri);
        MongoClient client = new MongoClient(connectionString);
        
        // Use the client to interact with the replica set
        // ...
        
        // Close the connection
        client.close();
    }
}

In the above example, we create ServerAddress objects for each replica set member, then concatenate them in the connection URI. We then create a MongoClientURI object and pass it to the MongoClient constructor to establish the connection.

Read preferences

Read preferences define how MongoDB routes read operations to the replica set members. The Java driver provides different options for read preferences, such as primary, primary preferred, secondary, secondary preferred, and nearest.

To set the read preference for your MongoDB Java driver, you can use the MongoClientOptions class as shown below:

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;

import java.util.Arrays;

public class ReadPreferenceExample {
    public static void main(String[] args) {
        ServerAddress server1 = new ServerAddress("host1:27017");
        ServerAddress server2 = new ServerAddress("host2:27017");
        ServerAddress server3 = new ServerAddress("host3:27017");

        // Replica set members
        MongoClientOptions options = MongoClientOptions.builder()
                .readPreference(ReadPreference.secondaryPreferred())
                .build();

        // Create the MongoClient with read preference
        MongoClient client = new MongoClient(Arrays.asList(server1, server2, server3), options);
        
        // Use the client to interact with the replica set
        // ...
        
        // Close the connection
        client.close();
    }
}

In the above example, we use the ReadPreference.secondaryPreferred() method to set the read preference to secondaries with the option to read from the primary if no secondaries are available.

Write concerns

Write concerns define the guarantee that MongoDB provides when reporting the success of a write operation. The Java driver supports different write concerns, such as acknowledging writes on the primary or waiting for a certain number of replicas to acknowledge the write.

To set the write concern for your MongoDB Java driver, you can use the MongoClientOptions class similar to the read preferences example:

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;

import java.util.Arrays;

public class WriteConcernExample {
    public static void main(String[] args) {
        ServerAddress server1 = new ServerAddress("host1:27017");
        ServerAddress server2 = new ServerAddress("host2:27017");
        ServerAddress server3 = new ServerAddress("host3:27017");

        // Replica set members
        MongoClientOptions options = MongoClientOptions.builder()
                .writeConcern(WriteConcern.MAJORITY)
                .build();

        // Create the MongoClient with write concern
        MongoClient client = new MongoClient(Arrays.asList(server1, server2, server3), options);
        
        // Use the client to interact with the replica set
        // ...
        
        // Close the connection
        client.close();
    }
}

In the above example, we use the WriteConcern.MAJORITY option to guarantee that write operations are acknowledged by the majority of the replica set members.

Reference

#hashtags #java