Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Rate Limiting

SynDB enforces per-IP rate limiting using a token bucket algorithm.

Defaults

ParameterDefaultEnvironment Variable
Requests per second100RATE_LIMIT_PER_SECOND
Burst capacity200RATE_LIMIT_BURST

The bucket refills at the sustained rate. Burst capacity allows short spikes above the sustained rate.

Client IP Detection

The rate limiter identifies clients by IP address, checked in order:

  1. X-Forwarded-For header (first address)
  2. X-Real-IP header
  3. Localhost (fallback for direct connections)

Behind a reverse proxy, ensure X-Forwarded-For is set correctly.

Response on Limit

When the rate limit is exceeded:

HTTP/1.1 429 Too Many Requests
Retry-After: 1

Too many requests

Client Handling

Respect the Retry-After header and implement exponential backoff:

import time
import requests

def request_with_backoff(url, headers, max_retries=3):
    for attempt in range(max_retries):
        resp = requests.get(url, headers=headers)
        if resp.status_code != 429:
            return resp
        wait = int(resp.headers.get("Retry-After", 1)) * (2 ** attempt)
        time.sleep(wait)
    raise Exception("Rate limited after retries")

For batch operations, throttle to well under 100 req/s to leave headroom for interactive use.