CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-sentinel-parameter-flow-control

Sentinel Parameter Flow Control provides functionality of flow control by frequent (hot spot) parameters.

Pending
Overview
Eval results
Files

cluster-mode.mddocs/

Cluster Mode

Advanced functionality for distributed parameter flow control across multiple instances. ParamFlowClusterConfig enables coordinated parameter limits in cluster environments, allowing flow control decisions to be made globally across all service instances.

Capabilities

Cluster Configuration

Configures parameter flow rules for cluster mode operation with distributed coordination.

/**
 * Parameter flow rule config in cluster mode
 */
public class ParamFlowClusterConfig {
    public ParamFlowClusterConfig();
}

Usage Example:

// Create cluster configuration for distributed parameter flow control
ParamFlowClusterConfig clusterConfig = new ParamFlowClusterConfig()
    .setFlowId(12345L)
    .setThresholdType(ClusterRuleConstant.FLOW_THRESHOLD_GLOBAL)
    .setFallbackToLocalWhenFail(true);

// Apply to parameter flow rule
ParamFlowRule rule = new ParamFlowRule("userService")
    .setParamIdx(0)
    .setCount(100)  // 100 QPS across entire cluster
    .setClusterMode(true)
    .setClusterConfig(clusterConfig);

Flow ID Configuration

Sets a global unique identifier for the parameter flow rule in cluster mode.

/**
 * Set global unique ID for cluster flow control
 * @param flowId global unique identifier
 * @return this config for method chaining
 */
public ParamFlowClusterConfig setFlowId(Long flowId);

/**
 * Get the global unique ID
 * @return flow ID
 */
public Long getFlowId();

Usage Example:

// Configure unique flow ID for cluster coordination
ParamFlowClusterConfig config = new ParamFlowClusterConfig()
    .setFlowId(1001L);  // Unique ID across all cluster instances

// Different services can use different flow IDs
ParamFlowClusterConfig userServiceConfig = new ParamFlowClusterConfig()
    .setFlowId(2001L);

ParamFlowClusterConfig productServiceConfig = new ParamFlowClusterConfig()
    .setFlowId(2002L);

Threshold Type Configuration

Specifies how the threshold should be calculated across cluster instances.

/**
 * Set threshold type (average by local value or global value)
 * @param thresholdType the threshold calculation type
 * @return this config for method chaining
 */
public ParamFlowClusterConfig setThresholdType(int thresholdType);

/**
 * Get the threshold type
 * @return threshold type
 */
public int getThresholdType();

Usage Example:

import com.alibaba.csp.sentinel.slots.block.ClusterRuleConstant;

// Global threshold - 100 QPS across entire cluster
ParamFlowClusterConfig globalConfig = new ParamFlowClusterConfig()
    .setFlowId(1001L)
    .setThresholdType(ClusterRuleConstant.FLOW_THRESHOLD_GLOBAL);

// Average local threshold - 100 QPS per instance on average
ParamFlowClusterConfig avgLocalConfig = new ParamFlowClusterConfig()
    .setFlowId(1002L)
    .setThresholdType(ClusterRuleConstant.FLOW_THRESHOLD_AVG_LOCAL);

// Example: If you have 4 instances
// Global: Total 100 QPS shared across all 4 instances
// Average Local: Each instance can handle ~25 QPS (100/4)

Fallback Configuration

Configures fallback behavior when cluster coordination fails.

/**
 * Set fallback behavior when cluster coordination fails
 * @param fallbackToLocalWhenFail true to fallback to local rules
 * @return this config for method chaining
 */
public ParamFlowClusterConfig setFallbackToLocalWhenFail(boolean fallbackToLocalWhenFail);

/**
 * Check if fallback to local is enabled
 * @return true if fallback is enabled
 */
public boolean isFallbackToLocalWhenFail();

Usage Example:

// Enable fallback to local rules when cluster fails
ParamFlowClusterConfig resilientConfig = new ParamFlowClusterConfig()
    .setFlowId(1001L)
    .setFallbackToLocalWhenFail(true);  // Fallback to local mode

// Disable fallback - pass all requests when cluster fails
ParamFlowClusterConfig strictConfig = new ParamFlowClusterConfig()
    .setFlowId(1002L)
    .setFallbackToLocalWhenFail(false); // Pass all when cluster unavailable

// Apply to rules
ParamFlowRule resilientRule = new ParamFlowRule("criticalService")
    .setCount(50)
    .setClusterMode(true)
    .setClusterConfig(resilientConfig);

Sampling Configuration

Configures statistical sampling for cluster coordination performance.

/**
 * Set sample count for cluster statistics
 * @param sampleCount number of samples
 * @return this config for method chaining
 */
public ParamFlowClusterConfig setSampleCount(int sampleCount);

/**
 * Get the sample count
 * @return sample count
 */
public int getSampleCount();

Usage Example:

// Configure sampling for high-performance cluster coordination
ParamFlowClusterConfig config = new ParamFlowClusterConfig()
    .setFlowId(1001L)
    .setSampleCount(10);    // Use 10 samples for statistics

// Higher sample count for more accurate statistics
ParamFlowClusterConfig preciseConfig = new ParamFlowClusterConfig()
    .setFlowId(1002L)
    .setSampleCount(20);    // More samples, higher accuracy, more overhead

Window Interval Configuration

Sets the time interval for statistical sliding windows in cluster mode.

/**
 * Set the time interval length of the statistic sliding window (in milliseconds)
 * @param windowIntervalMs window interval in milliseconds
 * @return this config for method chaining
 */
public ParamFlowClusterConfig setWindowIntervalMs(int windowIntervalMs);

/**
 * Get the window interval
 * @return window interval in milliseconds
 */
public int getWindowIntervalMs();

Usage Example:

// Configure sliding window for cluster statistics
ParamFlowClusterConfig config = new ParamFlowClusterConfig()
    .setFlowId(1001L)
    .setWindowIntervalMs(1000);  // 1-second sliding window

// Shorter window for more responsive flow control
ParamFlowClusterConfig responsiveConfig = new ParamFlowClusterConfig()
    .setFlowId(1002L)
    .setWindowIntervalMs(500);   // 500ms window, more responsive

// Longer window for smoother flow control
ParamFlowClusterConfig smoothConfig = new ParamFlowClusterConfig()
    .setFlowId(1003L)
    .setWindowIntervalMs(2000);  // 2-second window, smoother

Complete Cluster Configuration Example

import com.alibaba.csp.sentinel.slots.block.flow.param.*;
import com.alibaba.csp.sentinel.slots.block.ClusterRuleConstant;

// Configure comprehensive cluster parameter flow control
ParamFlowClusterConfig clusterConfig = new ParamFlowClusterConfig()
    .setFlowId(12345L)                                          // Unique cluster flow ID
    .setThresholdType(ClusterRuleConstant.FLOW_THRESHOLD_GLOBAL) // Global threshold across cluster
    .setFallbackToLocalWhenFail(true)                           // Fallback to local when cluster fails
    .setSampleCount(10)                                         // Use 10 samples for statistics
    .setWindowIntervalMs(1000);                                 // 1-second sliding window

// Create parameter flow rule with cluster configuration
ParamFlowRule clusterRule = new ParamFlowRule("userService")
    .setParamIdx(0)                    // Monitor first parameter (userId)
    .setCount(200)                     // 200 QPS across entire cluster
    .setGrade(RuleConstant.FLOW_GRADE_QPS)
    .setClusterMode(true)              // Enable cluster mode
    .setClusterConfig(clusterConfig);  // Apply cluster configuration

// Exception items work in cluster mode too
ParamFlowItem vipException = ParamFlowItem.newItem("VIP_USER", 500);
clusterRule.setParamFlowItemList(Arrays.asList(vipException));

// Load the cluster rule
ParamFlowRuleManager.loadRules(Arrays.asList(clusterRule));

Cluster Architecture

Cluster Token Server

  • Centralized coordination point for flow control decisions
  • Maintains global statistics and token allocation
  • Can be embedded or standalone

Cluster Token Client

  • Requests tokens from cluster server before processing requests
  • Falls back to local mode when server is unavailable
  • Caches decisions for performance

Flow Control Process in Cluster Mode

  1. Request arrives at any cluster instance
  2. Parameter extracted from method arguments
  3. Token requested from cluster server with parameter info
  4. Global statistics checked across all instances
  5. Decision made based on global parameter frequency
  6. Token granted or denied and returned to client
  7. Local fallback triggered if cluster server unavailable

Configuration Best Practices

  1. Use unique flow IDs: Ensure each parameter flow rule has a unique cluster flow ID
  2. Configure appropriate fallback: Enable fallback for critical services
  3. Tune sample count: Balance accuracy vs. performance based on traffic patterns
  4. Set reasonable window intervals: Shorter windows are more responsive but less stable
  5. Monitor cluster health: Ensure cluster server availability for consistent behavior
  6. Test fallback scenarios: Verify local fallback works correctly when cluster fails

Cluster Mode vs Local Mode

AspectLocal ModeCluster Mode
ScopePer instanceGlobal across cluster
CoordinationNoneCentralized token server
ConsistencyInstance-levelCluster-level
LatencyLowHigher (network calls)
AvailabilityHighDepends on cluster server
Use CaseSimple deploymentsDistributed systems

Install with Tessl CLI

npx tessl i tessl/maven-sentinel-parameter-flow-control

docs

cluster-mode.md

exception-items.md

flow-control-exceptions.md

index.md

parameter-flow-rules.md

rule-management.md

tile.json