Databases 6 min read

Master ShardingSphere: Implement Database Sharding, Read‑Write Splitting & Distributed Transactions

This article introduces ShardingSphere, an open‑source distributed database middleware, explains its core features such as horizontal sharding, read‑write separation, and distributed transactions, and provides a step‑by‑step configuration and Java usage example.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Master ShardingSphere: Implement Database Sharding, Read‑Write Splitting & Distributed Transactions

ShardingSphere is an open‑source distributed database middleware that provides data sharding, read‑write separation, distributed transactions and cross‑data‑source queries for large‑scale data processing.

Its main capabilities include horizontal database partitioning, read‑write splitting, support for distributed ACID transactions, and the ability to execute queries across multiple data sources.

Architecture

ShardingSphere consists of three sub‑projects: Sharding‑JDBC, Sharding‑Proxy and Sharding‑Sidecar.

Sharding‑JDBC

A lightweight Java framework built on JDBC that routes SQL statements to multiple databases according to sharding rules, supporting MySQL, Oracle, SQL Server, PostgreSQL, etc.

Sharding‑Proxy

A transparent database proxy that implements the MySQL protocol, enabling database‑level sharding, read‑write separation and distributed transactions without modifying application code.

Sharding‑Sidecar

A sidecar based on Service Mesh (e.g., Kubernetes, Istio) that integrates ShardingSphere into containerized environments for high availability and elastic scaling.

Quick Start Example

1. Add the ShardingSphere dependency:

<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>sharding-jdbc-core</artifactId>
    <version>5.0.0-alpha</version>
</dependency>

2. Configure sharding rules (YAML):

spring:
  shardingsphere:
    datasource:
      names: ds0, ds1
      ds0:
        url: jdbc:mysql://localhost:3306/db0?serverTimezone=UTC
        username: root
        password: root
      ds1:
        url: jdbc:mysql://localhost:3306/db1?serverTimezone=UTC
        username: root
        password: root
    rules:
      - !SHARDING
        tables:
          t_order:
            actualDataNodes: ds${0..1}.t_order_${0..1}
            tableStrategy:
              standard:
                shardingColumn: order_id
                preciseAlgorithmClassName: com.example.algorithm.PreciseShardingAlgorithm
                rangeAlgorithmClassName: com.example.algorithm.RangeShardingAlgorithm

3. Start the proxy layer (optional):

./sharding-proxy.sh conf/config-sharding.yaml

4. Use the datasource in Java:

// Obtain DataSource
DataSource dataSource = ShardingSphereDataSourceFactory.createDataSource(shardingSphereConfig);
// Get connection
Connection connection = dataSource.getConnection();
// Execute SQL
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM t_order WHERE user_id = 1");
// Process results
while (resultSet.next()) {
    // do something
}
// Close resources
resultSet.close();
statement.close();
connection.close();

This example demonstrates how to configure and use ShardingSphere to shard a MySQL database.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavamiddlewareShardingSphereDistributed TransactionsRead-Write Splitting
Mike Chen's Internet Architecture
Written by

Mike Chen's Internet Architecture

Over ten years of BAT architecture experience, shared generously!

0 followers
Reader feedback

How this landed with the community

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.