A high-performance Python library providing bucket-based rotating data structures with approximate TTL eviction and strict memory constraints.
Project description
RoTTL — Rotating TTL Data Structures
RoTTL provides memory-efficient rotating sets, dicts, and Bloom filters with approximate TTL-based eviction.
Rather than maintaining per-item expiry timestamps, the TTL window is divided into a fixed number of rotating buckets — expired buckets are evicted as a whole, trading per-item precision for significantly lower memory usage and faster writes, at the cost of sequential bucket scans.
Installation
pip install rottl
The only dependency is rbloom. If you encounter installation issues, refer to their documentation.
Quickstart
RotatingTTLSet— exact membership tracking backed by native Pythonsetbuckets.
from rottl import RotatingTTLSet
seen_ids = RotatingTTLSet(
ttl=24 * 60 * 60, num_buckets=6, bucket_capacity=1_000_000
)
seen_ids.add("user_42")
print("user_42" in seen_ids) # True
RotatingTTLDict— key-value storage backed by native Pythondictbuckets.
from rottl import RotatingTTLDict
response_cache = RotatingTTLDict(
ttl=60 * 60, num_buckets=4, bucket_capacity=10_000
)
response_cache["GET /api/status"] = {"ok": True}
print(response_cache.get("GET /api/status")) # {'ok': True}
RotatingTTLBloom— probabilistic membership tracking backed byrbloom.Bloombuckets.
from rottl import RotatingTTLBloom
visited_urls = RotatingTTLBloom(
ttl=7 * 24 * 60 * 60, num_buckets=7, bucket_capacity=10_000_000, bucket_fpr=0.001
)
visited_urls.add("https://example.com")
print("https://example.com" in visited_urls) # True
All three structures rotate automatically based on time and capacity, though capacity tracking differs between implementations:
RotatingTTLSetandRotatingTTLDict: Capacity is checked inline on every insertion via an $O(1)$len()call.RotatingTTLBloom: Estimating the number of unique inserted items requires inspecting filter occupancy by counting set bits — an $O(M)$ operation. To keep the hot path $O(1)$ for the vast majority of insertions, capacity is managed via an amortized countdown that defers the check.
When to use RoTTL
- Approximate TTL is acceptable. Expiry happens at bucket boundaries, not per item. Under normal load (no capacity-based eviction), items live between
ttl - (ttl / num_buckets)andttlseconds. Capacity pressure can cause earlier eviction. Settingbucket_ttl_jitter_ratiofurther reduces the minimum, but helps avoid synchronized eviction spikes across instances. - Memory-constrained environments. RoTTL's bucket-level eviction avoids per-item bookkeeping, keeping structure overhead proportional to bucket count rather than item count.
RotatingTTLDictuses roughly 3–4× less memory thancachetools.TTLCache.
- Write-heavy workloads. RoTTL's write path is lightweight — no per-item expiry metadata is maintained on insertion.
RotatingTTLDictis 6–15× faster thancachetools.TTLCacheon insertions (varies by fast-reject usage and rotation pressure).
- Lookup performance scales with configuration. Lookups scan up to
num_bucketsbuckets, so with a small bucket count the overhead is negligible. With a large bucket count, hit cost depends on which bucket the item is found in, and miss cost grows linearly.RotatingTTLSetandRotatingTTLDict's history fast-reject option makes most miss latency independent ofnum_buckets, at the cost of slower rotations.
Advanced Usage
Because eviction happens at the bucket level, a rotation event drops all items in that bucket at
once. In deployments with multiple instances — such as Kubernetes pods — all instances initialized
around the same time will rotate in lockstep, causing simultaneous cache drops across the fleet.
bucket_ttl_jitter_ratio addresses this by randomly shortening each bucket's TTL by up to
ratio * bucket_ttl seconds on rotation, spreading eviction events out over time.
RotatingTTLSet and RotatingTTLDict support an optional history fast-reject mode, which
maintains an auxiliary Bloom filter over all non-expired historical buckets. This allows most
negative lookups to be rejected without scanning the full bucket deque, at the cost of filter
rebuild on each rotation.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rottl-0.0.5.tar.gz.
File metadata
- Download URL: rottl-0.0.5.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8b7ac843ca8be91235263c83b2cecdffa8773465b267a86c1509dfb4d692fd9
|
|
| MD5 |
b50766ea86f8116c0c629c25ca1f415f
|
|
| BLAKE2b-256 |
585bff52594a99669cf05485b47e616def51f24182b64b50e083b7c5889a8ba4
|
Provenance
The following attestation bundles were made for rottl-0.0.5.tar.gz:
Publisher:
python-publish.yml on sharvul/rottl
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rottl-0.0.5.tar.gz -
Subject digest:
a8b7ac843ca8be91235263c83b2cecdffa8773465b267a86c1509dfb4d692fd9 - Sigstore transparency entry: 1850965021
- Sigstore integration time:
-
Permalink:
sharvul/rottl@371f1b9882dfda08fb5842d6448d61b3b017dd77 -
Branch / Tag:
refs/tags/v0.0.5 - Owner: https://github.com/sharvul
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@371f1b9882dfda08fb5842d6448d61b3b017dd77 -
Trigger Event:
release
-
Statement type:
File details
Details for the file rottl-0.0.5-py3-none-any.whl.
File metadata
- Download URL: rottl-0.0.5-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0fbf5588d7d4506a4aa593d4a4e5946cedc078cd9766586a2dad3934f050d167
|
|
| MD5 |
cba809e8f1ef34b7b240c8473a5f096a
|
|
| BLAKE2b-256 |
767adabe9e810979da30254f10b7ca7e43eb223765601e600a2ff29e5cddd9cd
|
Provenance
The following attestation bundles were made for rottl-0.0.5-py3-none-any.whl:
Publisher:
python-publish.yml on sharvul/rottl
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rottl-0.0.5-py3-none-any.whl -
Subject digest:
0fbf5588d7d4506a4aa593d4a4e5946cedc078cd9766586a2dad3934f050d167 - Sigstore transparency entry: 1850965147
- Sigstore integration time:
-
Permalink:
sharvul/rottl@371f1b9882dfda08fb5842d6448d61b3b017dd77 -
Branch / Tag:
refs/tags/v0.0.5 - Owner: https://github.com/sharvul
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@371f1b9882dfda08fb5842d6448d61b3b017dd77 -
Trigger Event:
release
-
Statement type: