Skip to main content

flightlock banner

CI status PyPI version Python versions MIT License Code style: ruff Test coverage

GitHub stars Last commit Repo size Open issues Contributor Covenant

flightlock

Single-flight cache stampede protection for Python.

When a cached value expires under concurrent load, most caching libraries let every waiting request independently recompute it — hammering your database or API with N identical calls at once (a "cache stampede" or "thundering herd"). flightlock ensures only one caller recomputes the value while every other caller waits for and reuses that result.

Named after the "singleflight" pattern used in production caching systems (e.g. Go's golang.org/x/sync/singleflight, groupcache).

Table of contents

Why flightlock

Picture a cache key with a 60-second TTL backing an expensive DB query. The moment it expires, if 500 concurrent requests arrive for that key before it's repopulated, a naive cache sends all 500 straight to your database at once. flightlock coordinates those 500 callers so exactly one hits the database, and the other 499 wait milliseconds for that one result instead.

Quickstart

from flightlock import cached

@cached(ttl=60)
def get_user(user_id: int) -> dict:
    print("hitting the database...")
    return expensive_db_call(user_id)

get_user(1)  # prints "hitting the database...", takes 300ms
get_user(1)  # instant — cache hit, no print

With Redis and metrics enabled:

from flightlock import cached
from flightlock.backends.redis import RedisBackend

backend = RedisBackend(host="localhost", port=6379)

@cached(ttl=60, backend=backend, metrics=True)
def get_user(user_id: int) -> dict:
    return expensive_db_call(user_id)

Benchmark

Run it yourself: python benchmarks/stampede_benchmark.py

Simulating 200 concurrent requests for the same cache key, with a 0.3s simulated origin latency (e.g. a slow DB query):

Without Flightlock (Naive) With Flightlock
Origin Calls 200 1
Wall Time 0.309s 0.312s

Result: 9.5% fewer origin calls with flightlock (200 -> 1 calls for 200 concurrent requests)

Every request still returns the correct value — flightlock just ensures only one caller does the work while the other 199 wait for and share the result.

How it works

  1. A per-key lock registry tracks in-flight computations.
  2. When a caller arrives for a missing/expired key, it either becomes the leader (first arrival — runs the function) or a follower (waits for the leader's result).
  3. The leader runs the function outside the lock, so unrelated keys never block each other.
  4. When the leader finishes, every waiting follower is woken simultaneously and receives the same result (or the same exception, if it failed).

See src/flightlock/core.py for the implementation.

Features

  • ✅ Single-flight stampede protection (in-process)
  • ✅ Pluggable backends: in-memory, Redis
  • ✅ TTL jitter to prevent synchronized mass-expiry across many keys
  • ✅ Prometheus-compatible metrics (hits, misses, errors, latency)
  • ✅ Custom cache key functions
  • 🔜 Async support
  • 🔜 Cross-process distributed locking (currently: stampede protection is per-process; Redis backend shares storage across processes, not the lock)

Installation

pip install flightlock                  # core, zero dependencies
pip install flightlock[redis]           # + Redis backend
pip install flightlock[metrics]         # + Prometheus metrics
pip install flightlock[redis,metrics]   # everything

Comparison with alternatives

flightlock functools.lru_cache cachetools
Stampede protection
TTL support
Redis backend ❌ (needs extra glue)
TTL jitter
Metrics hook

flightlock isn't trying to replace general-purpose caching libraries — it solves one specific, real production problem (concurrent cache-miss stampedes) thoroughly.

Roadmap

  • Project scaffolding, CI, packaging config
  • Core single-flight lock implementation
  • In-memory backend
  • The @cached() decorator
  • TTL jitter
  • Redis backend
  • Metrics hook
  • Benchmark proving stampede protection under load
  • Publish to PyPI
  • Async support

Contributing

Contributions are welcome. See CONTRIBUTING.md for setup instructions and guidelines.

Security

See SECURITY.md for how to report a vulnerability.

Support

See SUPPORT.md for how to get help.

AI-assisted development

This project was built with AI pair-programming assistance for scaffolding, boilerplate, and code review. See AI_POLICY.md for details on how AI was used and what was independently written, tested, and verified.

License

MIT — see LICENSE.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flightlock-0.1.0.tar.gz (21.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flightlock-0.1.0-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

Details for the file flightlock-0.1.0.tar.gz.

File metadata

  • Download URL: flightlock-0.1.0.tar.gz
  • Upload date:
  • Size: 21.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.14

File hashes

Hashes for flightlock-0.1.0.tar.gz
Algorithm Hash digest
SHA256 231e855920b17a5d15ddb2643a3fef158d36b406c6b8d3504f87b4b1efb509cb
MD5 783ed05d50a0a56e2f327c00aba8e088
BLAKE2b-256 2a9ea10ace86a01790089133debf9b248e225f7a08a13e2222162715a0a6bde1

See more details on using hashes here.

File details

Details for the file flightlock-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: flightlock-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.14

File hashes

Hashes for flightlock-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b27247c1bbd90070a4c8dece83cbfdcf63ff870347f031d86c4bc5ee5771f619
MD5 f474c275158a3ca054609f589a9bcc72
BLAKE2b-256 9ef634348bc3a249bb424df04c3eb8192f5449700cffac5fdbc35220f3942ae1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page