my-spring-boot-experiments
  • README
  • Spring Batch implementation
  • API Example for Archunit
  • spring-boot-chaos-monkey
  • Grafana LGTM Implementation
  • MongoDb for insertion and search using elastic search
  • OpenSearch Implementation
  • spring-boot-rabbitmq-thymeleaf
  • boot-rest-docs-sample
  • boot-strategy-plugin
  • spring-boot-togglz-sample
  • spring-boot-ultimate-redis
  • GraphQL
    • spring-boot-graphql-querydsl
    • GraphQl implementation using webflux
    • GraphQl implementation using webmvc
  • httpClients
    • http-proxy
    • rest-template
    • rest-client
    • web-client-mvc
    • web-client-webflux
  • jmh-benchmark
  • jpa
    • Custom Sequence
    • Hibernate Envers Implementation
    • multiple datasources using Spring Boot
    • spring-boot-hibernate2ndlevelcache-sample
    • JNDI in embedded Tomcat
    • JPA Jooq Marriage
    • JPA locks implementation
    • read-replica-with-spring-boot
    • KeySet Pagination Using Blaze
    • KeySet Pagination Using Data-JPA
    • MultiTenancy using Hibernate in Spring Data JPA
      • multitenancy-db
      • multidatasource-multitenancy
      • Partitioned (Discriminator) Data – the data for each tenant is partitioned by a discriminator value
      • schema
  • open-api-spring-boot
  • r2dbc
    • r2dbc-jooq
    • PostgreSQL JSON and enum column support
    • PostgreSQL Notify and Listen support using reactive spring boot
    • r2dbc-boot
    • reactive-cache
  • scheduler
    • Scheduling using JobRunr
    • Scheduling using Quartz
    • Scheduling using Shedlock distribution
  • Code Of Conduct
Powered by GitBook
On this page
  • Key Concepts
  • Liquibase
  • Run tests
  • Run locally
  • Useful Links
  • Reference

Was this helpful?

Edit on GitHub
  1. jpa

read-replica-with-spring-boot

PreviousJPA locks implementationNextKeySet Pagination Using Blaze

Last updated 4 months ago

Was this helpful?

This project is an example to show how we can separate read and write operations to primary and replica databases using spring boot and postgresql database.

A read replica in Postgres is a database instance that receives data from a primary database instance and serves it to clients. Read replicas are useful for scaling database workloads, as they can offload read operations from the primary instance, allowing it to focus on more resource-intensive tasks such as writing data. This can improve the performance of the overall database system. Read replicas can also be useful for providing high availability, as they can take over read operations if the primary instance becomes unavailable for any reason.

  • All reads will go to reader instance and writes will go to writer instance

  • Switching between master and replica can be observed in docker compose logs or application logs when datasourceproxy is enabled.


Key Concepts

  1. Primary-Replica Setup: Writes go to the primary database, while reads occur on the replica.

  2. Performance Optimization: Minimizes load on the primary, improving overall throughput.

  3. LazyConnectionDataSourceProxy: Ensures a single DataSource bean but dynamically determines read vs. write at runtime.


Liquibase

Use below script for generating the difference in the database

./diff.sh

Run tests

./mvnw clean verify

Run locally

docker-compose -f docker/docker-compose.yml up -d
./mvnw spring-boot:run -Dspring-boot.run.profiles=local

Useful Links

  • Swagger UI: http://localhost:8080/swagger-ui.html

  • Actuator Endpoint: http://localhost:8080/actuator

  • PgAdmin : http://localhost:5050(pgadmin4@pgadmin.org/admin)

Reference

  • https://stackoverflow.com/questions/25911359/how-to-split-read-only-and-read-write-transactions-with-jpa-and-hibernate/26026237#26026237

Architecture Image Credit :

Vlad