Introducing a new way to review your PRs.

Read the announcement

techarohq / anubis

Weighs the soul of incoming HTTP requests to stop AI crawlers

View on GitHub

19 documents · Updated Nov 23, 2026

docs / state-and-storage

State and storage backends

Anubis uses a small storage abstraction to keep protection state and short-lived cache data. The same contract lets one service use local memory or disk and lets several service instances share a remote store. Backend choice affects whether state survives a restart and whether it can be shared across instances.

Storage model

The storage model treats state as byte values addressed by string keys. Every value can have an expiry, so protection data can disappear when it is no longer useful; callers also receive a common not-found result instead of a backend-specific result. The store.Interface contract is the boundary between Anubis features and the selected backend. lib/…/interface.go• L11-44

The model has two useful layers:

  • Raw store: Reads, writes, and deletes byte values.
  • Typed view: store.JSON[T] adds JSON conversion and an optional key prefix for a feature that wants typed records without a new backend. lib/…/interface.go• L49-99

The IsPersistent result tells the wider service whether data remains after a restart. The service uses this signal to warn when persistent state is configured without a signing key that can keep challenges valid across restarts. cmd/…/main.go• L337-345

Backend selection

The policy selects a backend by name and passes backend-specific parameters to its factory. Validation happens before the policy creates the running service, which makes an unknown backend or invalid backend settings a configuration error rather than a runtime choice. lib/…/store.go• L17-43, lib/…/policy.go• L134-145

Factories register names with the store registry. This keeps the policy format stable while allowing local, remote, and future backends to share the same selection path. lib/…/registry.go• L15-42

flowchart LR
    Policy[Policy store choice] --> Validate[Validate backend settings]
    Validate --> Factory[Build selected backend]
    Factory --> Contract[Storage contract]
    Contract --> State[Protection state and caches]

Backend categories

The supported backends cover single-process use, local persistence, and shared remote persistence. The choice should match the service topology and the value of state after a restart.

CategoryBackendMain useRestart and sharing behavior
Volatile localmemorySimple single-instance operation and temporary stateLost on restart and not shared across Anubis instances. lib/…/memory.go• L73-100
Persistent localbboltA service that needs state on its local filesystemSurvives restarts but is not for several Anubis instances sharing one store. lib/…/bbolt.go• L19-35
Shared remotevalkeySeveral Anubis instances using one shared key-value servicePersistent and suitable for shared service use. lib/…/valkey.go• L11-16
Shared object storages3apiDurable state in an S3-compatible bucketPersistent and remote; expiry is managed as part of the stored object because object storage has no native TTL. lib/…/s3api.go• L33-78

Memory backend

The memory backend is a volatile store for one running Anubis instance. It supports expiry and limits its retained entries, but it does not provide a shared view to other instances or a durable copy after shutdown. lib/…/memory.go• L26-34, lib/…/memory.go• L56-75

References: lib/…/memory.go• L26-34 lib/…/memory.go• L56-75

Local database backend

The bbolt backend provides a persistent local store. It is a good fit when one Anubis instance owns the data file; it is not a shared coordination store for multiple instances. lib/…/factory.go• L28-54, lib/…/bbolt.go• L179-181

References: lib/…/factory.go• L28-54 lib/…/bbolt.go• L179-181

Valkey backend

The Valkey backend provides a shared remote key-value store with native expiry. Its connection mode can target a single service, a cluster, or a Sentinel-managed service, so it fits deployments where several Anubis instances must observe the same protection state. lib/…/factory.go• L30-71, lib/…/factory.go• L130-177

References: lib/…/factory.go• L30-71 lib/…/factory.go• L130-177

S3-compatible backend

The S3-compatible backend stores each value as an object in a configured bucket. It records the expiry with the object and treats an expired object as absent when it is read, which gives object storage the same short-lived state behavior as the other backends. lib/…/factory.go• L90-106, lib/…/s3api.go• L33-55

References: lib/…/factory.go• L90-106 lib/…/s3api.go• L33-55

Backend decoration

The storage layer can wrap a backend without changing the backend choice. ActorifiedStore serializes operations through actors while preserving the same storage contract, and JSON[T] adds typed records and key namespaces. These wrappers let a feature add access or data-shape rules without coupling itself to memory, disk, or a remote service. lib/…/actorifiedstore.go• L12-42, lib/…/interface.go• L49-99

Child pages

Read Storage contracts for the shared operations, error meanings, expiry rules, and typed wrapper. Read Storage backends for backend-specific configuration and operational limits.

Change checklist

When these files or sections are changed, remember to consider these:

  • A new backend name must be registered and loaded by the package set used during policy validation.
  • A change to expiry or not-found behavior must remain consistent across every backend and the typed JSON wrapper.
  • A change to persistence semantics must keep the restart warning aligned with IsPersistent.
  • A change to shared-instance support must update deployment guidance for the selected backend.

Important failure modes

TriggerConsequenceGuard
Memory storage is used by several Anubis instancesEach instance sees different protection stateUse a shared remote backend for multi-instance service use
A persistent backend is used without a signing keyChallenges are invalidated after a restartConfigure a supported private signing key
A backend configuration names an unknown backend or invalid parametersPolicy validation fails before the service startsValidate the policy and backend settings together
S3 object expiry metadata is missing or staleExpired protection state can remain readable or be treated inconsistentlyPreserve the backend expiry contract when writing and reading objects

Key files

FileWhy to read it
lib/…/interface.goDefines the common state contract and typed JSON view
lib/…/registry.goShows how backend names map to factories
lib/…/store.goShows how policy configuration selects and validates a backend
lib/…/memory.goShows the volatile local backend
lib/…/bbolt.goShows the persistent local backend boundary
lib/…/valkey.goShows the shared remote key-value backend
lib/…/s3api.goShows the shared object-storage backend and expiry behavior
cmd/…/main.goShows how persistence affects restart and signing-key warnings