Skip to main content
The Adaptive Rate Limiter is highly configurable to suit various deployment scenarios and API requirements. Configuration is handled through two separate classes: RateLimiterConfig and StateConfig.

Architecture Overview

Important: RateLimiterConfig and StateConfig are separate configuration objects, not nested. They are passed to different components:
  • RateLimiterConfigScheduler
  • StateConfigStateManager
from adaptive_rate_limiter.scheduler import (
    RateLimiterConfig,
    StateConfig,
    SchedulerMode,
    CachePolicy,
)

RateLimiterConfig

The RateLimiterConfig class controls the core behavior of the scheduler. It has 24 configurable fields.
from adaptive_rate_limiter.scheduler import RateLimiterConfig, SchedulerMode

config = RateLimiterConfig(
    mode=SchedulerMode.INTELLIGENT,
    max_concurrent_executions=100,
    max_queue_size=1000,
    overflow_policy="reject",
    enable_rate_limiting=True,
    rate_limit_buffer_ratio=0.9,
)

Core Scheduling

OptionTypeDefaultDescription
modeSchedulerModeINTELLIGENTOperation mode: BASIC, INTELLIGENT, or ACCOUNT.
max_concurrent_executionsint100Maximum number of concurrent executions allowed.
max_queue_sizeint1000Maximum size of each bucket’s request queue.
overflow_policystr"reject"Policy when queue is full: "reject" or "drop_oldest".
scheduler_intervalfloat0.01Interval between scheduler loops in seconds.

Request Processing

OptionTypeDefaultDescription
request_timeoutfloat30.0Request timeout duration in seconds.
enable_priority_schedulingboolTrueEnable priority-based request scheduling.

Rate Limiting Integration

OptionTypeDefaultDescription
enable_rate_limitingboolTrueEnable rate limiting enforcement.
rate_limit_buffer_ratiofloat0.9Ratio of rate limit to use as buffer (0.9 = use 90% of limit).

Failure Handling

OptionTypeDefaultDescription
failure_windowfloat30.0Time window for failure counting in seconds.
max_failuresint20Maximum failures within window before circuit break.
backoff_basefloat2.0Base for exponential backoff calculation.
max_backofffloat60.0Maximum backoff time in seconds.

Graceful Degradation

OptionTypeDefaultDescription
enable_graceful_degradationboolTrueEnable graceful degradation on failures.
health_check_intervalfloat30.0Interval between health checks in seconds.
max_consecutive_failuresint3Maximum consecutive failures before degradation.
conservative_multiplierfloat0.6Multiplier for conservative capacity during degradation.

Metrics and Monitoring

OptionTypeDefaultDescription
metrics_enabledboolTrueEnable metrics collection.
prometheus_hoststr"0.0.0.0"Prometheus metrics host.
prometheus_portint9090Prometheus metrics port.
metrics_export_intervalfloat60.0Interval for metrics export in seconds.
enable_performance_trackingboolTrueEnable detailed performance tracking.

State Management

OptionTypeDefaultDescription
enable_state_persistenceboolTrueEnable state persistence to backend.

Testing Support

OptionTypeDefaultDescription
test_modeboolFalseEnable test mode with relaxed constraints.
test_rate_multiplierfloat1.0Rate limit multiplier for testing.

RateLimiterConfig Validation Rules

The following validation rules are enforced when creating a RateLimiterConfig:
FieldConstraint
rate_limit_buffer_ratioMust be 0 < value <= 1.0
conservative_multiplierMust be 0 < value <= 1.0
max_concurrent_executionsMust be >= 1
max_queue_sizeMust be >= 1
overflow_policyMust be "reject" or "drop_oldest"
# Valid configuration
config = RateLimiterConfig(
    rate_limit_buffer_ratio=0.85,  # OK: within (0, 1.0]
    conservative_multiplier=0.5,    # OK: within (0, 1.0]
    max_concurrent_executions=50,   # OK: >= 1
    overflow_policy="drop_oldest",  # OK: valid option
)

# Invalid configuration (will raise ValidationError)
config = RateLimiterConfig(
    rate_limit_buffer_ratio=1.5,    # ERROR: > 1.0
)

StateConfig

The StateConfig class controls advanced state management, caching, and persistence. It has 25 configurable fields and is passed separately to the StateManager.
from adaptive_rate_limiter.scheduler import StateConfig, CachePolicy

state_config = StateConfig(
    cache_policy=CachePolicy.WRITE_THROUGH,
    cache_ttl=1.0,
    max_cache_size=1000,
    is_production=True,
    namespace="my-tenant",
)

Cache Configuration

OptionTypeDefaultDescription
cache_ttlfloat1.0Cache TTL in seconds.
max_cache_sizeOptional[int]1000Maximum number of cache entries before LRU eviction.
cache_policyCachePolicyWRITE_THROUGHCache write policy: WRITE_THROUGH, WRITE_BACK, or WRITE_AROUND.
warn_write_back_productionboolTrueEmit warning if using WRITE_BACK in production.
is_productionboolFalseFlag indicating production environment.
acknowledge_write_back_riskboolFalseExplicitly acknowledge WRITE_BACK risk in production. Required when is_production=True and cache_policy=WRITE_BACK.
Production Safety Warning: When using CachePolicy.WRITE_BACK in production, you MUST set acknowledge_write_back_risk=True. This ensures you understand that WRITE_BACK may lose data if the process crashes before the cache is flushed. Failure to set this field will raise a ValueError at startup.

Batch Operations

OptionTypeDefaultDescription
batch_sizeint50Number of items to process in batch operations.
batch_timeoutfloat0.1Timeout for batch operations in seconds.

Flush Retry Settings

OptionTypeDefaultDescription
flush_max_retriesint5Maximum retry attempts for cache flush before dropping updates.
flush_backoff_basefloat1.0Base delay in seconds for exponential backoff between flush retries.
flush_backoff_maxfloat60.0Maximum delay in seconds for exponential backoff between flush retries.

Cleanup and Maintenance

OptionTypeDefaultDescription
cleanup_intervalfloat30.0Interval in seconds between cleanup cycles.
enable_background_cleanupboolTrueEnable automatic background cleanup of expired entries.
cleanup_task_cancel_timeoutfloat2.0Timeout in seconds for canceling cleanup tasks during shutdown.
cleanup_task_wait_timeoutfloat1.0Timeout in seconds for waiting on cleanup task completion.

State Persistence

OptionTypeDefaultDescription
state_ttlint3600Time-to-live for state entries in backend storage (seconds).

Reservation Cleanup

OptionTypeDefaultDescription
reservation_cleanup_intervalfloat3600.0Interval in seconds between reservation cleanup cycles.
reservation_ttlfloat300.0Time-to-live for reservation entries in seconds.

Account State

OptionTypeDefaultDescription
account_state_ttlfloat86400.0Time-to-live for account state entries in seconds (24 hours).
account_state_max_sizeOptional[int]10000Maximum number of account state entries before eviction.

Versioning and Recovery

OptionTypeDefaultDescription
enable_versioningboolTrueEnable state versioning for recovery and rollback.
max_versionsint10Maximum number of versions to keep per state entry.

Concurrency Control

OptionTypeDefaultDescription
lock_free_readsboolTrueEnable lock-free read operations for better performance.
max_concurrent_operationsint100Maximum number of concurrent state operations allowed.

Namespace Isolation

OptionTypeDefaultDescription
namespacestr"default"State namespace for multi-tenant isolation.

StateConfig Validation Rules

The following validation rules are enforced when creating a StateConfig:
FieldConstraint
cache_ttlMust be positive (> 0)
max_cache_sizeMust be at least 1 (or None for unlimited)
batch_sizeMust be at least 1
state_ttlMust be at least 60 seconds
# Valid configuration
state_config = StateConfig(
    cache_ttl=0.5,         # OK: positive value
    max_cache_size=500,    # OK: >= 1
    batch_size=25,         # OK: >= 1
    state_ttl=300,         # OK: >= 60
)

# Invalid configurations (will raise ValueError)
StateConfig(cache_ttl=-1.0)    # ERROR: must be positive
StateConfig(max_cache_size=0)  # ERROR: must be at least 1
StateConfig(batch_size=0)      # ERROR: must be at least 1
StateConfig(state_ttl=30)      # ERROR: must be at least 60 seconds

Enums

SchedulerMode

Controls the scheduling strategy used by the rate limiter.
from adaptive_rate_limiter.scheduler import SchedulerMode

# Available modes:
SchedulerMode.BASIC        # Simple rate limiting without advanced features
SchedulerMode.INTELLIGENT  # Adaptive rate limiting with learning capabilities
SchedulerMode.ACCOUNT      # Account-aware rate limiting for multi-tenant scenarios

CachePolicy

Controls how state changes are synchronized with the backend.
from adaptive_rate_limiter.scheduler import CachePolicy

# Available policies:
CachePolicy.WRITE_THROUGH  # Writes immediately to backend (safest, highest latency)
CachePolicy.WRITE_BACK     # Writes asynchronously in batches (fastest, risk of data loss)
CachePolicy.WRITE_AROUND   # Writes bypass cache, reads may be stale

Complete Example

from adaptive_rate_limiter.scheduler import (
    RateLimiterConfig,
    StateConfig,
    SchedulerMode,
    CachePolicy,
)

# Configure the rate limiter (goes to Scheduler)
rate_limiter_config = RateLimiterConfig(
    mode=SchedulerMode.INTELLIGENT,
    max_concurrent_executions=50,
    max_queue_size=500,
    overflow_policy="reject",
    enable_rate_limiting=True,
    rate_limit_buffer_ratio=0.85,
    failure_window=30.0,
    max_failures=10,
    enable_graceful_degradation=True,
    metrics_enabled=True,
    prometheus_port=9090,
)

# Configure state management (goes to StateManager)
state_config = StateConfig(
    cache_policy=CachePolicy.WRITE_THROUGH,
    cache_ttl=0.5,
    max_cache_size=500,
    is_production=True,
)