Using database replicas unlocks horizontal scalability. Some replica designs force replicas to be read-only.
Read-only queries can be sent to the replica, while others have to go to the main DB.
The db-replica
API automatically routes the queries to the correct node.
It integrates at the java.sql.Connection
level, so you don't have to hunt down hundreds of queries manually.
- Automatic switching between main and replica databases.
- Configurable consistency model.
- Configurable main/replica split instrumentation.
The library is not available via Maven Central Repository yet. It can be accessed via Atlassian Maven proxy.
import com.atlassian.db.replica.api.*;
import java.sql.*;
import java.time.*;
class Example {
private final ReplicaConsistency consistency = new PessimisticPropagationConsistency.Builder().build();
ResultSet queryReplicaOrMain(String sql) {
try (ConnectionProvider connectionProvider = new PostgresConnectionProvider()) {
Connection connection = DualConnection.builder(connectionProvider, consistency).build();
return connection.prepareStatement(sql).executeQuery();
}
}
}
Maven:
<dependency>
<groupId>com.atlassian.db.replica</groupId>
<artifactId>db-replica</artifactId>
<version>2.9.0</version>
</dependency>
See Javadoc of classes in the api
and spi
packages.
See DualConnection states UML.
See how to release the library.
Run all checks: ./gradlew build
Run just the unit tests: ./gradlew test
Run mutation tests: ./gradlew pitest
Please see CONTRIBUTING.md for details.
Apache 2.0 licensed, see LICENSE file.