Cache for Sharding Route

Background

This feature is experimental and needs to be used with the data sharding rule. The cache for sharding route will put the logical SQL, the parameter value of the shard key, and the routing result into the cache, exchange space for time, and reduce CPU usage of the routing logic.

We recommend enabling it only if the following conditions are met:

  • Pure OLTP scenarios.
  • The CPU of the machine which deployed the ShardingSphere process has reached the bottleneck.
  • Most of the CPUs are used by ShardingSphere routing logic.
  • All SQLs are optimized and each SQL execution could be routed to a single data node.

If the above conditions are not met, the execution delay of SQL may not be significantly improved, and the memory pressure will be increased.

Parameters

Class:org.apache.shardingsphere.sharding.api.config.cache.ShardingCacheConfiguration

Attributes:

nameDataTypeDescriptionDefault Value
allowedMaxSqlLengthint允许缓存的 SQL 长度限制-
routeCacheorg.apache.shardingsphere.sharding.api.config.cache.ShardingCacheOptionsConfiguration路由缓存-

Class:org.apache.shardingsphere.sharding.api.config.cache.ShardingCacheOptionsConfiguration

Attributes:

nameDataTypeDescriptionDefault Value
softValuesboolean是否软引用缓存值-
initialCapacityint缓存初始容量-
maximumSizeint缓存最大容量-

Sample

  1. public final class ShardingDatabasesAndTablesConfigurationPrecise {
  2. @Override
  3. public DataSource getDataSource() throws SQLException {
  4. return ShardingSphereDataSourceFactory.createDataSource(createDataSourceMap(), Arrays.asList(createShardingRuleConfiguration(), createBroadcastRuleConfiguration())), new Properties());
  5. }
  6. private ShardingRuleConfiguration createShardingRuleConfiguration() {
  7. ShardingRuleConfiguration result = new ShardingRuleConfiguration();
  8. result.getTables().add(getOrderTableRuleConfiguration());
  9. result.getTables().add(getOrderItemTableRuleConfiguration());
  10. // ...
  11. result.setShardingCache(new ShardingCacheConfiguration(512, new ShardingCacheConfiguration.RouteCacheConfiguration(65536, 262144, true)));
  12. return result;
  13. }
  14. private ShardingTableRuleConfiguration getOrderTableRuleConfiguration() {
  15. ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_order", "demo_ds_${0..1}.t_order_${[0, 1]}");
  16. result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "snowflake"));
  17. result.setAuditStrategy(new ShardingAuditStrategyConfiguration(Collections.singleton("sharding_key_required_auditor"), true));
  18. return result;
  19. }
  20. private ShardingTableRuleConfiguration getOrderItemTableRuleConfiguration() {
  21. ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_order_item", "demo_ds_${0..1}.t_order_item_${[0, 1]}");
  22. result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_item_id", "snowflake"));
  23. return result;
  24. }
  25. private Map<String, DataSource> createDataSourceMap() {
  26. Map<String, DataSource> result = new HashMap<>();
  27. result.put("demo_ds_0", DataSourceUtil.createDataSource("demo_ds_0"));
  28. result.put("demo_ds_1", DataSourceUtil.createDataSource("demo_ds_1"));
  29. return result;
  30. }
  31. private BroadcastRuleConfiguration createBroadcastRuleConfiguration() {
  32. return new BroadcastRuleConfiguration(Collections.singletonList("t_address"));
  33. }
  34. }