eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

1. Overview

Apache ShardingSphere is an open-source project that consists of a set of integrated tools for data processing. It provides a set of functionalities such as distributed database solutions, transactions, governance, and more.

In this tutorial, we’ll provide a quick overview of this ecosystem and a how-to-start guide.

2. What Is ShardingSphere?

Apache ShardingSphere, initially known as Sharding-JDBC, was created to tackle the problem of data sharding for Java applications. However, now it has expanded to a suite of tools including proxy, sidecar, and handling more than sharding.

When considering using ShardingSphere, it’s important to know what kind of advantages such a project brings to our solutions. A few points we can this are the following:

  • Performance: given the project’s maturity, the driver is close to a native JDBC in terms of efficiency and performance
  • Compatibility: the driver can be connected to any database that implements JDBC specifications; besides that, the proxy used by any application using MySQL and PostgreSQL
  • Zero Business intrusion: failover with no business impact
  • Low Ops and Maintenance Cost: fast learning curve, and it keeps minimal intervention on the current stack
  • Security and Stability: add extra capabilities while ensuring both
  • Elastic Extention: only expansion
  • Open Ecosystem: provides excellent flexibility

3. Use Cases

Now let’s go a bit further into the capabilities and briefly describe each of these use cases in the context of ShardingSphere.

3.1. Sharding

Sharding is the practice of splitting a database into smaller parts called shards, spread across multiple servers. ShardingSphere simplifies this process, allowing developers to distribute their data more effectively, improving their applications’ performance and scalability.

3.2. Distributed Transaction

A transaction may need to alter data on multiple databases in a distributed system. ShardingSphere provides a mechanism for managing these distributed transactions, ensuring data consistency across all databases involved.

3.3. Read/Write Splitting

This is a method of optimizing database access by directing read and write operations to different databases. ShardingSphere can automatically route read operations to replica databases and write operations to the primary database, thus balancing the load and increasing the system’s overall performance.

3.4. DB Gateway

ShardingSphere acts as a database gateway, abstracting the complexities of multiple databases into a unified data interface for the application. This allows developers to interact with various databases as if they were a single entity, simplifying database management.

3.5. Traffic Governance

ShardingSphere allows for fine-grained control over the data traffic in the system. It provides features like data sharding, read/write splitting, and more, which can efficiently distribute the traffic load among various resources.

3.6. Data Migration

ShardingSphere provides support for data migration between shards or databases. It helps smoothly redistribute data when scaling the system by adding or removing database nodes.

3.7. Encryption

ShardingSphere supports automatic data encryption before it’s saved to the database, providing an additional layer of security. This is particularly useful when dealing with sensitive data, such as user passwords or personal identifiable information.

3.8. Data Masking

Data masking is the process of hiding original data with modified content (characters or other data). ShardingSphere supports data masking, which is essential in non-production environments to ensure data privacy.

3.9. Shadow

The Shadow feature in ShardingSphere allows you to test the impact of database updates, new SQL statements, and indexes without affecting the actual production environment. It’s done by routing certain traffic to a shadow database in parallel to the actual database.

3.10. Observability

ShardingSphere provides a mechanism for monitoring the health and performance of your sharded databases. It supports metrics for query tracing, latency tracking, traffic insights, and more, enabling developers to observe and diagnose issues in real-time.

4. Getting Started

In order to introduce such technology and start getting used to it, let’s take an example of a Spring Boot application using Maven.

As mentioned, there are multiple capabilities available in the project. Therefore, to keep it simple, we’ll use only the sharding capability for now. Doing so lets us know how to configure and integrate the solution into our sample application.

4.1. Dependencies

The first step is to add the latest project dependency to our pom.xml:

<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>shardingsphere-jdbc-core</artifactId>
    <version>5.4.0</version>
</dependency>

That enables us to start configuring our data sources to use ShardingSphere.

4.2. Datasource Configuration

Now that we have the dependencies required, we must configure our data sources to use the ShardingSphere JDBC driver. Here we have to define the capabilities we want to use, in this example, the sharding capabilities.

The data of our Order table will be distributed across two MySQL instances based on the mod of the order_id field. To do so, we’ll create a sharding.yml file to hold the necessary configurations and place it under our resources folder:

dataSources:
  ds0:
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    driverClassName: com.mysql.jdbc.Driver
    jdbcUrl: jdbc:mysql://localhost:13306/ds0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
    username: test
    password: test
  ds1:
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    driverClassName: com.mysql.jdbc.Driver
    jdbcUrl: jdbc:mysql://localhost:13307/ds1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
    username: test
    password: test
rules:
  - !SHARDING
    tables:
      order:
        actualDataNodes: ds${0..1}.order
    defaultDatabaseStrategy:
      standard:
        shardingColumn: order_id
        shardingAlgorithmName: database_inline
    defaultTableStrategy:
      none:
    shardingAlgorithms:
      database_inline:
        type: INLINE
        props:
          algorithm-expression: ds${order_id % 2}
props:
  sql-show: false

Next, we need to configure JPA to use these settings.

4.3. JPA Configuration

Now, we need to connect our JPA/Spring Data setup to our ShardingSphere data sources. Now let’s adjust our application.yml to use the configuration just mentioned:

spring:
  datasource:
    driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver
    url: jdbc:shardingsphere:classpath:sharding.yml
  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL8Dialect
        ...

For the rest, our application should follow default Spring Data JPA patterns by defining our entities and repositories. For instance, in our case, we can consider the following classes:

@Entity
@Table(name = "`order`")
public class Order {

    @Id
    @Column(name = "order_id")
    private Long orderId;

    @Column(name = "customer_id")
    private Long customerId;

    @Column(name = "total_price")
    private BigDecimal totalPrice;

    @Enumerated(EnumType.STRING)
    @Column(name = "order_status")
    private Status orderStatus;

    @Column(name = "order_date")
    private LocalDate orderDate;

    @Column(name = "delivery_address")
    private String deliveryAddress;

    // ... getter and setters
}

This is the mapping of our Order class, and next, we can also see its respective repository:

public interface OrderRepository extends JpaRepository<Order, Long> { }

As we can observe, standard Spring JPA. No other code change is necessary at this point.

5. Connecting the Dots

With minimal changes, ShardingSphere enabled us to apply a sharding strategy to our table. However, no significant changes were needed in the application. Actually, only configuration changes at the persistence layer were required.

Thanks to the great integration of ShardingSphere with the JDBC drivers, our application can leverage advanced capabilities with nearly no code changes.

6. Conclusion

In this article, we gave our first steps using ShardingSphere. ShardingSphere is a powerful tool for managing and manipulating databases in distributed systems, and it offers a large range of advanced capabilities but abstracts a good amount of its complexity.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)