Databases 18 min read

Sharding-JDBC Advanced Guide: Configuration and Practical Implementation

This article provides a comprehensive guide to Sharding-JDBC, covering the challenges of database sharding such as distributed transactions, cross‑node joins, pagination, global key collisions, and demonstrates practical configuration and code examples for vertical and horizontal partitioning using Spring Boot.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Sharding-JDBC Advanced Guide: Configuration and Practical Implementation

This article introduces Sharding-JDBC, the lightweight Java framework for transparent sharding of databases, and explains why sharding (both vertical and horizontal) is necessary for performance and scalability.

Problems introduced by sharding include distributed transaction handling, cross‑node JOIN difficulties, pagination and sorting across nodes, and global primary‑key collisions. Various solutions such as global tables, field redundancy, data assembly, and binding tables are discussed.

It also reviews common distributed primary‑key generation strategies (UUID, Snowflake, Baidu UidGenerator, Meituan Leaf) and their trade‑offs.

Sharding-JDBC Overview

Lightweight Java framework based on JDBC

Supports any Java ORM (Hibernate, MyBatis, Spring JDBC Template)

Works with any connection pool (Druid, HikariCP, etc.)

Compatible with MySQL, Oracle, SQLServer, PostgreSQL

Key concepts

Logical table – the abstract table name used in sharding rules (e.g., t_order )

Actual table – the physical tables after sharding (e.g., t_order_1 , t_order_2 )

Data node – combination of datasource name and actual table (e.g., ds1.t_order_1 )

Sharding key – column used for partitioning

Sharding algorithm – implements precise (=), range (BETWEEN), IN, etc.

Sharding strategy – sharding key + algorithm (standard, composite, inline, hint)

Distributed key generation – UUID, Snowflake, etc.

Practical implementation with Spring Boot

1. Add Maven dependency:

<dependency>
  <groupId>org.apache.shardingsphere</groupId>
  <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
  <version>${sharding-sphere.version}</version>
</dependency>

2. Configure vertical sharding (different databases for product and shop):

spring:
  shardingsphere:
    datasource:
      names: ds1,ds2
      ds1:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/product_db?useUnicode=true&characterEncoding=utf-8
        username: root
        password: Nov2014
      ds2:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/shop_db?useUnicode=true&characterEncoding=utf-8
        username: root
        password: Nov2014
    sharding:
      tables:
        product_base:
          actual-data-nodes: ds$->{1}.product_base
          key-generator:
            column: product_id
            type: SNOWFLAKE
        product_description:
          actual-data-nodes: ds$->{1}.product_description
        shop:
          actual-data-nodes: ds$->{2}.shop
          key-generator:
            column: shop_id
            type: SNOWFLAKE

3. Horizontal sharding of the product database using modulo on product_id :

spring:
  shardingsphere:
    sharding:
      tables:
        product_base:
          database-strategy:
            inline:
              sharding-column: product_id
              algorithm-expression: ds$->{product_id%2+1}
        product_description:
          database-strategy:
            inline:
              sharding-column: product_id
              algorithm-expression: ds$->{product_id%2+1}

4. Horizontal table sharding based on shop_id :

spring:
  shardingsphere:
    sharding:
      tables:
        product_base:
          actual-data-nodes: ds$->{1..2}.product_base_$->{1..2}
          table-strategy:
            inline:
              sharding-column: shop_id
              algorithm-expression: product_base_$->{shop_id%2+1}
        product_description:
          actual-data-nodes: ds$->{1..2}.product_description_$->{1..2}
          table-strategy:
            inline:
              sharding-column: shop_id
              algorithm-expression: product_description_$->{shop_id%2+1}

Sample JUnit test inserting data demonstrates automatic routing to the correct datasource and table:

@Test
public void testInsert() {
    Product product = Product.builder()
        .name("Spring Cloud Alibaba Course")
        .price(159L)
        .originAddress("Code Monkey Column")
        .shopId(1L)
        .build();
    productMapper.insertProductBase(product);
}

The console output shows SQL statements being sent to both ds1 and ds2 , confirming successful sharding.

Conclusion

The article walks through the essential concepts of Sharding-JDBC and provides end‑to‑end configuration and code examples for vertical and horizontal sharding, enabling readers to apply database partitioning in real‑world Java projects.

Spring Bootdatabase shardingdistributed transactionshorizontal partitioningSharding-JDBC
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.