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
Please enter at least three characters to search
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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • STRIDE: A Guide to Threat Modeling and Secure Implementation
  • Scaling in Practice: Caching and Rate-Limiting With Redis and Next.js
  • Why Rate Limiting Matters in Istio and How to Implement It
  • AI-Driven RAG Systems: Practical Implementation With LangChain

Trending

  • Unlocking AI Coding Assistants Part 3: Generating Diagrams, Open API Specs, And Test Data
  • Integrating Security as Code: A Necessity for DevSecOps
  • Event-Driven Architectures: Designing Scalable and Resilient Cloud Solutions
  • Unlocking the Potential of Apache Iceberg: A Comprehensive Analysis

Rate Limiter Internals in Resilience4j

In this article, we look at how this open source framework can help you boost the performance of your web application by making rate limiting easier.

By 
Bohdan Storozhuk user avatar
Bohdan Storozhuk
·
Nov. 10, 17 · Tutorial
Likes (9)
Comment
Save
Tweet
Share
15.2K Views

Join the DZone community and get the full member experience.

Join For Free

This is the third article of a short series about the Resilience4j library. If you are not familiar with the library itself, please read this introduction article first. There is also a similar write-up about circuit breaker implementation.

What Is Rate Limiting and When Is it Useful?

So, let's talk about another core component of the Resilience4j called the Rate Limiter. Unlike circuit breaking, the impact of rate limiting on your application resilience is not so obvious and comes into the game only after a certain scale. If you've ever worked with APIs for some huge products you know that they have rate limiting applied to almost any operation. Examples: Facebook, Twitter, Google Analytics...

But it doesn't mean that this pattern can be useful only for giants. In reality, it's the opposite - the smaller production environment you have, the more you should think about incoming traffic and your ability to handle possible request spikes. In practice, there are also situations when you're integrating with some legacy API and it doesn't have any rate limiters applied, so you can unintentionally perform a DoS attack on your partners. In such cases, you should of course tune this API usage and enforce rate limits on your side.

In general, rate limiting is an imperative technique to prepare your API for scale and establish high availability and reliability of your service. But also, this technique comes with a whole bunch of different options of how to handle a detected limits surplus, or what type of requests you want to limit. You can simply decline this over limit request, or build a queue to execute them later or combine these two approaches in some way. You can also limit concurrent requests by allowing only a certain amount of parallel executions (Resilience4j has a separate component for such limits, called Bulkhead). Another option would be to categorize all requests by priority and apply different limits for each category. 


Image title

Impact of queueing rate limiter on application throughput.

Rate Limiter Implementations

Resilience4j has a simple interface called RateLimiter (obviously) and the main method within it is

boolean getPermission(java.time.Duration timeoutDuration);

where timeoutDuration is a period you're ready to wait for permission if it's not there yet. This method returns true if a permission was acquired and false if while waiting timeoutDuration elapsed before a permit was acquired.

Currently, we have de-facto standard rate limiting algorithm called "token-bucket," but it comes with different variations and in Resilience4j you can find two implementations: SemaphoreBasedRateLimiter and AtomicRateLimiter.

Semaphore-Based Rate Limiter

Let's talk about SemaphoreBasedRateLimiter first, because it is actually the simplest and most performant implementation at the same time.

It's based on the simple idea that we can have one java.util.concurrent.Semaphore to store current permissions and all user threads will call semaphore.tryAcquire method, while we will have an additional internal thread and it will call semaphore.release when new limitRefreshPeriod starts.

Image title

Permission acquire and waiting sequence diagram

This simple approach is actually really performant because user threads have very small hot paths and don't care about any other required calculations; all permission refreshment is handled by an internal thread. So, my relatively small laptop can perform up to 30 permission checks within a microsecond. In the end, this implementation has only one drawback: you should have this internal thread in place and it looks like a bit of overhead. We know about that issue and provide our users with some ways to share this management thread with a whole bunch of limiters, so overall overhead can be really low. We also have another implementation called AtomicRateLimiter and it has no need of such management threads because all permission recalculation logic is executed by user threads themselves.

Atomic Rate Limiter

This rate limiter has a bit of tricky logic, and a sequence diagram for it would be hard to understand, so I'll just describe the main ideas behind it:

  1. We can virtually split time into equal pieces called cycles. In any time, we can determine the current cycle by calculating currentTime/cyclePeriod.
  2. If we know the current cycle number and cycle when the limiter was in use last, we can actually calculate how many permissions should've appeared in the limiter.
  3. After this calculation, if available permissions aren't enough, we can perform permission reservation, by decreasing current permissions and calculating the time we should wait for it to appear.
  4. After all calculations, we can produce a new limiter state and store it in AtomicReference, to propagate these changes across all user threads.

Of course, real implementation has some nuances to handle corner cases, deal with thread interrupts, maintain consistent limiter throughput under contention, etc. After all, this implementation is also very performant and much easier to configure. On my laptop, this limiter is capable to maintain 10 limiter checks per microsecond even under big contention. This is more than enough for almost any load you have, that's why this limiter is our default implementation. But if you really need to check permissions more than 2 million times per second, then I think it will be more beneficial to spend some time and configure semaphore based limiter for this high load operation.

rate limit Implementation

Opinions expressed by DZone contributors are their own.

Related

  • STRIDE: A Guide to Threat Modeling and Secure Implementation
  • Scaling in Practice: Caching and Rate-Limiting With Redis and Next.js
  • Why Rate Limiting Matters in Istio and How to Implement It
  • AI-Driven RAG Systems: Practical Implementation With LangChain

Partner Resources

×

Comments
Oops! Something Went Wrong

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
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!