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 / storage-contracts

Storage contracts and concurrency

The storage layer gives Anubis one contract for short-lived and persistent key-value data. Callers select a backend by name, then use the same get, set, delete, expiry, and persistence rules without depending on the backend technology.

Store interface

The store.Interface in lib/…/interface.go is the boundary between application features and storage. It accepts a context for each operation and stores bytes under string keys. Get returns live data, Set applies a caller-supplied lifetime, and Delete removes one key. A missing key is reported as store.ErrNotFound, so callers can use one check across all backends.

The interface also exposes whether data survives a service restart. This lets the application warn when persistent challenge data needs a signing key that remains stable across restarts. The typed JSON wrapper adds value encoding and decoding plus an optional key prefix while keeping the same expiry and persistence behavior.

ContractMeaning for callers
GetReturns the stored bytes only while the key is live.
SetReplaces or creates a key and gives it an expiry duration.
DeleteRemoves a key and reports a missing key as ErrNotFound.
IsPersistentReports whether the backend keeps data after a restart.
ErrCantEncode / ErrCantDecodeIdentifies a value format failure at a storage boundary.

References: lib/…/interface.go• L11-45 lib/…/interface.go• L49-99 cmd/…/main.go• L337-345

Backend registry

The registry maps a configuration name to a store.Factory. A factory validates raw JSON parameters before startup and builds a store.Interface when the policy is loaded. Backend packages register themselves during package initialization, while the configuration package imports the aggregate backend package so normal startup can discover the available choices.

Selection follows the same path for every backend:

  1. The policy names a backend and supplies its parameters.
  2. Configuration validation looks up the name and calls Factory.Valid.
  3. Policy loading looks up the same name and calls Factory.Build.
  4. The resulting interface is shared with features such as challenge state and caches.

This separation keeps backend-specific configuration at the edge. Unknown names and invalid parameters stop configuration before the application uses a partial store.

The diagram shows how a policy choice becomes the store used by application features.

flowchart LR
    P[Policy store choice] --> V[Validate name and parameters]
    V --> R[Registry lookup]
    R --> B[Factory builds backend]
    B --> I[Common store interface]
    I --> C[Challenges and caches]

References: lib/…/registry.go• L10-42 lib/…/store.go• L12-43 lib/…/policy.go• L134-147 lib/…/all.go• L1-11

Actor-serialized access

ActorifiedStore provides a serialized access boundary for a backend when callers need operations to pass through one actor queue. Each get, set, or delete call waits for its own result, while the actor runs one handler at a time in a background goroutine. The caller's context controls both queueing and waiting, so cancellation can stop a request without changing the store contract.

The wrapper does not change values, expiry, or error meanings. It forwards set and delete operations to the wrapped interface and forwards reads to the backend. Closing the wrapper cancels its actors; calls that reach a stopped actor return an actor-lifecycle error rather than pretending that the backend operation succeeded.

The diagram shows the request path and the serialization point.

sequenceDiagram
    participant Caller
    participant ActorifiedStore
    participant Actor as One actor queue
    participant Backend
    Caller->>ActorifiedStore: Get, Set, or Delete
    ActorifiedStore->>Actor: Enqueue request
    Actor->>Backend: Run one operation
    Backend-->>Actor: Value or error
    Actor-->>ActorifiedStore: Reply
    ActorifiedStore-->>Caller: Result

This wrapper is distinct from each backend's own safety rules. A backend may support concurrent calls directly, but actorification gives the application an explicit serialized ordering at the wrapper boundary. A request that waits longer than its context allows receives the context cause.

References: lib/…/actorifiedstore.go• L12-81 internal/…/actorify.go• L17-29 internal/…/actorify.go• L46-113

Shared store contract

The shared test contract in lib/…/storetest.go checks the behavior that every factory must present to callers. It validates configuration, builds a store, then checks the full basic lifecycle and expiry behavior. Backend tests use this contract so a local, file, or remote store cannot silently diverge on common operations.

The expected lifecycle is:

  1. A new key is absent and Get returns ErrNotFound.
  2. Set makes the value readable with the same bytes.
  3. Delete removes the value.
  4. A later Get again returns ErrNotFound.
  5. A second Delete reports that the key is absent.
  6. A value set with a short lifetime becomes absent after that lifetime.

The contract runs its behavior checks in parallel. Backend implementations therefore need safe behavior for concurrent use, not only correct results in a single sequential example. The memory backend also checks that concurrent writes remain within its fixed capacity and that repeated updates do not evict the same key as new data.

References: lib/…/storetest.go• L13-94 lib/…/memory_test.go• L9-11 lib/…/capacity_test.go• L13-55

State lifecycle

Storage state is temporary unless its key remains live and its backend keeps data across restarts. Callers should treat a successful Set as the start of a bounded lifetime, and should treat ErrNotFound as the normal result for missing or expired state. This makes challenge markers, retry limits, DNS data, and fetched metadata self-cleaning rather than permanent records.

The lifecycle has two independent boundaries:

BoundaryState effect
ExpiryThe value is no longer available after its requested lifetime.
Process restartVolatile backends lose state; persistent backends retain it.

Persistent state does not by itself preserve challenge validity. The application also needs a stable signing key. Without that key, a restart can invalidate challenges even when the backend retains their stored data.

References: lib/…/interface.go• L28-44 lib/anubis.go• L163-180 cmd/…/main.go• L337-345 lib/…/bbolt.go• L179-197

Change checklist

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

  • Keep every factory's validation and build paths aligned with the registry name used in policy configuration.
  • Preserve ErrNotFound wrapping across backend-specific missing-key errors so caches and challenge flows keep the same branch behavior.
  • Keep expiry semantics consistent when a backend has no native TTL, including reads after expiry and cleanup of expired data.
  • Recheck persistence warnings when the meaning of IsPersistent or challenge signing state changes.
  • Preserve actor shutdown and context-cancellation behavior when changing serialized access.

Important failure modes

TriggerConsequenceGuard
Policy names an unregistered backendStore validation fails before startup can use itRegistry lookup reports an unknown backend
Factory parameters are invalidBackend construction fails and policy loading records the errorCall Valid before Build
A backend hides a missing key behind its own errorCache or challenge code may treat absence as a system faultWrap or return store.ErrNotFound
A value outlives its required lifetimeRetry or challenge state can remain active too longPass the required expiry to Set and enforce it in Get
Persistent storage runs without a stable signing keyRetained challenge records do not survive restart as valid challengesConfigure a signing key when IsPersistent is true
An actor is closed while a request is queuedThe caller receives an actor-lifecycle or context errorTie wrapper lifetime to service lifetime and honor call contexts

Key files

FileWhy to read it
lib/…/interface.goDefines the storage boundary, shared errors, JSON values, and persistence signal.
lib/…/registry.goShows backend discovery and factory selection.
lib/…/store.goShows policy validation of backend names and parameters.
lib/…/actorifiedstore.goShows the serialized access boundary.
internal/…/actorify.goExplains queueing, replies, cancellation, and actor shutdown.
lib/…/storetest.goDefines the behavior shared by all storage backends.
lib/anubis.goShows a caller using expiry and ErrNotFound for challenge-related state.
cmd/…/main.goShows how persistence affects restart and signing-key warnings.