DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Boosting Application Performance With MicroStream and Redis Integration
  • Redis-Based Tomcat Session Management
  • Rethinking Java CRUDs With Event Sourcing and CQRS Patterns
  • Detecting Bugs and Vulnerabilities in Java With SonarQube

Trending

  • The Hidden Bottlenecks That Break Microservices in Production
  • Context-Aware Authorization for AI Agents
  • How Retry Storms Crash API-Led Systems: Bounded Reliability Patterns for Distributed Architectures
  • Building a Production-Ready AI Agent in 2026: Beyond the Hello World Demo
  1. DZone
  2. Coding
  3. Java
  4. A Redis-Based RateLimiter for Java

A Redis-Based RateLimiter for Java

Let's discuss how you can use RateLimiter with Redis and Java.

By 
Nikita Koksharov user avatar
Nikita Koksharov
·
Updated Nov. 18, 19 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
23.2K Views

Join the DZone community and get the full member experience.

Join For Free

Java RateLimiter in Redis

Let's discuss how you can use RateLimiter with Redis and Java.

RateLimiter 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

[DZone Refcard] Getting Started With Redis 

Java-Distributed Caching in Redis

Java (programming language) Redis (company)

Opinions expressed by DZone contributors are their own.

Related

  • Boosting Application Performance With MicroStream and Redis Integration
  • Redis-Based Tomcat Session Management
  • Rethinking Java CRUDs With Event Sourcing and CQRS Patterns
  • Detecting Bugs and Vulnerabilities in Java With SonarQube

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook