Databases 29 min read

Master ShardingSphere: 12 Sharding Strategies & Algorithms Explained

This comprehensive guide walks through ShardingSphere's core sharding strategies and the twelve built‑in sharding algorithms, showing how to configure each with YAML, illustrating their behavior with code examples, and highlighting practical tips for choosing the right approach in real‑world database partitioning scenarios.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
Master ShardingSphere: 12 Sharding Strategies & Algorithms Explained

Sharding Strategy

Sharding strategy is the combination of a sharding key and a sharding algorithm; the key determines how data is split, while the algorithm decides which physical shard stores the data.

Note: If an unsupported SQL operator (e.g., certain MySQL functions) is used in a sharding strategy, the system will ignore the strategy and perform full‑table routing.

ShardingSphere offers five core strategies: standard , inline , complex , hint , and none .

Standard Sharding Strategy

Applicable to scenarios with a single sharding key, supporting precise sharding (=, IN) and range sharding (BETWEEN, >, <, etc.).

spring:
  shardingsphere:
    rules:
      sharding:
        tables:
          t_order:
            shardingColumn: order_id
            shardingAlgorithmName: standard_algorithm

Inline Sharding Strategy

Uses a Groovy expression to compute the target shard directly, suitable for simple single‑key scenarios with = and IN operators.

spring:
  shardingsphere:
    rules:
      sharding:
        shardingAlgorithms:
          t_order_inline:
            type: INLINE
            props:
              algorithm-expression: t_order_${order_id % 3}
        tables:
          t_order:
            tableStrategy:
              standard:
                shardingColumn: order_id
                shardingAlgorithmName: t_order_inline

Complex Sharding Strategy

Handles multiple sharding keys; the shardingColumns property lists the keys, and the Groovy expression can combine them.

spring:
  shardingsphere:
    rules:
      sharding:
        shardingAlgorithms:
          t_order_complex:
            type: COMPLEX_INLINE
            props:
              sharding-columns: order_id,user_id
              algorithm-expression: t_order_${(order_id+user_id) % 3}

Hint Sharding Strategy

For cases where the sharding key is absent or routing must be forced, the Hint API supplies explicit database and table values.

spring:
  shardingsphere:
    rules:
      sharding:
        shardingAlgorithms:
          t_order_hint:
            type: HINT_INLINE
            props:
              algorithm-expression: t_order_${value % 3}
        tables:
          t_order:
            databaseStrategy:
              hint:
                shardingAlgorithmName: t_order_hint
            tableStrategy:
              hint:
                shardingAlgorithmName: t_order_hint

None Sharding Strategy

Disables sharding; all operations are routed to every physical table.

spring:
  shardingsphere:
    rules:
      sharding:
        tables:
          t_order:
            databaseStrategy:
              none: {}
            tableStrategy:
              none: {}

Sharding Algorithm

ShardingSphere provides four families of algorithms: automatic, standard, complex, and hint, each with several concrete types.

Start note: The official documentation for these algorithms is terse, making onboarding harder for beginners.

Automatic Sharding Algorithms

1. MOD

Simple modulo algorithm: (shardingKey / instance) % shardingCount. Only the sharding-count property is required.

spring:
  shardingsphere:
    rules:
      sharding:
        autoTables:
          t_order:
            shardingStrategy:
              standard:
                shardingColumn: order_date
                shardingAlgorithmName: t_order_mod
        shardingAlgorithms:
          t_order_mod:
            type: MOD
            props:
              sharding-count: 6

2. HASH_MOD

Hash‑based modulo algorithm, similar to MOD but applies a hash function first.

spring:
  shardingsphere:
    rules:
      sharding:
        shardingAlgorithms:
          t_order_hash_mod:
            type: HASH_MOD
            props:
              sharding-count: 6

3. VOLUME_RANGE

Distributes rows based on a fixed capacity per shard; useful for evenly spreading data and supporting frequent range queries.

spring:
  shardingsphere:
    rules:
      sharding:
        shardingAlgorithms:
          t_order_volume_range:
            type: VOLUME_RANGE
            props:
              range-lower: 2
              range-upper: 20
              sharding-volume: 10

4. BOUNDARY_RANGE

Uses explicit boundary values to map key ranges to shards, ideal for numeric range queries.

spring:
  shardingsphere:
    rules:
      sharding:
        shardingAlgorithms:
          t_order_boundary_range:
            type: BOUNDARY_RANGE
            props:
              sharding-ranges: 10,20,30,40

