Skip to content

Automatically chooses between database connections to read-write main or read-only replica

License

Notifications You must be signed in to change notification settings

atlassian-labs/db-replica

Repository files navigation

db-replica

CI license PRs Welcome

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.

High level overview

Features

Usage

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();
        }
    }
}

Installation

Maven:

<dependency>
    <groupId>com.atlassian.db.replica</groupId>
    <artifactId>db-replica</artifactId>
    <version>2.9.0</version>
</dependency>

Documentation

See Javadoc of classes in the api and spi packages. See DualConnection states UML. See how to release the library.

Tests

Run all checks: ./gradlew build Run just the unit tests: ./gradlew test Run mutation tests: ./gradlew pitest

Contributions

Please see CONTRIBUTING.md for details.

License

Apache 2.0 licensed, see LICENSE file.

With ❤️ from Atlassian

About

Automatically chooses between database connections to read-write main or read-only replica

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Languages