Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 380285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:58:27+00:00 2026-05-12T14:58:27+00:00

The legitimate users of my site occasionally hammer the server with API requests that

  • 0

The legitimate users of my site occasionally hammer the server with API requests that cause undesirable results. I want to institute a limit of no more than say one API call every 5 seconds or n calls per minute (haven’t figured out the exact limit yet). I could obviously log every API call in a DB and do the calculation on every request to see if they’re over the limit, but all this extra overhead on EVERY request would be defeating the purpose. What are other less resource-intensive methods I could use to institute a limit? I’m using PHP/Apache/Linux, for what it’s worth.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:58:28+00:00Added an answer on May 12, 2026 at 2:58 pm

    Ok, there’s no way to do what I asked without any writes to the server, but I can at least eliminate logging every single request. One way is by using the “leaky bucket” throttling method, where it only keeps track of the last request ($last_api_request) and a ratio of the number of requests/limit for the time frame ($minute_throttle). The leaky bucket never resets its counter (unlike the Twitter API’s throttle which resets every hour), but if the bucket becomes full (user reached the limit), they must wait n seconds for the bucket to empty a little before they can make another request. In other words it’s like a rolling limit: if there are previous requests within the time frame, they are slowly leaking out of the bucket; it only restricts you if you fill the bucket.

    This code snippet will calculate a new $minute_throttle value on every request. I specified the minute in $minute_throttle because you can add throttles for any time period, such as hourly, daily, etc… although more than one will quickly start to make it confusing for the users.

    $minute = 60;
    $minute_limit = 100; # users are limited to 100 requests/minute
    $last_api_request = $this->get_last_api_request(); # get from the DB; in epoch seconds
    $last_api_diff = time() - $last_api_request; # in seconds
    $minute_throttle = $this->get_throttle_minute(); # get from the DB
    if ( is_null( $minute_limit ) ) {
        $new_minute_throttle = 0;
    } else {
        $new_minute_throttle = $minute_throttle - $last_api_diff;
        $new_minute_throttle = $new_minute_throttle < 0 ? 0 : $new_minute_throttle;
        $new_minute_throttle += $minute / $minute_limit;
        $minute_hits_remaining = floor( ( $minute - $new_minute_throttle ) * $minute_limit / $minute  );
        # can output this value with the request if desired:
        $minute_hits_remaining = $minute_hits_remaining >= 0 ? $minute_hits_remaining : 0;
    }
    
    if ( $new_minute_throttle > $minute ) {
        $wait = ceil( $new_minute_throttle - $minute );
        usleep( 250000 );
        throw new My_Exception ( 'The one-minute API limit of ' . $minute_limit 
            . ' requests has been exceeded. Please wait ' . $wait . ' seconds before attempting again.' );
    }
    # Save the values back to the database.
    $this->save_last_api_request( time() );
    $this->save_throttle_minute( $new_minute_throttle );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're currently developing a site that uses a simple JSON API (RoR) to populate
For a very small number of users (who are making legitimate requests) on my
In a .NET program that's written to follow declarative style, what are some legitimate
I'm coding a new website that will need users to enter their mobile phone
I have a site that allow the user to request a secret report in
I am trying to install openID into my web site project that is using
I am designing a web site in which users solve puzzles as quickly as
I am trying to address session fixation/hijacking/sidejacking on an ATG/JBoss/Tomcat site. It seems that
We are working on a project that requires us to allow users to register
I have legitimate reasons to do what I am trying to explain. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.