5. AUTO_INTERVAL

Time‑based automatic interval algorithm; shards data by time intervals defined by lower/upper bounds and a seconds interval.

spring:
  shardingsphere:
    rules:
      sharding:
        shardingAlgorithms:
          t_order_auto_interval:
            type: AUTO_INTERVAL
            props:
              datetime-lower: '2023-01-01 00:00:00'
              datetime-upper: '2025-01-01 00:00:00'
              sharding-seconds: 31536000

Standard Sharding Algorithms

6. INLINE

Row‑expression algorithm using Groovy; supports only = and IN operators.

spring:
  shardingsphere:
    rules:
      sharding:
        shardingAlgorithms:
          t_order_inline:
            type: INLINE
            props:
              algorithm-expression: t_order_${order_id % 3}
              allow-range-query-with-inline-sharding: false

7. INTERVAL

Time‑range algorithm for string‑type date fields; supports custom suffix patterns (e.g., yyyyMM) and interval units.

spring:
  shardingsphere:
    rules:
      sharding:
        shardingAlgorithms:
          t_order_interval:
            type: INTERVAL
            props:
              datetime-pattern: "yyyy-MM-dd HH:mm:ss"
              datetime-lower: "2024-01-01 00:00:00"
              datetime-upper: "2024-06-30 23:59:59"
              sharding-suffix-pattern: "yyyyMM"
              datetime-interval-unit: "MONTHS"
              datetime-interval-amount: 1

COSID Type Algorithms

8. COSID_MOD

Modulo algorithm based on CosId distributed IDs.

spring:
  shardingsphere:
    rules:
      sharding:
        shardingAlgorithms:
          t_order_cosid_mod:
            type: COSID_MOD
            props:
              mod: 3
              logic-name-prefix: t_order_

9. COSID_INTERVAL

Fixed‑time‑range algorithm using CosId; similar to INTERVAL but with CosId hashing.

spring:
  shardingsphere:
    rules:
      sharding:
        shardingAlgorithms:
          t_order_cosid_interval:
            type: COSID_INTERVAL
            props:
              zone-id: "Asia/Shanghai"
              logic-name-prefix: t_order_
              sharding-suffix-pattern: "yyyyMM"
              datetime-lower: "2024-01-01 00:00:00"
              datetime-upper: "2024-12-31 00:00:00"
              datetime-interval-unit: "MONTHS"
              datetime-interval-amount: 1

10. COSID_INTERVAL_SNOWFLAKE

Same as COSID_INTERVAL but the underlying ID generator uses Snowflake.

spring:
  shardingsphere:
    rules:
      sharding:
        shardingAlgorithms:
          t_order_cosid_interval_snowflake:
            type: COSID_INTERVAL_SNOWFLAKE
            props:
              zone-id: "Asia/Shanghai"
              logic-name-prefix: t_order_
              sharding-suffix-pattern: "yyyyMM"
              datetime-lower: "2024-01-01 00:00:00"
              datetime-upper: "2024-12-31 00:00:00"
              datetime-interval-unit: "MONTHS"
              datetime-interval-amount: 1

Complex Sharding Algorithm

11. COMPLEX_INLINE

Combines multiple sharding keys in a Groovy expression.

spring:
  shardingsphere:
    rules:
      sharding:
        shardingAlgorithms:
          t_order_complex_inline:
            type: COMPLEX_INLINE
            props:
              sharding-columns: order_id,user_id
              algorithm-expression: t_order_${(order_id+user_id) % 3}
              allow-range-query-with-inline-sharding: false

Hint Sharding Algorithm

12. HINT_INLINE

Forces routing via explicit values supplied through the Hint API.

spring:
  shardingsphere:
    rules:
      sharding:
        shardingAlgorithms:
          t_order_hint_inline:
            type: HINT_INLINE
            props:
              algorithm-expression: db${value % 2}
          t_order_table_hint_inline:
            type: HINT_INLINE
            props:
              algorithm-expression: t_order_${value % 3}

Summary

ShardingSphere‑JDBC supports twelve distinct sharding algorithms, each with unique characteristics. Selecting the appropriate algorithm requires understanding the specific business scenario, data distribution patterns, and query workload to achieve optimal performance and scalability.

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.

Spring BootShardingSpheredatabase shardingYAML configurationSharding Algorithms
Su San Talks Tech
Written by

Su San Talks Tech

Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.

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.