A Redis-Based RateLimiter for Java
Let's discuss how you can use RateLimiter with Redis and Java.
Join the DZone community and get the full member experience.
Join For FreeRateLimiter
is a Java class that helps regulate a program’s rate of execution. This could be highly useful for applications like Redis, an in-memory data structure store that can be used to build NoSQL databases.
However, Redis doesn’t include Java support out of the box. In this article, we’ll discuss how you can use RateLimiter
with Redis and Java.
You may also like: [DZone Refcard] Getting Started With Redis
What Is RateLimiter?
RateLimiter is a class from Guava, an open-source set of Java libraries mainly developed by engineers at Google.
Conceptually, a RateLimiter
is similar to a Semaphore, in that both classes seek to restrict access to a physical or logical resource. However, whereas the Semaphore class limits access to a certain number of concurrent users, the RateLimiter
class limits access to a certain number of requests per second.
An example of how to use RateLimiter
is below:
final RateLimiter rateLimiter = RateLimiter.create(2.0);
// rate is "2 permits per second"
void submitTasks(List<Runnable> tasks, Executor executor) {
for (Runnable task : tasks) {
rateLimiter.acquire(); // may wait
executor.execute(task);
}
When Should You Use RateLimiter?
The RateLimiter
class is used to throttle processing rates in a high-concurrency system in order to avoid errors and performance issues.
Possible RateLimiter
use cases include limiting the number of tasks executed per second, or capping a stream of data at a certain number of bytes per second.
RateLimiter for Java and Redis
As mentioned above, Redis does not include built-in compatibility with the Java programming language. However, Redis developers can gain access to Java classes and functionality, including RateLimiter
, through third-party Redis Java clients such as Redisson.
The RRateLimiter interface in Redisson reimplements the RateLimiter
class from Guava. Redis developers can use RRateLimiter
to limit the total rate of calls — either from all threads regardless of Redisson instance, or from all threads working with the same Redisson instance.
RRateLimiter
includes Async, Reactive, and RxJava2 interfaces. Note that RRateLimiter
does not guarantee fairness, i.e. threads are not necessarily guaranteed a chance to fairly execute.
An example of how to use RRateLimiter
is below:
RRateLimiter limiter = redisson.getRateLimiter("myLimiter");
// Initialization required only once.
// 5 permits per 2 seconds
limiter.trySetRate(RateType.OVERALL, 5, 2, RateIntervalUnit.SECONDS);
// acquire 3 permits, or block until they became available
limiter.acquire(3);
Happy coding!
Further Reading
Opinions expressed by DZone contributors are their own.
Comments