Master Distributed Caching, Rate Limiting, Locks & Idempotency with Alibaba Tools
This article introduces Alibaba's distributed‑tools component, explaining how to configure Redis/Tair cache, implement fixed‑window rate limiting, use a high‑performance distributed lock template, and apply idempotency mechanisms with annotations and templates to ensure reliable microservice operations.
Cache
Cache is ubiquitous in modern applications, from browser to reverse proxy, web server, application, and database layers, providing a "space‑for‑time" trade‑off to avoid costly operations such as database queries or repeated calculations.
redis.extend.hostName=127.0.0.1
redis.extend.port=6379
redis.extend.password=pwdcode
redis.extend.timeout=10000
redis.idempotent.enabled=trueThe distributed‑tools library offers a CacheEngine interface with Tair and Redis implementations.
public String get(String key);
/**
* Get object by key, return null on exception
*/
public <T> T get(String key, Class<T> clz);
/**
* Store value without expiration
*/
public <T extends Serializable> boolean put(String key, T value);
/**
* Store value with expiration
*/
public <T extends Serializable> boolean put(String key, T value, int expiredTime, TimeUnit unit);
/**
* Delete cache by key
*/
public boolean invalid(String key);The get method queries a key, put stores data, and invalid removes entries.
Rate Limiting
In high‑concurrency scenarios such as flash sales, rate limiting protects system stability by throttling requests, rejecting excess traffic, queuing, or degrading service.
/**
* Increment counter with expiration (fixed window)
*/
public long incrCount(String key, int expireTime, TimeUnit unit);
/**
* Fixed‑window rate limit; returns true when threshold exceeded
*/
public boolean rateLimit(String key, int rateThreshold, int expireTime, TimeUnit unit);Using CacheEngine.rateLimit implements fixed‑window limiting; sliding windows are not supported.
The RateLimitTemplate simplifies usage via an execute method.
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface RateLimit {
/** Limiting key */
String limitKey();
/** Allowed count, default MAX_VALUE */
long limitCount() default Long.MAX_VALUE;
/** Time window */
long timeRange();
/** Block duration */
long blockDuration();
/** Time unit, default seconds */
TimeUnit timeUnit() default TimeUnit.SECONDS;
} @RateLimit(limitKey = "#key", limitCount = 5, timeRange = 2, blockDuration = 3, timeUnit = TimeUnit.MINUTES)
public String testLimit2(String key) {
// ...
return key;
}Distributed Lock
Distributed locks extend the concept of local mutexes to multiple processes, typically using shared memory stores like Redis or Tair.
Mutual exclusion across nodes
Re‑entrancy within the same thread
Lock timeout with heartbeat refresh
High performance and availability
Supports lock and tryLock (including timed tryLock)
Fair lock not supported
/**
* Distributed lock execution template
*/
public static <T> T execute(String lockKey, Supplier<T> resultSupplier, long waitTime, TimeUnit unit, ErrCodeEnum errCodeEnum) {
AssertUtils.assertTrue(StringUtils.isNotBlank(lockKey), ExceptionEnumType.PARAMETER_ILLEGALL);
boolean locked = false;
Lock lock = DistributedReentrantLock.newLock(lockKey);
try {
locked = waitTime > 0 ? lock.tryLock(waitTime, unit) : lock.tryLock();
} catch (InterruptedException e) {
throw new RuntimeException(String.format("lock error,lockResource:%s", lockKey), e);
}
if (errCodeEnum != null) {
AssertUtils.assertTrue(locked, errCodeEnum);
} else {
AssertUtils.assertTrue(locked, ExceptionEnumType.ACQUIRE_LOCK_FAIL);
}
try {
return resultSupplier.get();
} finally {
lock.unlock();
}
}Idempotency
Idempotency ensures that repeated invocations of an operation produce the same result, which is crucial for payment, order creation, message sending, and other retry‑prone scenarios.
Two annotations— IdempotentTxId and IdempotentTxIdGetter —extract a unique idempotent key from method parameters or return values.
@Idempotent(spelKey = "#request.requestId", firstLevelExpireDate = 7, secondLevelExpireDate = 30)
public void execute(BizFlowRequest request) {
// ...
}The component relies on the distributed‑lock service to guarantee exclusive execution.
/**
* Idempotent template processor
*/
public R execute(IdempotentRequest<P> request, Supplier<R> executeSupplier,
Consumer<IdempotentResult<P, R>> resultPreprocessConsumer,
Predicate<R> ifResultNeedIdempotence) {
// implementation omitted
}First‑level storage (e.g., Tair MDB) provides high performance; second‑level storage (e.g., LDB or TableStore) ensures reliability, with parallel reads returning the fastest result and parallel writes improving throughput.
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.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
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.
