Skip to content

vaishnkv/Throttle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Throttle

This project implements fixed-window rate limiters using Redis to manage request limits for users based on their subscription type. It provides three variants with varying caching mechanisms:

  1. Vanilla Rate Limiter: No caching; ensures consistent data by always querying Redis.
  2. Periodic Pooling + RAM Caching Rate Limiter: Combines lazy caching with periodic synchronization from Redis.
  3. Subscription + RAM Caching Rate Limiter: Uses Redis Pub/Sub to proactively update local caches in real-time.

Assumptions

  • The objective is to rate limit a service with one operational endpoint.
  • User types can be one of the following: normal_user, premium_user, or admin.
  • Max allowed requests for user types are not changed frequently, resulting in reasonably low configuration load.
  • Any user can make a minimum of 0 requests.
  • The service is under reasonably high load, with multiple replicas of the server behind a load balancer.
    • This necessitates the use of a centrally accessible database (Redis) for maintaining the Request_count instead of storing it as a Python dictionary in memory.

Configuration Store: Redis

Schemas

  1. User_Requestcount_map

    • Tracks the live number of requests received from each user.
    • Each key (user ID) has an expiry of 1 minute.
  2. User_Usertype_map

    • Maps each user ID to their corresponding user type (e.g., normal_user, premium_user, admin).
  3. Usertype_Maxnumrequests_map

    • Maps each user type to its respective maximum allowed requests.

Flask Server

  • A dummy Flask-based HTTP server is implemented with one endpoint:
    • Endpoint: /submit_job
      Decorated with a Rate_limiter_check function, which implements the rate limiter before executing the core logic.

Features

  • Rate limiting using a fixed-window approach.
  • Redis-backed live request tracking with a flexible schema.
  • Modular implementation of rate limiter variants:
    1. Vanilla Rate Limiter: No caching, strict consistency.
    2. Periodic Pooling + RAM Caching: Lazy caching with periodic syncs.
    3. Subscription + RAM Caching: Real-time updates using Redis Pub/Sub.

About

A lightweight and modular rate-limiting system built with Flask and Redis. Throttle ensures fair API usage by implementing a fixed-window rate-limiting mechanism. Perfect for managing user quotas and preventing abuse, with easy-to-use decorators and scalable design.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages