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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

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

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

Related

  • Identify API Policies for All Levels in the Application Network With API-Led Connectivity
  • Understanding the Fan-Out/Fan-In API Integration Pattern
  • Implementing DDoS With MuleSoft: Demonstration With Apache JMeter
  • Protecting Go Applications: Limiting the Number of Requests and Memory Consumption

Trending

  • Fraud Detection Using Artificial Intelligence and Machine Learning
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • Testing SingleStore's MCP Server
  • Debugging With Confidence in the Age of Observability-First Systems
  1. DZone
  2. Data Engineering
  3. Databases
  4. A Simple Way to Implement Rate Limiting

A Simple Way to Implement Rate Limiting

This simple solution could meet most of the enterprises’ applications or APIs needs. Enterprises need to implement rate limiting in their applications.

By 
Roberto Picanco user avatar
Roberto Picanco
·
Mar. 12, 17 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
30.3K Views

Join the DZone community and get the full member experience.

Join For Free

With the advent of APIs and its growth inside organizations, rate limiting has become an important functionality.

Let’s start this article with a question: Why do enterprises need to implement rate limiting in their applications?

Before we heard about API (Application Programming Interface) and before its growth inside organizations, one of the main reasons to implement rate limiting was to defend applications against DoS (Denial of Service) attacks. These applications could apply policies to limit traffic coming from specific sources, specific customers, IP addresses, and so forth.

Nowadays, with the advent of API and its increasing popularity, API management has emerged. Thereby, rate limiting has been replaced by throttling.

According to Wikipedia:

API management is the process of creating and publishing APIs, enforcing their usage policies, controlling access, nurturing the subscriber community, and collecting and analyzing usage statistics.

API management allows enterprises to monetize their APIs through throttling (rate limiting) by controlling API usage. For example, a specific customer has the right to access the API (its resources) only 100 times or requests per day. If the customer wants more accesses, he or she must pay more for that.

There are a lot of companies providing API management in the market. This API management usually has a throttling functionality embedded among others. The idea in this article is not to reinvent the wheel, but to show you a simple way to implement rate limiting (or, if you prefer, throttling) and to see how things work in the background.

To do so, we will use the following technologies:

  • Java.
  • Memcached (server and client).

The Example

Imagine that DZone releases an API that allows its customers to retrieve articles. They will register into the DZone API. However, the DZone API allows them to retrieve just five articles per day per customer, limiting their access.

Get the Memcached connection:

MemcachedClient memcacheClient = new MemcachedClient(new InetSocketAddress("localhost", 11211));

Get the counter information into Memcached. Set it if it is not stored yet:

// EXPIRED_SECONDS = 86400 (one day)
// ALLOW_ARTICLE = 5

Object fetchedObject = memcacheClient.get(customerLogin);
  if(fetchedObject == null){
      memcacheClient.set(customerLogin, EXPIRED_SECONDS, String.valueOf(ALLOW_ARTICLE -1));
      return "article";
  }

If the counter information is zero, throw an exception:

long counter = Long.parseLong((String) fetchedObject);
if(counter == 0){
   // Too many request exception
}

Decrement the counter information into Memcached:

// DECREMENT = 1
memcacheClient.decr(customerLogin, DECREMENT);
return “article”

Summary

Memcached has many commands — but it has one, called decr, that is the core of this solution. Probably, this simple solution could meet most of the enterprises’ applications or APIs needs. There are two points to observe. First, parallel requests in applications or APIs could grant exceeded access. Second, as Memcached is an in-memory key-value store, the counter information could be lost when the Memcached server restarts, and it could grant exceeded access, too. Depending on the business’s flexibility, some exceeded access is not a problem.

rate limit API application Memcached IT DZone Requests Release (computing) Forth (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Identify API Policies for All Levels in the Application Network With API-Led Connectivity
  • Understanding the Fan-Out/Fan-In API Integration Pattern
  • Implementing DDoS With MuleSoft: Demonstration With Apache JMeter
  • Protecting Go Applications: Limiting the Number of Requests and Memory Consumption

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

Let's be friends: