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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Boosting Application Performance With MicroStream and Redis Integration
  • Redis-Based Tomcat Session Management
  • Multi-Tenancy Implementation Using Spring Boot, MongoDB, and Redis
  • Learning Redis Basic Data Structure and Mapping With Java Data Structure

Trending

  • How Developers Are Driving Supply Chain Innovation With Modern Tech
  • Self-Supervised Learning Techniques
  • Building an AI Nutrition Coach With OpenAI, Gradio, and gTTS
  • Stabilizing ETL Pipelines With Airflow, Presto, and Metadata Contracts
  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
22.6K 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
  • Multi-Tenancy Implementation Using Spring Boot, MongoDB, and Redis
  • Learning Redis Basic Data Structure and Mapping With Java Data Structure

Partner Resources

×

Comments

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: