Rate Limiting Code Quiz

Rate Limiting Code Quiz

animation
rest
protocols
WarningCommon Mistake: Ignoring API Rate Limiting on IoT Devices

The Error: Not implementing rate limits on device APIs, assuming “our devices are well-behaved.” A firmware bug causes 1,000 devices to poll every 100 ms instead of every 5 minutes.

Real Impact:

Normal traffic: 1,000 devices × 12 requests/hour = 12K req/hour
Bug traffic: 1,000 devices × 36,000 requests/hour = 36M req/hour (3,000× increase)

AWS API Gateway cost:
  12K req/hour: $0.04/hour = $29/month (normal)
  36M req/hour: $120/hour = $86,400/month (bug)

Damage: $86K bill + service outage for ALL devices (API throttled)

The Fix: Implement per-device rate limiting:

# Return 429 Too Many Requests after 20 requests/minute
@app.route('/api/v1/temperature')
@rate_limit(max_requests=20, window=60)  # 20 per minute per device
def get_temperature():
    return jsonify({"temp": 22.5})

Result: Bug triggers rate limit, affects only buggy devices. Bill capped at $150/month. Service continues for well-behaved devices.

Back to the chapter