Logo
Sharding Algorithm

Sharding Algorithm #

Auto Sharding Algorithm #

Modulo Sharding Algorithm #

Type: MOD

Configurable Properties:

Property nameData typeDescription
sharding-countintNumber of shards

Hash Modulo Sharding Algorithm #

Type: HASH_MOD

Configurable Properties:

Property nameData typeDescription
sharding-countintNumber of shards

Volume Based Range Sharding Algorithm #

Type: VOLUME_RANGE

Configurable Properties:

Property nameData typeDescription
range-lowerlongRange lower bound, throw exception if lower than bound
range-upperlongRange upper bound, throw exception if upper than bound
sharding-volumelongSharding volume

Boundary Based Range Sharding Algorithm #

Type: BOUNDARY_RANGE

Configurable Properties:

Property nameData typeDescription
sharding-rangesStringRange of sharding border, multiple boundaries separated by commas

Auto Interval Sharding Algorithm #

Type: AUTO_INTERVAL

Configurable Properties:

Property nameData typeDescription
datetime-lowerStringShard datetime begin boundary, pattern: yyyy-MM-dd HH:mm:ss
datetime-upperStringShard datetime end boundary, pattern: yyyy-MM-dd HH:mm:ss
sharding-secondslongMax seconds for the data in one shard, allows sharding key timestamp format seconds with time precision, but time precision after seconds is automatically erased

Consistent Hash Sharding Algorithm #

Type: SphereEx:CONSISTENT_HASH

Configurable Properties:

Property nameData typeDescription
sharding-countintRequired, used to specify the number of shards.
sharding-node-weightStringOptional, the weight of sharding data nodes, separated by commas, for example: “2,2,1,1”. The greater the weight, the more virtual nodes are used for consistent Hash computing, and the default weight is 1.
virtual-node-countintRequired item, the number of virtual nodes corresponding to the sharding data nodes. The more the number of virtual nodes, the more balanced the sharding, but too many virtual nodes will also affect the performance.
consistent-hash-seedintOptional, the seed parameter for computing consistent Hash, the default value is 0.

HASH RANGE Algorithm #

Type: SphereEx:HASH_RANGE

Configurable Properties:

Property NameData TypeDescription
partition-countintNumber of partitions
partition-lengthintLength of partitions

Precise Routing Explanation:

partitionCount: C1, C2...Cn
partitionLength: L1, L2...Ln
By configuring these two items, calculate the cardinality M = C1*L1 + C2*L2 + ... + Cn*Ln
Routing logic: shardingValue % M, then see which partition it falls into
For example:
partitionCount is configured as 2, 3
partitionLength is configured as 100, 50
A total of 5 Ranges, the first two ranges are each 100 in size, and the last three ranges are each 50 in size
So M = 2 * 100 + 3 * 50 = 350
For example, if the routing sharingValue = 805, then 805 % 350 = 105, falls into the first range (indexed from 0), corresponding to t_order_0.

String Hash Range Algorithm #

Type: SphereEx:STRING_HASH_RANGE

Configurable Properties:

Property NameData TypeDescription
partition-countintNumber of partitions
partition-lengthintLength of partitions

Precise Routing Explanation:

Sharding route logic:
Precise routing:
1. Substring according to shardingValue string
2. Calculate the hash value of the substring
3. Modulo (based on partitionLength and partitionCount jointly calculated cardinality M.)
4. Calculate which range it falls into
5. Route to the real table
   Range routing: Full routing

Standard Sharding Algorithm #

The builtin standard sharding algorithm implementation classes of DB Plus Engine include:

Inline Sharding Algorithm #

With Groovy expressions, InlineShardingStrategy provides single-key support for the sharding operation of = and IN in SQL. Simple sharding algorithms can be used through a simple configuration to avoid laborious Java code developments. For example, t_user_$->{u_id % 8} means table t_user is divided into 8 tables according to u_id, with table names from t_user_0 to t_user_7. Please refer to Inline Expression for more details.

Type: INLINE

Configurable Properties:

Property nameData typeDescriptionDefault value
algorithm-expressionStringInline expression sharding algorithm
allow-range-query-with-inline-sharding (?)booleanWhether range query is allowed. Note: range query will ignore sharding strategy and conduct full routingfalse

Interval Sharding Algorithm #

Type: INTERVAL

Configurable Properties:

NameDataTypeDescriptionDefault Value
datetime-patternStringTimestamp pattern of sharding value, must can be transformed to Java LocalDateTime. For example: yyyy-MM-dd HH:mm:ss, yyyy-MM-dd or HH:mm:ss etc. But Gy-MM etc. related to java.time.chrono.JapaneseDate are not supported-
datetime-lowerStringDatetime sharding lower boundary, pattern is defined datetime-pattern-
datetime-upper (?)StringDatetime sharding upper boundary, pattern is defined datetime-patternNow
sharding-suffix-patternStringSuffix pattern of sharding data sources or tables, must can be transformed to Java LocalDateTime, must be consistent with datetime-interval-unit. For example: yyyyMM-
datetime-interval-amount (?)intInterval of sharding value, after which the next shard will be entered1
datetime-interval-unit (?)StringUnit of sharding value interval, must can be transformed to Java ChronoUnit’s Enum value. For example: MONTHSDAYS

Complex Sharding Algorithm #

Complex Inline Sharding Algorithm #

Please refer to Inline Expression for more details.

Type: COMPLEX_INLINE

NameDataTypeDescriptionDefault Value
sharding-columns (?)Stringsharing column names, Multiple columns need to be separated by commas. If it is not configured, it cannot be verified
algorithm-expressionStringInline expression sharding algorithm
allow-range-query-with-inline-sharding (?)booleanWhether range query is allowed. Note: range query will ignore sharding strategy and conduct full routingfalse

Hint Sharding Algorithm #

Hint Inline Sharding Algorithm #

Please refer to Inline Expression for more details.

Type: HINT_INLINE

NameDataTypeDescriptionDefault Value
algorithm-expression (?)StringInline expression sharding algorithm${value}

Class Based Sharding Algorithm #

Custom extensions can be realised by configuring the sharding strategy type and algorithm class name.

Type: CLASS_BASED

Configurable Properties:

NameDataTypeDescription
strategyStringSharding strategy type, support STANDARD, COMPLEX or HINT (case insensitive)
algorithmClassNameStringFully qualified name of sharding algorithm