Databases 8 min read

Implementing Read/Write Splitting with ShardingSphere in a Spring Boot Application

This article explains how to implement database read‑write splitting in a Spring Boot application using ShardingSphere, covering the creation of master‑slave MySQL databases, Maven dependency setup, multi‑data‑source configuration, load‑balance algorithms, and handling replication lag with HintManager.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Implementing Read/Write Splitting with ShardingSphere in a Spring Boot Application

A startup developer named Xiao Wang built an initial single‑node MySQL‑backed app that soon required scaling as user traffic grew, prompting the need for read/write separation by introducing a master‑slave architecture.

Instead of manually configuring multiple data sources in Spring, the author recommends using ShardingSphere (formerly Sharding‑JDBC), an open‑source distributed database middleware that provides transparent read/write splitting, sharding, and transaction management.

Step 1: Create two MySQL databases (master and slave) and a user table in each:

CREATE DATABASE `ds_0` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
CREATE DATABASE `ds_1` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';

CREATE TABLE `user` (
    id BIGINT NOT NULL,
    city VARCHAR(20) NOT NULL,
    name VARCHAR(20) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Step 2: Add the ShardingSphere JDBC starter to the Maven project:

<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    <version>4.0.0-RC1</version>
</dependency>

Step 3: Configure master and slave data sources in application.yml (or application.properties ) and enable read/write splitting:

# Data source names
spring.shardingsphere.datasource.names=master,slave

# Master datasource
spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=123456

# Slave datasource
spring.shardingsphere.datasource.slave.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.slave.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
spring.shardingsphere.datasource.slave.username=root
spring.shardingsphere.datasource.slave.password=123456

# Master‑slave rule
spring.shardingsphere.masterslave.load-balance-algorithm-type=round_robin
spring.shardingsphere.masterslave.name=dataSource
spring.shardingsphere.masterslave.master-data-source-name=master
spring.shardingsphere.masterslave.slave-data-source-names=slave

With this configuration, read queries are automatically routed to the slave while write queries go to the master. The load‑balance algorithm can be set to ROUND_ROBIN or RANDOM to distribute reads among multiple slaves.

For cases where immediate consistency is required (e.g., reading right after an insert), the replication lag can be avoided by forcing the query to the master using ShardingSphere’s HintManager :

public List
list() {
    // Force routing to master
    HintManager.getInstance().setMasterRouteOnly();
    return userRepository.list();
}

The article also notes that the approach works with any ORM framework such as MyBatis or plain JdbcTemplate , and provides a GitHub repository for a complete runnable example.

MySQLShardingSphereSpringBootReadWriteSplittingDatabaseReplicationJdbcTemplate
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

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.