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.
| Contract | Meaning for callers |
|---|---|
Get | Returns the stored bytes only while the key is live. |
Set | Replaces or creates a key and gives it an expiry duration. |
Delete | Removes a key and reports a missing key as ErrNotFound. |
IsPersistent | Reports whether the backend keeps data after a restart. |
ErrCantEncode / ErrCantDecode | Identifies 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:
- The policy names a backend and supplies its parameters.
- Configuration validation looks up the name and calls
Factory.Valid. - Policy loading looks up the same name and calls
Factory.Build. - 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:
- A new key is absent and
GetreturnsErrNotFound. Setmakes the value readable with the same bytes.Deleteremoves the value.- A later
Getagain returnsErrNotFound. - A second
Deletereports that the key is absent. - 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:
| Boundary | State effect |
|---|---|
| Expiry | The value is no longer available after its requested lifetime. |
| Process restart | Volatile 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
ErrNotFoundwrapping 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
IsPersistentor challenge signing state changes. - Preserve actor shutdown and context-cancellation behavior when changing serialized access.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Policy names an unregistered backend | Store validation fails before startup can use it | Registry lookup reports an unknown backend |
| Factory parameters are invalid | Backend construction fails and policy loading records the error | Call Valid before Build |
| A backend hides a missing key behind its own error | Cache or challenge code may treat absence as a system fault | Wrap or return store.ErrNotFound |
| A value outlives its required lifetime | Retry or challenge state can remain active too long | Pass the required expiry to Set and enforce it in Get |
| Persistent storage runs without a stable signing key | Retained challenge records do not survive restart as valid challenges | Configure a signing key when IsPersistent is true |
| An actor is closed while a request is queued | The caller receives an actor-lifecycle or context error | Tie wrapper lifetime to service lifetime and honor call contexts |
Key files
| File | Why to read it |
|---|---|
| lib/…/interface.go | Defines the storage boundary, shared errors, JSON values, and persistence signal. |
| lib/…/registry.go | Shows backend discovery and factory selection. |
| lib/…/store.go | Shows policy validation of backend names and parameters. |
| lib/…/actorifiedstore.go | Shows the serialized access boundary. |
| internal/…/actorify.go | Explains queueing, replies, cancellation, and actor shutdown. |
| lib/…/storetest.go | Defines the behavior shared by all storage backends. |
| lib/anubis.go | Shows a caller using expiry and ErrNotFound for challenge-related state. |
| cmd/…/main.go | Shows how persistence affects restart and signing-key warnings. |