← Back to articles

Alibaba Sentinel: Achieving High-Throughput Statistics with LeapArray and LongAdder

Under the Hood of Sentinel: Achieving High-Throughput Statistics with LeapArray and LongAdder

In distributed systems, traffic governance is the ultimate “safety valve.” At Beike Zhaofang, we integrated Alibaba Sentinel into our standard Spring Boot framework to protect microservices from cascading failures.

The most critical challenge for any flow-control middleware is performance: how do you count millions of requests per second without becoming the bottleneck itself? Today, we dive into the core of Sentinel’s StatisticSlot, exploring the “Logic Zero-Copy” of LeapArray and the lock-free magic of LongAdder.


1. The Statistical Engine: StatisticSlot

Within Sentinel’s ProcessorSlotChain, the StatisticSlot is the “heart.” Every request must pass through this slot to update real-time metrics (QPS, Thread Count, RT).

To handle massive concurrency, Sentinel avoids global locks. Instead, it leverages a sophisticated data structure: the LeapArray.


2. LeapArray: Sliding Windows and Logic Zero-Copy

Traditional counters often struggle with “window jumping” (resetting every second), which leads to inaccurate spikes. Sentinel uses a Sliding Window algorithm implemented via LeapArray.

The “Circular Buffer” Efficiency

LeapArray pre-allocates an array of WindowWrap objects. When time progresses:

  1. Index Calculation: The current timestamp is mapped to an array index using (timestamp / windowLength) % arraySize.
  2. Logic Zero-Copy: If a window is expired, Sentinel doesn’t destroy the object. It uses a ReentrantLock (specifically a Non-Fair Lock for throughput) to reset the window’s start time and clear its counter.
  3. Performance: This reuse minimizes GC pressure and avoids constant memory allocation, effectively achieving “Logic Zero-Copy.”

3. Breaking the Write Bottleneck: LongAdder & CPU Cache Lines

Flow control is a Write-Intensive operation. Every request requires an atomic increment. In high-concurrency environments, a standard AtomicLong becomes a nightmare due to the MESI Protocol.

The MESI Problem & Cache Line Bouncing

When multiple CPU cores attempt to update the same AtomicLong (the same Cache Line), the hardware must synchronize the state across cores. Every successful write by one core invalidates the cache of all other cores. This results in massive Snoop Traffic and CPU “spinning,” drastically increasing Latency (RT).

The LongAdder Solution

Sentinel utilizes LongAdder (based on Striped64) to solve this:

  • Cell Sharding: It distributes the counter across an array of Cell objects. Each thread hashes to a specific cell, reducing contention.
  • False Sharing Protection: Cells are annotated with @Contended, adding padding to ensure each Cell occupies its own 64-byte Cache Line.
  • Trade-off: It sacrifices “Read” performance (summing the cells is O(n)O(n)) to maximize “Write” throughput. For a flow-control system, where we write 100k times but read only once per second to check rules, this is the perfect trade-off.

4. The Engineering Philosophy: Precision vs. Performance

A common question arises: Is LongAdder.sum() accurate?

In the world of system protection, the answer is: It doesn’t have to be. Sentinel’s philosophy is to act as a Protection Device, not a Billing System. If the threshold is 1000 and the system allows 1001 due to a micro-delay in LongAdder visibility, the system won’t collapse. By embracing Eventual Consistency, Sentinel maintains microsecond-level overhead even under extreme stress.


5. Summary & Key Takeaways

When we architected the platform at Beike, our choice to use Sentinel was driven by these low-level optimizations:

  • LeapArray handles the “time” dimension through memory reuse.
  • LongAdder handles the “concurrency” dimension through hardware-aware sharding.
  • Non-Fair Locks in the updateLock ensure that the rare window-reset events don’t stall the processing pipeline.

Understanding these primitives allows us to build systems that aren’t just functional, but are mechanically sympathetic to the underlying hardware.


About the Author

Senior Platform Engineer with 10 years of experience in infrastructure and cloud-native application architecture.