No description
  • Erlang 94.1%
  • Shell 3.1%
  • Rust 2.8%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
beamologist acc1aa9418
All checks were successful
lint-and-test / rust (clippy, deny warnings) (pull_request) Successful in 34s
lint-and-test / compile + NIFs + xref + lint + eunit + ct + proper + dialyzer (OTP 28.4.3) (pull_request) Successful in 9m0s
lint-and-test / rust (clippy, deny warnings) (push) Successful in 42s
lint-and-test / compile + NIFs + xref + lint + eunit + ct + proper + dialyzer (OTP 28.4.3) (push) Successful in 9m19s
Publish to hex / guard (push) Successful in 4s
Publish to hex / The release guard (push) Successful in 0s
Publish to hex / rust (clippy, deny warnings) (push) Successful in 31s
Publish to hex / Guard and build the package (push) Successful in 36s
Publish to hex / compile + NIFs + xref + lint + eunit + ct + proper + dialyzer (OTP 28.4.3) (push) Successful in 9m28s
Publish to hex / Test the release commit (push) Successful in 0s
Publish to hex / Publish to hex.pm (push) Successful in 30s
reckon-db#14 r2: an activate asked while one runs leaves it running and marks one rerun (Mercurius r1 livelock); tests for it, the crash rerun, and the CHANGELOG
Refs #14
2026-12-02 12:30:09 +01:00
.forgejo/workflows ci: reckon-db's workflows run on the forge (reckon-db#5) 2026-12-02 07:33:52 +01:00
.github ci: reckon-db's workflows run on the forge (reckon-db#5) 2026-12-02 07:33:52 +01:00
assets links: a started link follows its source, once per event, across crashes and restarts 2026-10-23 04:18:22 +02:00
benchmarks docs: benchmarks README says the shared bench harness is not published 2026-10-20 11:35:04 +02:00
docs chore: point workspace paths at work/github.com 2026-07-26 13:08:39 +02:00
guides guide: joining a store that holds data, and the two limits of the guard (reckon-db#4) 2026-12-02 09:55:37 +01:00
include concurrent_append_SUITE: ten writers are held to the contract too (reckon-db#7) 2026-12-02 09:30:29 +01:00
native ci: reckon-db's workflows run on the forge (reckon-db#5) 2026-12-02 07:33:52 +01:00
plans docs: the texts that still named khepri 0.17.2, horus 0.3 and ra 2.16.x 2026-11-11 19:20:05 +01:00
priv reckon-db: the NIF summary line names both causes 2026-10-23 04:58:16 +02:00
scripts ci: reckon-db's workflows run on the forge (reckon-db#5) 2026-12-02 07:33:52 +01:00
src reckon-db#14 r2: an activate asked while one runs leaves it running and marks one rerun (Mercurius r1 livelock); tests for it, the crash rerun, and the CHANGELOG 2026-12-02 12:30:09 +01:00
test reckon-db#14 r2: an activate asked while one runs leaves it running and marks one rerun (Mercurius r1 livelock); tests for it, the crash rerun, and the CHANGELOG 2026-12-02 12:30:09 +01:00
.dialyzer_baseline coordinator: a cluster join never wipes a store that holds data (reckon-db#2) 2026-12-02 07:20:50 +01:00
.gitignore chore: untrack doc/ — ex_doc generated, source lives in assets/+guides/ 2026-05-26 23:50:34 +02:00
.tool-versions ci: OTP 28.4.3, and pin the project plugins, rebar3_hex at 7.2.0 2026-09-23 16:10:15 +02:00
CHANGELOG.md reckon-db#14 r2: an activate asked while one runs leaves it running and marks one rerun (Mercurius r1 livelock); tests for it, the crash rerun, and the CHANGELOG 2026-12-02 12:30:09 +01:00
CODE_OF_CONDUCT.md chore: release-prep for v2.2.2 — docs clean, contributing files, dialyzer backlog 2026-05-17 17:39:25 +02:00
CONTRIBUTING.md Point every Codeberg reference at GitHub 2026-09-02 18:11:38 +02:00
LICENSE Initial commit 2026-01-03 07:24:41 +01:00
README.md reckon-db 5.12.1: the leader answers while it activates (#14); a late store joins (#15) 2026-12-02 12:18:46 +01:00
rebar.config deps: Khepri 0.17.11, the first release with the does_path_match crash fix 2026-11-11 19:08:05 +01:00
rebar.config.script docs: the texts that still named khepri 0.17.2, horus 0.3 and ra 2.16.x 2026-11-11 19:20:05 +01:00
SECURITY.md Add SECURITY.md: where to report a vulnerability, and what happens next 2026-09-28 21:12:35 +02:00

reckon-db

Buy Me a Coffee

BEAM-native Event Store built on Khepri/Ra with Raft consensus.

Architecture

Overview

reckon-db is an Erlang implementation of a distributed event store designed for:

  • Event Sourcing: Store and replay events with optimistic concurrency
  • Clustering: Automatic node discovery and Raft-based replication
  • High Throughput: Partitioned writers for concurrent stream writes
  • Edge & Datacenter: Works on Nerves devices and Kubernetes clusters

Features

  • Event stream operations (append, read, subscribe) with versioning and optimistic concurrency
  • Structural aggregate-type namespace (Model C) plus an opt-in write-maintained secondary index (by tag, event type, metadata key) for O(matches) cross-cutting reads (see guides/storage_internals.md, guides/event_sourcing.md)
  • Dynamic Consistency Boundary (DCB): conditional append on a tag-filter context query, with tag and event_type leaves and full boolean algebra (see guides/dcb.md, guides/dcb_raft_design.md)
  • Command Context Consistency (CCC): payload-indexed conditional append and reads via {payload, Key} / {payload_hash, [Keys]} index declarations and {payload_match, ...} / {payload_hash_match, ...} filters (see guides/ccc.md)
  • Persistent subscriptions (stream, event type, pattern, payload matching)
  • Snapshot management for aggregate state
  • Stream links and projections, system streams under the $ namespace, and temporal (time-based) queries (see guides/stream_links.md, guides/system_streams.md, guides/temporal_queries.md)
  • Event lifecycle management (scavenging, schema evolution / upcasting) (see guides/scavenging.md, guides/schema_evolution.md)
  • Emitter pools for high-throughput event delivery
  • Clustering with hardened UDP multicast discovery (HMAC-keyed gossip v2) and Kubernetes DNS discovery (see guides/configuration.md, guides/cluster_consistency.md)
  • Embedded Rust NIFs (crypto, hashing, compression, aggregation, filter matching) with pure-Erlang fallbacks when no toolchain is present
  • BEAM telemetry with optional OpenTelemetry exporters

See the guides index for the full documentation set.

Installation

Add to your rebar.config:

{deps, [
    {reckon_db, "~> 5.5"}
]}.

The embedded Rust NIFs are optional: with a Cargo toolchain present at build time they are compiled for 3-15x acceleration; without one, reckon-db logs a warning and uses the pure-Erlang fallbacks, so it works everywhere.

Versions

Component Version
reckon_db (this repo) 5.12.1
reckon_gater (dep) ~> 3.14
khepri (dep) 0.17.11
ra (transitive, via khepri) 2.16.13
telemetry (dep) 1.3.0
Erlang/OTP 28 (built and tested on 28.4.3: CI and .tool-versions)

OTP 28 is the supported release, and the only one anything here is built and tested on. A build on a newer OTP stops at once with a message saying so (rebar.config.script), also when reckon_db is a dependency.

reckon-db depends on reckon_gater, khepri, and ra only. It does not depend on evoq or reckon_evoq (see Reckon stack).

Quick Start

%% Start the application
application:ensure_all_started(reckon_db).

%% Append events to a stream. A stream id is <prefix>-<32 hex>; anything
%% else is refused, and reckon_gater_stream_id:new(user) makes one. The rule:
%% https://hexdocs.pm/reckon_gater/stream_ids.html
Events = [
    #{
        event_type => <<"user_registered_v1">>,
        data => #{name => <<"Alice">>, email => <<"alice@example.com">>},
        metadata => #{correlation_id => <<"req-123">>}
    }
],
{ok, Version} = reckon_db_streams:append(my_store, <<"user-018f6a7b8c9d4e5f60718293a4b5c6d7">>, -1, Events).

%% Read events from a stream
{ok, ReadEvents} = reckon_db_streams:read(my_store, <<"user-018f6a7b8c9d4e5f60718293a4b5c6d7">>, 0, 100, forward).

%% Subscribe to events. The subscriber is the process the events are sent to.
{ok, SubKey} = reckon_db_subscriptions:subscribe(
    my_store,
    stream,                    %% Type: stream | event_type | event_pattern | event_payload
    <<"user-018f6a7b8c9d4e5f60718293a4b5c6d7">>,            %% Selector
    <<"user_projection">>,     %% Subscription name
    #{subscriber => self()}
).

%% Receive events: they arrive in batches of #event{} records
%% (include_lib "reckon_gater/include/reckon_gater_types.hrl")
receive
    {events, Received} -> io:format("Received: ~p~n", [Received])
end.

API Reference

Streams

%% Append events (returns new version)
reckon_db_streams:append(StoreId, StreamId, ExpectedVersion, Events) ->
    {ok, NewVersion} | {error, version_mismatch | term()}.

%% Read events from a stream
reckon_db_streams:read(StoreId, StreamId, FromVersion, Count, Direction) ->
    {ok, [Event]} | {error, stream_not_found | term()}.

%% Read across all streams
reckon_db_streams:read_all(StoreId, FromVersion, Count, Direction) ->
    {ok, [Event]} | {error, term()}.

%% Read events by type
reckon_db_streams:read_by_event_types(StoreId, EventTypes, Opts) ->
    {ok, [Event]} | {error, term()}.

%% Get stream version
reckon_db_streams:get_version(StoreId, StreamId) -> {ok, Version} | {error, term()}.

%% Check if stream exists
reckon_db_streams:exists(StoreId, StreamId) -> boolean().

%% List all streams
reckon_db_streams:list_streams(StoreId) -> {ok, [StreamId]} | {error, term()}.

%% Delete stream (soft delete)
reckon_db_streams:delete(StoreId, StreamId) -> ok | {error, term()}.

%% Conditional append for Dynamic Consistency Boundary (DCB).
%% Writes Events iff no event matching TagFilter has seq > SeqCutoff.
%% Use SeqCutoff = -1 to require "no matching event has ever existed".
%% Full guide: guides/dcb.md
reckon_db_streams:append_if_no_tag_matches(StoreId, TagFilter, SeqCutoff, Events) ->
    {ok, LastSeq} | {error, {context_changed, MaxSeq}} | {error, term()}.

Subscriptions

%% Create subscription
reckon_db_subscriptions:subscribe(StoreId, Type, Selector, Name) ->
    {ok, SubscriptionKey} | {error, term()}.
reckon_db_subscriptions:subscribe(StoreId, Type, Selector, Name, Opts) ->
    {ok, SubscriptionKey} | {error, term()}.

%% Remove subscription (by key or by type+name)
reckon_db_subscriptions:unsubscribe(StoreId, SubscriptionKey) -> ok | {error, term()}.
reckon_db_subscriptions:unsubscribe(StoreId, Type, SubscriptionName) -> ok | {error, term()}.

%% Get subscription by key
reckon_db_subscriptions:get(StoreId, SubscriptionKey) ->
    {ok, Subscription} | {error, not_found}.

%% Acknowledge event processing
reckon_db_subscriptions:ack(StoreId, StreamId, SubscriptionName, EventNumber) -> ok.

%% List subscriptions
reckon_db_subscriptions:list(StoreId) -> {ok, [Subscription]}.

%% Check if subscription exists
reckon_db_subscriptions:exists(StoreId, SubscriptionKey) -> boolean().

%% Subscription types:
%%   stream - Events from a specific stream
%%   event_type - Events matching event type
%%   event_pattern - Events matching stream pattern (wildcards)
%%   event_payload - Events matching payload criteria

Snapshots

%% Save snapshot
reckon_db_snapshots:save(StoreId, StreamId, Version, Data) -> ok.
reckon_db_snapshots:save(StoreId, StreamId, Version, Data, Metadata) -> ok.

%% Load latest snapshot
reckon_db_snapshots:load(StoreId, StreamId) -> {ok, Snapshot} | {error, not_found}.

%% Load snapshot at specific version
reckon_db_snapshots:load_at(StoreId, StreamId, Version) -> {ok, Snapshot} | {error, not_found}.

%% List all snapshots for stream
reckon_db_snapshots:list(StoreId, StreamId) -> {ok, [Snapshot]}.

%% Delete all snapshots for stream
reckon_db_snapshots:delete(StoreId, StreamId) -> ok.

%% Delete snapshot at specific version
reckon_db_snapshots:delete_at(StoreId, StreamId, Version) -> ok.

%% Check if snapshot exists
reckon_db_snapshots:exists(StoreId, StreamId) -> boolean().
reckon_db_snapshots:exists_at(StoreId, StreamId, Version) -> boolean().

Aggregation

%% Fold events left to right (chronological order)
%% Returns a tagged_map with {sum, N} and {overwrite, V} tags preserved
reckon_db_aggregator:foldl(Events) -> tagged_map().
reckon_db_aggregator:foldl(Events, InitialState) -> tagged_map().

%% Fold events right to left (reverse order)
reckon_db_aggregator:foldr(Events) -> tagged_map().
reckon_db_aggregator:foldr(Events, InitialState) -> tagged_map().

%% Finalize a tagged map (unwrap {sum, N} -> N, {overwrite, V} -> V)
reckon_db_aggregator:finalize(TaggedMap) -> map().

%% Aggregate events with optional snapshot (convenience function)
reckon_db_aggregator:aggregate(Events, Snapshot | undefined, Opts) -> map().
%% Opts: #{initial_state => map(), finalize => boolean()}

Example usage:

%% Load events and aggregate
{ok, Events} = reckon_db_streams:read(my_store, <<"account-018f6a7b8c9d4e5f60718293a4b5c6d8">>, 0, 10000, forward),
TaggedState = reckon_db_aggregator:foldl(Events, #{balance => {sum, 0}}),
FinalState = reckon_db_aggregator:finalize(TaggedState).

%% Or use aggregate/3 with snapshot support
{ok, Snapshot} = reckon_db_snapshots:load(my_store, <<"account-018f6a7b8c9d4e5f60718293a4b5c6d8">>),
{ok, NewEvents} = reckon_db_streams:read(my_store, <<"account-018f6a7b8c9d4e5f60718293a4b5c6d8">>, Snapshot#snapshot.version + 1, 10000, forward),
State = reckon_db_aggregator:aggregate(NewEvents, Snapshot, #{}).

Telemetry

%% Attach default logger handler
reckon_db_telemetry:attach_default_handler() -> ok.

%% Attach custom handler
reckon_db_telemetry:attach(HandlerId, HandlerFun, Config) -> ok.

%% Detach handler
reckon_db_telemetry:detach(HandlerId) -> ok.

Configuration

%% sys.config
[{reckon_db, [
    {stores, [
        {my_store, [
            {data_dir, "/var/lib/reckon_db/my_store"},
            {mode, cluster},  %% single | cluster
            {timeout, 5000}
        ]}
    ]},
    {telemetry_handlers, [logger]},
    {writer_pool_size, 10},
    {reader_pool_size, 10},

    %% Cluster discovery (cluster mode only)
    {discovery, [
        {method, multicast},  %% multicast | k8s_dns
        {port, 45892},
        {multicast_addr, {239, 255, 0, 1}},
        {secret, <<"cluster_secret">>}
    ]}
]}].

Architecture

Supervision Tree

Supervision Tree

Event Flow

Event Flow

Telemetry Events

Every duration measurement is in native time units (erlang:monotonic_time/0 arithmetic, the unit telemetry:span/3 reports): convert with erlang:convert_time_unit(D, native, microsecond). A measurement in another unit names it (duration_us, uptime_ms).

Event Measurements Metadata
[reckon_db, stream, write, start] system_time store_id, stream_id, event_count
[reckon_db, stream, write, stop] duration, event_count store_id, stream_id, new_version
[reckon_db, stream, write, error] duration store_id, stream_id, reason
[reckon_db, stream, read, start] system_time store_id, stream_id
[reckon_db, stream, read, stop] duration, event_count store_id, stream_id
[reckon_db, subscription, created] system_time store_id, subscription_id, type
[reckon_db, subscription, deleted] system_time store_id, subscription_id
[reckon_db, snapshot, created] duration, size_bytes store_id, stream_id, version
[reckon_db, cluster, node, up] system_time store_id, node, member_count
[reckon_db, cluster, node, down] system_time store_id, node, reason
[reckon_db, cluster, leader, elected] system_time store_id, leader

Building

rebar3 compile         # Compile
rebar3 eunit           # Unit tests
scripts/check_ct.sh    # Integration + e2e tests, as the gate runs them
rebar3 dialyzer        # Type checking
rebar3 cover           # Coverage report

Testing

Test counts:

  • Unit tests: 684 tests (including NIF modules with enterprise/community equivalence tests)
  • Integration tests: 213 tests (streams, subscriptions, snapshots, integrity, cluster)
  • End-to-end tests: 29 tests (25 through the gater, 4 over the mesh)
rebar3 eunit --dir=test/unit              # All unit tests
rebar3 ct --dir=test/integration          # Integration tests
rebar3 ct --dir=test/e2e                  # E2E tests with gater
rebar3 ct --suite=reckon_db_streams_SUITE  # Streams tests
rebar3 ct --suite=reckon_db_cluster_SUITE  # Cluster tests

scripts/check_ct.sh runs rebar3 ct (and passes its arguments on) but also fails when a suite did not load: ct prints error_in_suite for such a suite, leaves it out of "All N tests passed" and rebar3 ct exits 0. A stale suite left in _build after its source was deleted is the usual way to get one.

Gateway API

reckon-db is accessed through reckon-gater, which provides the unified API for load-balanced, distributed access to event stores.

How It Works

  1. reckon-db starts and creates a gateway worker for each store
  2. Gateway workers register with the reckon-gater pg-based registry
  3. Clients use the gater API for all event store operations
  4. The gater routes requests to registered workers using round-robin with failover

Architecture

Gateway Architecture

Using the Gateway API

All event store operations go through the gater API:

%% Stream operations
{ok, Version} = reckon_gater_api:append_events(my_store, StreamId, Events).
{ok, Events} = reckon_gater_api:stream_forward(my_store, StreamId, 0, 100).
{ok, Version} = reckon_gater_api:get_version(my_store, StreamId).

%% Subscription operations
ok = reckon_gater_api:save_subscription(my_store, stream, StreamId, Name, 0, self()).

%% Snapshot operations
ok = reckon_gater_api:record_snapshot(my_store, SourceUuid, StreamUuid, Version, Record).
{ok, Snap} = reckon_gater_api:read_snapshot(my_store, SourceUuid, StreamUuid, Version).
%% Acknowledged: ok once written, or why not (reckon-gater 3.15.0+, 5.11.14+)
ok = reckon_gater_api:record_snapshot_sync(my_store, SourceUuid, StreamUuid, Version, Record).

See reckon-gater for complete API documentation.

Documentation

Full guide index with audience-grouped reading orders: guides/README.md.

Reckon stack

reckon-db is one library in the Reckon event-sourcing ecosystem. In dependency order (a library only knows about the ones above it):

  • reckon-proto: the wire-contract protobufs; source of truth for the gateway surface.
  • reckon-gater: shared types and protocols (event, snapshot, subscription, DCB/CCC tag_filter); no Reckon dependencies. The API reckon-db registers its store workers with.
  • reckon-db (this repo): BEAM-native event store. Depends on reckon_gater, khepri, ra. Does not depend on evoq or reckon_evoq.
  • reckon-nifs: standalone Rust NIF helpers; pure-Erlang fallbacks when no toolchain is present.
  • evoq: standalone CQRS/event-sourcing framework (aggregates, projections, process managers); no Reckon dependencies.
  • reckon-evoq: the adapter wiring evoq to a Reckon store. Depends on evoq and reckon_gater; not on reckon_db (it reaches the store through the gater API).
  • reckon-gateway: gRPC + HTTP/JSON ingress. Consumes reckon_gater; can embed a local reckon_db store or federate remote clusters.
  • reckon-go: the Go client; talks to reckon-gateway.
  • reckon-portal: docs and landing site for the ecosystem (reckon-internal/reckon-portal).

Origin

  • ex-esdb: the original Elixir implementation reckon-db descends from.

Contributing

Contributors are expected to follow the project's Generative AI use policy.

License

Apache-2.0