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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
  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.

Roberto Picanco user avatar by
Roberto Picanco
·
Mar. 12, 17 · Tutorial
Like (5)
Save
Tweet
Share
28.73K 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 (agency) Forth (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Role of Data Governance in Data Strategy: Part II
  • Beginners’ Guide to Run a Linux Server Securely
  • Educating the Next Generation of Cloud Engineers With Google Cloud
  • How To Convert HTML to PNG in Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: