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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Identify API Policies for All Levels in the Application Network With API-Led Connectivity
  • Understanding the Fan-Out/Fan-In API Integration Pattern
  • Running a Self-Hosted WAF in Production: Practical Lessons From SafeLine
  • The Bill You Didn't See Coming

Trending

  • Giving AI Agents Safe Access to Legacy Systems: Introducing legacy2mcp
  • PWA on iOS, Part 2: HTTPS, Manifest and Service Worker
  • Fix the Target, Precompute Once: A Backend-Free Word-Ladder Solver With a BFS Distance Field
  • Integrating Apache Spark With Drools: A Loan Approval Demo

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Mar. 12, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
30.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
  • Running a Self-Hosted WAF in Production: Practical Lessons From SafeLine
  • The Bill You Didn't See Coming

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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