How Sentinel Beats Hystrix: Deep Dive into Alibaba’s Flow Control Engine
This article introduces Alibaba’s Sentinel, compares it with Netflix Hystrix, walks through its project structure and a flow‑control demo, and then demystifies its core architecture—including the SlotChain, entry processing, dashboard console, framework adapters, and rule persistence mechanisms.
Introduction
Sentinel is an open‑source lightweight high‑availability flow‑control component from Alibaba, designed for distributed service architectures. It provides flow control, circuit breaking, and system load protection.
Sentinel vs Hystrix
Key differences between Sentinel and Hystrix are summarized as follows:
Isolation strategy: Sentinel uses semaphore isolation, while Hystrix supports thread‑pool and semaphore isolation.
Circuit‑breaking strategy: Sentinel supports both response‑time and failure‑rate based rules; Hystrix only supports failure‑rate.
Real‑time metrics: Sentinel uses sliding windows; Hystrix uses RxJava‑based sliding windows.
Rule configuration: Both support multiple data sources.
Extension points: Sentinel provides many extension points; Hystrix offers plugin extensions.
Rate limiting: Sentinel supports QPS‑based limiting and call‑graph limiting; Hystrix does not.
Traffic shaping: Sentinel supports warm‑up and constant‑rate modes; Hystrix does not.
System load protection: Sentinel supports it; Hystrix does not.
Console: Sentinel ships with an out‑of‑the‑box dashboard; Hystrix console is limited.
Framework adapters: Sentinel integrates with Servlet, Spring Cloud, Dubbo, gRPC, etc.; Hystrix mainly targets Spring Cloud Netflix.
Project Structure
sentinel-core : core implementation of flow control, degradation, system protection.
sentinel-dashboard : visual console for rule management and metrics.
sentinel-transport : transport layer providing client‑server APIs.
sentinel-extension : extensions for data sources.
sentinel-adapter : adapters for common frameworks (Servlet, Dubbo, gRPC, etc.).
sentinel-demo : sample applications demonstrating flow control, degradation, etc.
sentinel-benchmark : benchmark suite for performance testing.
Running Example
The article presents a QPS‑based flow‑control demo. The demo defines a FlowRule that limits a resource named "abc" to 20 requests per second, starts a timer thread for statistics, and spawns multiple worker threads that invoke SphU.entry() in a loop.
public class FlowQpsDemo {
private static final String KEY = "abc";
private static final AtomicInteger pass = new AtomicInteger();
private static final AtomicInteger block = new AtomicInteger();
private static final AtomicInteger total = new AtomicInteger();
private static volatile boolean stop = false;
private static final int threadCount = 32;
private static final int seconds = 30;
public static void main(String[] args) throws Exception {
initFlowQpsRule();
tick();
simulateTraffic();
// print statistics ...
}
private static void initFlowQpsRule() {
List<FlowRule> rules = new ArrayList<>();
FlowRule rule1 = new FlowRule();
rule1.setResource(KEY);
rule1.setCount(20);
rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule1.setLimitApp("default");
rules.add(rule1);
FlowRuleManager.loadRules(rules);
}
// ... other helper methods omitted for brevity ...
}When the demo runs, the console shows the number of passed and blocked requests per second. With multiple threads, the pass count may temporarily exceed the limit due to lack of synchronization inside SphU.entry(). Reducing threadCount to 1 demonstrates the expected 20‑QPS limit.
Deep Dive into Core Implementation
Sentinel’s entry point is SphU.entry(). It creates an Entry object and delegates to Sph.entry(), which obtains a ProcessorSlotChain for the resource and invokes its entry() method.
SlotChain Creation
The chain is built by Env.slotsChainBuilder.build(). The default chain contains the following slots in order:
NodeSelectorSlot
ClusterBuilderSlot
LogSlot
StatisticSlot
SystemSlot
AuthoritySlot
FlowSlot
DegradeSlot
Each slot implements AbstractLinkedProcessorSlot, forming a linked‑list (responsibility chain). The first dummy node ( first) points to the real head; end points to the tail.
DefaultProcessorSlotChain
The entry() method of DefaultProcessorSlotChain simply calls first.transformEntry(...), which forwards the call to the next slot via fireEntry(). After a slot finishes its logic, it invokes next.transformEntry(), propagating the request through the chain.
NodeSelectorSlot
This slot builds a call‑tree of DefaultNode objects based on the current Context. It stores the node in the context and then triggers the next slot’s entry(). Subsequent slots use the collected statistics for decisions.
Other slots have specific responsibilities:
ClusterBuilderSlot : gathers runtime metrics (RT, QPS, thread count).
StatisticSlot : records statistical data.
FlowSlot : enforces flow‑control rules and may throw BlockException.
AuthoritySlot : applies black‑list/white‑list checks.
DegradeSlot : performs circuit‑breaking based on degradation rules.
SystemSlot : protects the system based on load, CPU, etc.
Dashboard Console
The sentinel-dashboard is a Spring Boot application that provides real‑time monitoring, rule management, and machine discovery. Clients send heartbeat HTTP requests to /machine/heartbeat; the dashboard stores client info in memory and displays metrics per machine.
Client Integration
To connect an application to the dashboard, add the sentinel-transport-simple-http dependency and start the JVM with the option -Dcsp.sentinel.dashboard.server=host:port. On the first request, Sentinel initializes, registers the client, and begins sending heartbeats.
Framework Adapters
Sentinel integrates with popular frameworks via adapters. The Servlet adapter implements a Filter that performs the following steps:
Derive a resource name from the incoming HttpServletRequest.
Enter the Sentinel context with ContextUtil.enter(resource).
Call SphU.entry(resource, EntryType.IN) to acquire the entry.
If the entry is obtained, continue the filter chain; otherwise catch BlockException and invoke WebCallbackManager.getUrlBlockHandler().blocked() to return a block page.
Finally, exit the entry and the context.
Similar adapters exist for Dubbo, Spring Cloud, gRPC, RocketMQ, etc.
Rule Persistence
Rules can be loaded at runtime via the manager APIs:
FlowRuleManager.loadRules(List<FlowRule> rules) DegradeRuleManager.loadRules(List<DegradeRule> rules) SystemRuleManager.loadRules(List<SystemRule> rules)For durable storage, Sentinel defines a DataSource<T> interface. Implementations can pull rules from files, databases, or configuration centers (pull model) or receive push notifications from services such as Nacos or Zookeeper. The dashboard can push updated rules to a centralized rule store, and clients listen for changes via a custom DataSource.
Conclusion
Sentinel’s architecture is built on a chain of slots, each responsible for a specific aspect of traffic management. The modular design enables seamless integration with a visual dashboard, various application frameworks, and external rule stores, providing a powerful solution for flow control, circuit breaking, and system protection in distributed Java back‑end services.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
