Kotlin
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Null Safety
-
Section 4: Object-Oriented Kotlin
-
Section 5: Functional Kotlin
-
Section 6: Coroutines
-
Section 7: Collections Deep Dive
-
Section 8: Type System Deep Dive
-
Section 9: Interop and Tooling
-
Section 10: Kotlin DSLs and Patterns
-
Section 11: Testing and Quality
-
Section 12: Server-Side Kotlin
-
Section 13: Practical Projects
-
Section 14: Interview Practice
-
Section 15: More Practice Exercises
-
Section 16: More Standard Library
-
Section 17: Multiplatform Kotlin
-
Section 18: kotlin.collections In Depth
-
Section 19: kotlin.text In Depth
-
Section 20: kotlin.ranges and kotlin.sequences
-
Section 21: kotlin.io and File Handling
-
Section 22: kotlinx.coroutines Deep Dive
-
Section 23: kotlin.reflect
-
Section 24: Android Development with Kotlin Overview
-
Section 25: Kotlin Multiplatform Deep Dive
-
Section 26: Kotlin for Backend Deep Dive
-
Section 27: Kotlin Design Patterns
-
Section 28: Advanced Language Features
-
Section 29: More Practice Exercises
-
Section 30: More Interview Practice
-
Section 31: Kotlin Type System Deep Dive
-
Section 32: Kotlin Null Safety Advanced
-
Section 33: Kotlin Testing Deep Dive
-
Section 34: Kotlin Build Tooling Deep Dive
-
Section 35: Kotlin Serialization
-
Section 36: Kotlin Performance Considerations
-
Section 37: Kotlin Native Overview
-
Section 38: Kotlin for Data and Scripting
-
Section 39: More Coroutines Practice
-
Section 40: More Android-Adjacent Patterns
-
Section 41: More Practical Projects
-
Section 42: More Design and Architecture Practice
-
Section 43: Kotlin Language Evolution
-
Section 44: More Interview and Review
-
Section 45: Kotlin Delegation Patterns Deep Dive
-
Section 46: Kotlin Annotations Deep Dive
-
Section 47: Kotlin for Gradle Plugin Development
-
Section 48: Kotlin Concurrency Beyond Coroutines
-
Section 49: Kotlin Compiler Internals
-
Section 50: Real-World Kotlin Case Studies
-
Section 51: Final Practice Projects
-
Section 52: Kotlin for Server-Side Reactive Programming
-
Section 53: More Practice and Drills
-
Section 54: Kotlin Security Practices
122: Rate Limiting and Caching in Ktor
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
RateLimitandHttpCacheplugins. - 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 aCache-Controlheader that allows this response to be cached for 60 seconds. - Ensure that the
/healthcheck route remains completely unrestricted and uncached.
There are no comments for now.