CultLib/src/GameCult.Networking/Contracts/cultnet-distributed-database.md

118 lines
5.7 KiB
Markdown
Raw Normal View History

# CultNet Distributed Database Notes
CultNet's distributed database path is a typed realtime database over CultCache
documents. The design should borrow proven distributed-systems ideas without
copying their entire machine or laundering vague consistency through impressive
words.
## Prior Art To Keep
- RethinkDB changefeeds: subscriptions should emit old/new document views and
support point, table/schema, and filtered watches.
- Firebase Realtime Database: client ergonomics should make realtime sync feel
like the default path, but presence, disconnect handling, and server-side
authority still need explicit ownership.
- Dynamo: partitioning, replication metadata, vector-ish causality, hinted
recovery, and application-visible conflict resolution are useful. "Always
writeable" is not the first CultNet promise.
- Raft: primary authority per shard is the first understandable consistency
model. Leader election/log replication can come later if the cluster needs
automatic failover.
- SWIM: peer membership and failure suspicion should be gossip-shaped when the
mesh grows beyond small static clusters.
- CRDTs: offline multi-writer documents are valid only for types with declared,
deterministic merge semantics.
- Hashgraph: gossip-about-gossip is interesting for compact event provenance
and fair ordering research. CultNet should not adopt public-ledger machinery
or crypto-token assumptions as a shortcut to database correctness.
## Current Cut
Implemented:
- `CultNetDatabase`: database-style facade over a `CultCache`
- primary-shard write checks
- typed R3 watch streams
- raw CultNet put/delete application through shard policy
- `CultNetDatabaseServer`: schema-v0 bridge for snapshot, put, and delete
- `Client` and `Server` schema-v0 dispatch hooks
- `CultNetHost.Database` and `CultNetHost.DatabaseServer`
2026-05-19 17:10:24 +01:00
- subscription request/cancel/change messages
- server-side live change fanout for schema/key-filtered subscriptions
2026-05-19 17:34:49 +01:00
- shard catalog request/response messages
- shard descriptors with owner runtime id, epoch, schema filters, key prefix,
primary endpoints, replica endpoints, read replica endpoints, and region
- epoch-aware raw put/delete messages
- routing hints on shard authority errors
- injectable non-primary write forwarding policy
2026-05-19 17:47:43 +01:00
- schema-v0 write forwarder that dials advertised `cultnet://host:port`
primary endpoints
2026-05-19 17:52:29 +01:00
- in-memory per-shard mutation logs with sequence numbers, epochs, mutation
kind, schema id, key, and document references
- mutation-log catch-up by last seen sequence
2026-05-19 18:01:11 +01:00
- wire-level shard log request/response messages
- raw put/delete representation for accepted mutation-log entries
2026-05-19 18:05:51 +01:00
- replica-side shard log application with epoch checks, gap rejection, and
idempotent replay
2026-05-19 18:12:24 +01:00
- shard log replicator that pulls non-primary shards from catalog-advertised
primary endpoints
- schema-v0 shard log fetcher for `cultnet://host:port` endpoints
2026-05-19 18:19:09 +01:00
- restart-safe replica cursor store, including a local MessagePack file-backed
implementation
- client-authority scopes for locally predicted input documents
- predicted and reconciled database change kinds for client-side prediction
- simulation witness observations for mesh-side opinions about frame facts
- deterministic consensus candidates derived from witness observations
Not implemented yet:
- membership/failure detection
- leader election or automatic shard failover
2026-05-19 17:52:29 +01:00
- durable log persistence and compaction
2026-05-19 18:19:09 +01:00
- snapshot fallback for compacted log history
- transport messages/documents for witness observation gossip
- rollback/resimulation helpers for simulation frames
- declared CRDT merge policies
## Live Invariants
- CultCache owns document schema, identity, local indexes, and local diffing.
- CultNet owns transport, shard authority, remote mutation delivery, and
subscription fanout.
- One primary owns writes for a shard until a stronger policy exists.
- Non-owner writes are rejected or explicitly forwarded. They are not silently
applied.
- Raw snapshot/put/delete messages must pass through `CultNetDatabase`, not
bypass shard policy.
- Realtime subscriptions publish domain changes, not storage envelopes.
- Client-owned input documents may be predicted locally only inside explicit
`CultNetClientAuthorityScope` declarations.
- Predicted input state is not authoritative shared state. The shard log still
decides the committed ordering and emits reconciliation when it corrects or
confirms local prediction.
- Witness observations are immutable reports about a shard epoch, frame,
subject, claim kind, and claim hash.
- Consensus candidates are derived from observations. They may inform the
authoritative commit path, but they are not themselves committed world state
until a shard owner writes them into the log.
## Next Coherent Slice
2026-05-19 18:19:09 +01:00
Add durable mutation-log storage and snapshot fallback:
2026-05-19 18:01:11 +01:00
- persist per-shard mutation logs instead of keeping them only in memory
- define the compaction boundary that turns old log reads into snapshot reads
2026-05-19 18:19:09 +01:00
- return `ResyncRequired` when a replica asks for compacted history
- after that, add simulation-frame rollback/resimulation helpers around
predicted input streams
- add schema-v0 observation gossip once the local consensus document shape is
stable
2026-05-19 18:19:09 +01:00
The log is wire-readable, replicas can apply it explicitly, a pull loop can
drive non-primary shards from primary endpoints, and replica cursors can survive
restart. Client-owned input documents can now be predicted locally and
reconciled when the authoritative log arrives. Nodes can also aggregate witness
observations into deterministic consensus candidates for simulation facts like
"who shot first." The next useful layer is making authoritative mutation logs
durable and compaction-aware. Membership and leader election should wait until
basic replica catch-up is boring.