Skip to Content
Course content

122: Rate Limiting and Caching in Ktor

Click on the "Edit" button in the top corner of the screen to edit your slide content.

A few years ago, I was maintaining a public API for a client that provided real-time sports statistics. Everything was humming along perfectly until a third-party developer wrote a poorly optimized synchronization script. Instead of requesting updates every minute, their script entered a tight loop, hitting our /live-scores endpoint thousands of times per second. Our database CPU spiked to 100% in minutes, the connection pool exhausted, and the entire site went dark for every other user. I spent four hours manually blocking IP addresses in the firewall while the client breathed down my neck. That's the moment I realized that "trusting the client" is a dangerous architectural decision.

Stopping the Flood with RateLimit

In Ktor, we handle this using the RateLimit plugin. It’s essentially a gatekeeper that tracks how many requests a specific client has made within a time window. If they exceed that window, Ktor returns a 429 Too Many Requests response, protecting your backend from being overwhelmed. I usually recommend defining different limits for different "tiers" of users—for example, guests get a tight limit, while authenticated API keys get a more generous one.

install(RateLimit) {
    register(RateLimitName("public")) {
        rateLimiter(limit = 100, refillPeriod = 60.seconds)
    }
    register(RateLimitName("premium")) {
        rateLimiter(limit = 1000, refillPeriod = 60.seconds)
    }
}

routing {
    rateLimit(RateLimitName("public")) {
        get("/live-scores") {
            call.respondText("Here are the scores!")
        }
    }
}

Notice how I used RateLimitName. This is crucial because you don't want a global limit across your entire app. You might want your /health check to be wide open, but your expensive /generate-report endpoint to be strictly limited. By wrapping specific routes in the rateLimit block, you apply the constraint only where the risk is highest.

Reducing Load via HTTP Caching

Rate limiting stops the abuse, but caching stops the waste. If your sports score data only updates every 30 seconds, there is absolutely no reason to recalculate the response and query the database for every single request. This is where the HttpCache plugin comes in. It allows Ktor to respect and manage Cache-Control headers, telling the browser (or a CDN like Cloudflare) to keep a copy of the response for a set period.

I've seen developers try to build their own custom caching maps inside their services, but that's often an anti-pattern for HTTP APIs. It's better to leverage the standard HTTP protocol. When you set a max-age, you're offloading the work from your server to the edge of the network.

install(HttpCache)

routing {
    get("/league-standings") {
        // Tell the client and any intermediate proxies to cache this for 5 minutes
        call.response.cacheControl(CacheControl.MaxAge(300)) 
        val standings = database.getStandings()
        call.respond(standings)
    }
}

One thing to keep in mind: caching is a double-edged sword. If you cache the /user-profile endpoint for too long, your users will complain that their profile picture isn't updating. Only cache data that is "read-heavy" and "change-light." For our sports example, league standings are perfect for caching; a live game clock is not.




📋 Practical Task

Implement a Protected Crypto-Price API

You are building a mock cryptocurrency price API. Your goal is to prevent API abuse while ensuring the server doesn't work harder than it needs to. Complete the following implementation:

  • Install the RateLimit and HttpCache plugins.
  • Create a route /price/{coin}. This endpoint should be limited to 10 requests per minute per client to prevent scraping bots from hammering the API.
  • Create a route /market-summary. This endpoint returns a general overview of the market that only changes every few minutes. Implement a Cache-Control header that allows this response to be cached for 60 seconds.
  • Ensure that the /health check route remains completely unrestricted and uncached.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.