VSTG Skips Table Gets
A pure Python, zero dependency bloom filter, built so a check that would otherwise hit your database on every request almost never has to.
The name is also short for vestige, a trace left behind by something that passed through, which is what the underlying bit array actually is.
Before:
def check_username(request):
username = request.GET["username"]
taken = User.objects.filter(username=username).exists()
return JsonResponse({"available": not taken})After:
import vstg
def check_username(request):
username = request.GET["username"]
if not vstg.might_contain("usernames", username.encode("utf-8")):
return JsonResponse({"available": True})
taken = User.objects.filter(username=username).exists()
return JsonResponse({"available": not taken})Every username that was never registered returns without the database being touched. The rare "maybe" still runs the exact same query as before, so correctness never changes, only how often the expensive path is needed.
pip install vstgNo dependencies are ever installed alongside it.
For contributing, clone and install in editable mode instead:
git clone https://github.com/vszlx4/vstg.git
cd vstg
pip install -e '.[dev]'from pathlib import Path
import vstg
vstg.init(Path("bloom_state"))
vstg.register("usernames", capacity=10_000_000, error_rate=0.001)
vstg.might_contain("usernames", username.encode("utf-8")) # check
vstg.add("usernames", username.encode("utf-8")) # recordinit and register run once, at startup. might_contain and add
run everywhere else, for the life of the process.
vstg.register(
"usernames",
capacity=10_000_000,
error_rate=0.001,
policy=vstg.PersistPolicy(
checkpoint_interval_seconds=300,
checkpoint_insert_threshold=50_000,
checkpoint_on_shutdown=True,
),
)
vstg.register(
"email_verification_tokens",
capacity=500_000,
error_rate=0.01,
shard_count=8,
)Every named filter is independent, its own capacity, error rate, and
policy. shard_count, left unset, gives a single checkpoint file. Set
it and a checkpoint only rewrites the shards that actually changed.
Capacity, error rate, and policy are fixed the moment register is
called, for the rest of the process.
A filter is a bit array of length
Sizing, given an expected item count capacity) and a target
false positive probability error_rate):
optimal_size and optimal_hash_count in core.py compute exactly
these two values.
Bit positions, rather than running
Resulting false positive rate, after
The sizing formulas above are exactly this expression solved for the
capacity degrades the real rate past what was
requested, since
BloomFilter— the bit array,add,might_contain, in memory only.ManagedBloomFilter— aBloomFilterplus automatic checkpointing.ShardedBloomFilter— checkpointing partitioned across shard files.BloomFilterRegistry— the named collectionvstg.registeruses internally.PersistPolicy— checkpoint trigger configuration for all three above.
Two space indentation, mypy --strict, no runtime dependencies, a
docstring on every module, class, and function.
git clone https://github.com/vszlx4/vstg.git
cd vstg
pip install -e '.[dev]'
python -m unittest discover -s tests -vMIT, see LICENSE.