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

Folders and files

NameName
Last commit message
Last commit date
Jan 8, 2021
Jun 2, 2022
Mar 18, 2021
Sep 15, 2021
Sep 27, 2023
Dec 3, 2020
Nov 17, 2020
Jul 24, 2023
Jul 25, 2023
Jul 21, 2023
Jan 8, 2021
Feb 8, 2021
Jan 8, 2021
Oct 11, 2022
Sep 15, 2021
Nov 13, 2020
Nov 13, 2020
Nov 17, 2020

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