DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Advanced Search and Filtering API Using Spring Data and MongoDB
  • Manage Hierarchical Data in MongoDB With Spring
  • Upgrade Guide To Spring Boot 3.0 for Spring Data JPA and Querydsl
  • The Generic Way To Convert Between Java and PostgreSQL Enums

Trending

  • The Man Who Invented Selenium Also Built Its Replacement
  • How Performance Engineers Find and Fix Hidden System Bottlenecks
  • How to Keep AI Agents From Going Rogue Using Docker Sandbox
  • 9 Practical Ways to Fix Laravel Performance Issues in Production

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Read Replicas and Spring Data Part 1: Configuring the Databases

Read Replicas and Spring Data Part 1: Configuring the Databases

The first part of this series of articles takes a look at read replicas and Spring Data. Let's see how to configure the databases.

By 
Emmanouil Gkatziouras
DZone Core CORE ·
Oct. 11, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
19.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Image title

Read replicas

This is a series of blog posts on our quest to increase our application's performance by utilizing read replicas.

For this project, our goal is to set up our spring data application and use read repositories for writes and
repositories based on read replicas for reads.

In order to simulate this environment, we shall use PostgreSQL instances through Docker.

You might also like:  The Magic of Spring Data

The motives are simple. Your Spring application has become increasingly popular and you want it to handle more requests. Most of the applications out there have a higher demand for read operations rather than write operations. Thus, I assume that your application falls into the same category.

Although SQL databases are not horizontally scalable on their own, you can work your way with them by using read replicas.

Our goal is not to make an actual Read replication in PostgreSQL; therefore, instead of configuring any replication, we will just copy some data from both databases.

This is the script we shall use to populate the databases.

#!/bin/bash
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
    create schema spring_data_jpa_example;

    create table spring_data_jpa_example.employee(
        id  SERIAL PRIMARY KEY,
        firstname   TEXT    NOT NULL,
        lastname    TEXT    NOT NULL,
        email       TEXT    not null,
        age         INT     NOT NULL,
        salary         real,
        unique(email)
    );

    insert into spring_data_jpa_example.employee (firstname,lastname,email,age,salary)
    values ('John','Doe 1','[email protected]',18,1234.23);
    insert into spring_data_jpa_example.employee (firstname,lastname,email,age,salary)
    values ('John','Doe 2','[email protected]',19,2234.23);
    insert into spring_data_jpa_example.employee (firstname,lastname,email,age,salary)
    values ('John','Doe 3','[email protected]',20,3234.23);
    insert into spring_data_jpa_example.employee (firstname,lastname,email,age,salary)
    values ('John','Doe 4','[email protected]',21,4234.23);
    insert into spring_data_jpa_example.employee (firstname,lastname,email,age,salary)
    values ('John','Doe 5','[email protected]',22,5234.23);
EOSQL

Since we are using Docker and Docker Compose, the script above shall be used to initialize the database.

Now let's create our Docker Compose stack.

version: '3.5'

services:
  write-db:
    image: postgres
    restart: always
    environment:
      POSTGRES_USER: db-user
      POSTGRES_PASSWORD: your-password
      POSTGRES_DB: postgres
    networks:
      - postgresql-network
    ports:
      - "127.0.0.2:5432:5432"
    volumes:
      - $PWD/init-db-script.sh:/docker-entrypoint-initdb.d/init-db-script.sh
  read-db-1:
    image: postgres
    restart: always
    environment:
      POSTGRES_USER: db-user
      POSTGRES_PASSWORD: your-password
      POSTGRES_DB: postgres
    networks:
      - postgresql-network
    ports:
      - "127.0.0.3:5432:5432"
    volumes:
      - $PWD/init-db-script.sh:/docker-entrypoint-initdb.d/init-db-script.sh
networks:
  postgresql-network:
    name: postgresql-network

As you see, our configuration is pretty simple. If you are careful enough, you can see that I gave the number one to the read-db. This is because in the future, we will add more replicas to it.

I also bound the machines to different local ips.

If you have problem binding addresses like 127.0.0.*:5432, you should try:

sudo ifconfig lo0 alias 127.0.0.2 up
sudo ifconfig lo0 alias 127.0.0.3 up

If you are unsuccessful, then just change the ports and it will work. It might not be as convenient, but it's still ok.

So let's get our Docker Compose stack up and running.

docker-compose -f ./postgresql-stack.yaml up

We must be able to query data in both PostgreSQL instances.

docker exec -it deploy_read-db-1_1 /bin/bash
root@07c502968cb3:/# psql -v --username "$POSTGRES_USER" --dbname "$POSTGRES_DB"
db-user=# select*from spring_data_jpa_example.employee;
 id | firstname | lastname |     email     | age | salary
----+-----------+----------+---------------+-----+---------
  1 | John      | Doe 1    | [email protected] |  18 | 1234.23
  2 | John      | Doe 2    | [email protected] |  19 | 2234.23
  3 | John      | Doe 3    | [email protected] |  20 | 3234.23
  4 | John      | Doe 4    | [email protected] |  21 | 4234.23
  5 | John      | Doe 5    | [email protected] |  22 | 5234.23
(5 rows)

We are pretty much set up for our next step. We have some databases up and running, and we are going to spin up a spring application running upon them. The next article focuses on implementing an application running upon our primary database.

Further Reading

Spring Boot With Spring Data JPA

Introduction to Spring Data and Spring Data JPA

Database Spring Framework Spring Data Data (computing)

Published at DZone with permission of Emmanouil Gkatziouras. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Advanced Search and Filtering API Using Spring Data and MongoDB
  • Manage Hierarchical Data in MongoDB With Spring
  • Upgrade Guide To Spring Boot 3.0 for Spring Data JPA and Querydsl
  • The Generic Way To Convert Between Java and PostgreSQL Enums

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook