In this project, we will implement two Spring Boot Java Web application called, streamer-data-jpa and streamer-data-r2dbc.

Overview

spring-data-jpa-r2dbc-mysql-stream-million-records

In this project, we will implement two Spring Boot Java Web application called, streamer-data-jpa and streamer-data-r2dbc. They both will fetch 1 million of customer's data from MySQL and stream them to Kafka. The main goal is to compare the application's performance and resource utilization.

Applications

  • streamer-data-jpa

    Spring Boot Web Java application that connects to MySQL using Spring Data JPA and to Kafka.

    It provides some endpoints such as:

    • PATCH api/customers/stream-naive[?limit=x]: to stream customer records using a naive implementation with Spring Data JPA;
    • PATCH api/customers/stream[?limit=x]: to stream customer records using a better implementation with Java 8 Streams and Spring Data JPA as explained in this article.
    • PATCH api/customers/load?amount=x: to create a specific amount of random customer records.
  • streamer-data-r2dbc

    Spring Boot Web Java application that connects to MySQL using Spring Data R2DBC and to Kafka.

    It provides some endpoints such as:

    • PATCH api/customers/stream[?limit=x]: to stream customer records;
    • PATCH api/customers/load?amount=x: to create a specific amount of random customer records.

Prerequisites

Start Environment

  • Open a terminal and inside spring-data-jpa-r2dbc-mysql-stream-million-records root folder run

    docker-compose up -d
    
  • Wait for Docker containers to be up and running. To check it, run

    docker-compose ps
    
  • Once MySQL, Kafka and Zookeeper are up and running, run the following scripts

    • To create two Kafka topics

      ./init-kafka-topics.sh
      
    • To initialize MySQL database and to create two Kafka topics

      ./init-mysql-db.sh 1M
      

      Note: we can provide the following load amount values: 0, 100k, 200k, 500k or 1M

Run applications with Maven

Inside spring-data-jpa-r2dbc-mysql-stream-million-records, run the following Maven commands in different terminals

  • streamer-data-jpa

    ./mvnw clean spring-boot:run --projects streamer-data-jpa
    
  • streamer-data-r2dbc

    ./mvnw clean spring-boot:run --projects streamer-data-r2dbc
    

Run applications as Docker containers

  • Build Docker Images

    • In a terminal, make sure you are in spring-data-jpa-r2dbc-mysql-stream-million-records root folder
    • Run the following script to build the Docker images
      ./docker-build.sh
      
  • Environment Variables

    • streamer-data-jpa

      Environment Variable Description
      MYSQL_HOST Specify host of the MySQL database to use (default localhost)
      MYSQL_PORT Specify port of the MySQL database to use (default 3306)
      KAFKA_HOST Specify host of the Kafka message broker to use (default localhost)
      KAFKA_PORT Specify port of the Kafka message broker to use (default 29092)
    • streamer-data-r2dbc

      Environment Variable Description
      MYSQL_HOST Specify host of the MySQL database to use (default localhost)
      MYSQL_PORT Specify port of the MySQL database to use (default 3306)
      KAFKA_HOST Specify host of the Kafka message broker to use (default localhost)
      KAFKA_PORT Specify port of the Kafka message broker to use (default 29092)
  • Start Docker Containers

    Run the following docker run commands in different terminals

    • streamer-data-jpa

      docker run --rm --name streamer-data-jpa -p 9080:9080 \
        -e MYSQL_HOST=mysql -e KAFKA_HOST=kafka -e KAFKA_PORT=9092 \
        --network spring-data-jpa-r2dbc-mysql-stream-million-records_default \
        ivanfranchin/streamer-data-jpa:1.0.0
      
    • streamer-data-r2dbc

      docker run --rm --name streamer-data-r2dbc -p 9081:9081 \
        -e MYSQL_HOST=mysql -e KAFKA_HOST=kafka -e KAFKA_PORT=9092 \
        --network spring-data-jpa-r2dbc-mysql-stream-million-records_default \
        ivanfranchin/streamer-data-r2dbc:1.0.0
      

Simulation with 1 million customer records

Previously, during Start Environment step, we initialized MySQL with 1 million customer records.

Resource Consumption Monitoring Tool

Streaming customer records

In another terminal, call the following curl commands to trigger the streaming of customer records from MySQL to Kafka. At the end of the curl command, the total time it took (in seconds) to process will be displayed.

We can monitor the amount of messages and the messages themselves been streamed using Kafdrop – Kafka Web UI at http://localhost:9000

  • streamer-data-jpa

    Naive implementation

    curl -w "Response Time: %{time_total}s" -s -X PATCH localhost:9080/api/customers/stream-naive
    

    Better implementation

    curl -w "Response Time: %{time_total}s" -s -X PATCH localhost:9080/api/customers/stream
    
  • streamer-data-r2dbc

    curl -w "Response Time: %{time_total}s" -s -X PATCH localhost:9081/api/customers/stream
    

Sample

A simulation sample running the applications with Maven and using JConsole tool

  • streamer-data-jpa

    Naive implementation

    Response Time: 414.486126s
    

    jconsole-jpa-stream-naive

    Better implementation

    Response Time: 453.692525s
    

    jconsole-jpa-stream

  • streamer-data-r2dbc

    Response Time: 476.951654s
    

    jconsole-r2dbc-stream

Useful commands & links

  • Kafdrop

    Kafdrop can be accessed at http://localhost:9000

  • MySQL monitor

    To check data in customerdb database

    docker exec -it -e MYSQL_PWD=secret mysql mysql -uroot --database customerdb
    SELECT count(*) FROM customer;
    

    To create a dump from customer table in customerdb database, make sure you are in spring-data-jpa-r2dbc-mysql-stream-million-records root folder and run

    ./dump-mysql-db.sh
    

Shutdown

  • To stop streamer-data-jpa and streamer-data-r2dbc, go to the terminals were they are running and press Ctrl+C
  • To stop and remove docker-compose containers, network and volumes, go to a terminal and, inside spring-data-jpa-r2dbc-mysql-stream-million-records root folder, run the command below
    docker-compose down -v
    

Cleanup

To remove all Docker images created by this project, go to a terminal and, inside spring-data-jpa-r2dbc-mysql-stream-million-records root folder, run the following script

./remove-docker-images.sh
You might also like...

Spring Boot JdbcTemplate example with SQL Server: CRUD Rest API using Spring Data JDBC, Spring Web MVC

Spring Boot JdbcTemplate example with SQL Server: Build CRUD Rest API Build a Spring Boot CRUD Rest API example that uses Spring Data Jdbc to make CRU

Dec 20, 2022

Spring JPA @Query for custom query in Spring Boot example

Spring JPA @Query example (Custom query) in Spring Boot Use Spring JPA @Query for custom query in Spring Boot example: Way to use JPQL (Java Persisten

Dec 3, 2022

Spring JPA Native Query example in Spring Boot

Spring JPA Native Query example in Spring Boot

Nov 30, 2022

There are two challenges one is to create a backend api the other is to create a frontend application to consume the public data api devall.

There are two challenges one is to create a backend api the other is to create a frontend application to consume the public data api devall.

Sobre | Desafio | Resolução | Tecnologias | Execução | Itexto desafio tecnico Sobre os Desafios existem dois desafios um é criar uma api backend o out

Oct 18, 2021

An example of how to working with paging in Spring for GraphQL / Spring Data JPA

Spring for GraphQL Paging This repo contains the code for a live coding session I did on: Spring Data JPA GraphQL Paging & Sorting The reason I put th

Nov 28, 2022

A code sharing platform built using spring boot, hibernate and JPA as ORM with PostgreSQL which also follows a RESTful architecture.

Snap-Snippet A code sharing platform built using spring boot, hibernate and JPA as ORM with PostgreSQL which also follows a RESTful architecture. Tech

Nov 29, 2022

The respository of a student group called 'Bombenstimmung' at the University of Wuppertal during the SWT-Praktikum 2021/22

Bomberfrau The respository of a student group called 'Bombenstimmung' at the University of Wuppertal during the SWT-Praktikum 2021/22 Installation: Vo

Jan 10, 2022

Spring Boot Login and Registration example with MySQL, JWT, Rest Api - Spring Boot Spring Security Login example

Spring Boot Login and Registration example with MySQL, JWT, Rest Api - Spring Boot Spring Security Login example

Spring Boot Login example with Spring Security, MySQL and JWT Appropriate Flow for User Login and Registration with JWT Spring Boot Rest Api Architect

Jan 5, 2023

A web application to generate Java source code with spring-boot and mybatis-plus

A web application to generate Java source code with spring-boot and mybatis-plus

A web application to generate Java source code with spring-boot and mybatis-plus. Also, The class of Domain,Mapper,XML of Mapper Interface,Service,Controller are included. You can change the data source what you want to generate for your project in app running without restart this code -generator application.

Aug 29, 2022
Owner
Ivan Franchin
Ivan Franchin
该仓库中主要是 Spring Boot 的入门学习教程以及一些常用的 Spring Boot 实战项目教程,包括 Spring Boot 使用的各种示例代码,同时也包括一些实战项目的项目源码和效果展示,实战项目包括基本的 web 开发以及目前大家普遍使用的线上博客项目/企业大型商城系统/前后端分离实践项目等,摆脱各种 hello world 入门案例的束缚,真正的掌握 Spring Boot 开发。

Spring Boot Projects 该仓库中主要是 Spring Boot 的入门学习教程以及一些常用的 Spring Boot 实战项目教程,包括 Spring Boot 使用的各种示例代码,同时也包括一些实战项目的项目源码和效果展示,实战项目包括基本的 web 开发以及目前大家普遍使用的前

十三 4.5k Dec 30, 2022
Two Spring-boot applications registering themselves to an spring-boot-admin-server application as separate clients for the purpose of monitoring and managing the clients

Spring-boot-admin implementation with 1 Server and 2 clients Creating a Server application to monitor and manage Spring boot applications (clients) un

null 6 Dec 6, 2022
This Web Application Allows A user to upload a two minutes Video. It uses Server Side Capabilities of Nodejs and Spring Boot .

VideoStreamingApplication Purpose Of This Application These days trend of short videos are on rise youtube recently realsed "Shorts" . So , taking ins

Prateek Kumar 57 Nov 13, 2022
Dynamically filters JPA entities with a simple query syntax. Provides JPA/Hibernate predicates and Spring Data specifications.

Spring Filter You need a way to dynamically filter entities without any effort? Just add me to your pom.xml. Your API will gain a full featured search

Turkraft 142 Dec 13, 2022
Spring Boot JWT Authentication example with Spring Security & Spring Data JPA

Spring Boot JWT Authentication example with Spring Security & Spring Data JPA

null 1 Jan 26, 2022
循序渐进,学习Spring Boot、Spring Boot & Shiro、Spring Batch、Spring Cloud、Spring Cloud Alibaba、Spring Security & Spring Security OAuth2,博客Spring系列源码:https://mrbird.cc

Spring 系列教程 该仓库为个人博客https://mrbird.cc中Spring系列源码,包含Spring Boot、Spring Boot & Shiro、Spring Cloud,Spring Boot & Spring Security & Spring Security OAuth2

mrbird 24.8k Jan 6, 2023
An examples of creating test records in the database with Spring Boot + Spring Data + JPA usage.

Spring Boot + JPA — Clear Tests An examples of creating test records in the database with Spring Boot + Spring Data + JPA usage. Check out the article

Semyon Kirekov 8 Nov 24, 2022
Automatic creation of simple CRUD API of Spring boot and JPA project.

fast-crud Automatic creation of simple CRUD API of Spring boot and JPA project.

JinHwanKim 18 Oct 23, 2022
The goal of the project is to create a web application using Java EE and database (PostgreSQL) without connecting a modern technology stack like spring boot and hibernate

About The Project SignIn page SignUp page Profile page The goal of the project is to create a web application using Java EE and database (PostgreSQL)

Islam Khabibullin 2 Mar 23, 2022
Spring JPA Many To Many example with Hibernate and Spring Boot CRUD Rest API - ManyToMany annotation

Spring JPA Many To Many example with Hibernate and Spring Boot CRUD Rest API - ManyToMany annotation

null 17 Dec 28, 2022