返回顶部
e

event-store事件存储设计

Design and implement event stores for event-sourced systems. Use when building event sourcing infrastructure, implementing event persistence, projections, snapshotting, or CQRS patterns.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.0
安全检测
已通过
811
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

event-store

Event Store

Guide to designing event stores for event-sourced applications — covering event schemas, projections, snapshotting, and CQRS integration.

When to Use This Skill

  • - Designing event sourcing infrastructure
  • Choosing between event store technologies
  • Implementing custom event stores
  • Building projections from event streams
  • Adding snapshotting for aggregate performance
  • Integrating CQRS with event sourcing

Core Concepts

Event Store Architecture

CODEBLOCK0

Event Store Requirements

RequirementDescription
Append-onlyEvents are immutable, only appends
Ordered
Per-stream and global ordering | | Versioned | Optimistic concurrency control | | Subscriptions | Real-time event notifications | | Idempotent | Handle duplicate writes safely |

Technology Comparison

TechnologyBest ForLimitations
EventStoreDBPure event sourcingSingle-purpose
PostgreSQL
Existing Postgres stack | Manual implementation | | Kafka | High-throughput streams | Not ideal for per-stream queries | | DynamoDB | Serverless, AWS-native | Query limitations |

Event Schema Design

Events are the source of truth. Well-designed schemas ensure long-term evolvability.

Event Envelope Structure

CODEBLOCK1

Schema Evolution Rules

  1. 1. Add fields freely — new optional fields are always safe
  2. Never remove or rename fields — introduce a new event type instead
  3. Version event typesOrderPlacedV2 when the schema changes materially
  4. Upcast on read — transform old versions to the current shape in the deserializer

PostgreSQL Event Store Schema

CODEBLOCK2

Event Store Implementation

CODEBLOCK3

Projections

Projections build read-optimised views by replaying events. They are the "Q" side of CQRS.

Projection Lifecycle

  1. 1. Start from checkpoint — resume from last processed global position
  2. Apply events — update the read model for each relevant event type
  3. Save checkpoint — persist the new position atomically with the read model

Projection Example

CODEBLOCK4

Projection Design Rules

  • - Idempotent handlers — replaying the same event twice must not corrupt state
  • One projection per read model — keep projections focused
  • Rebuild from scratch — projections should be deletable and fully replayable
  • Separate storage — projections can live in different databases (Postgres, Elasticsearch, Redis)

Snapshotting

Snapshots accelerate aggregate rehydration by caching state at a known version.

Use when streams exceed ~100 events, aggregates have expensive rehydration, or on a cadence (e.g., every 50 events).

Snapshot Flow

CODEBLOCK5

CQRS Integration

CQRS separates the write model (commands → events) from the read model (projections).

CODEBLOCK6

Key Principles

  1. 1. Write side validates commands, emits events, enforces invariants
  2. Read side subscribes to events, builds optimised query models
  3. Eventual consistency — reads may lag behind writes by milliseconds to seconds
  4. Independent scaling — scale reads and writes separately

Command Handler Pattern

CODEBLOCK7

EventStoreDB Integration

CODEBLOCK8

DynamoDB Event Store

CODEBLOCK9

DynamoDB table design: PK=STREAM#{id}, SK=VERSION#{version}, GSI1 for global ordering.

Best Practices

Do

  • - Name streams {Type}-{id} — e.g., INLINECODE4
  • Include correlation / causation IDs in metadata for tracing
  • Version event schemas from day one — plan for evolution
  • Implement idempotent writes — use event IDs for deduplication
  • Index for your query patterns — stream, global position, event type

Don't

  • - Mutate or delete events — they are immutable facts
  • Store large payloads — keep events small; reference blobs externally
  • Skip optimistic concurrency — prevents data corruption
  • Ignore backpressure — handle slow consumers gracefully
  • Couple projections to the write model — projections should be independently deployable

NEVER Do

  • - NEVER update or delete events — Events are immutable historical facts; create compensating events instead
  • NEVER skip version checks on append — Optimistic concurrency prevents lost updates and corruption
  • NEVER embed large blobs in events — Store blobs externally, reference by ID in the event
  • NEVER use random UUIDs for event IDs without idempotency checks — Retries create duplicates
  • NEVER read projections for command validation — Use the event stream as the source of truth
  • NEVER couple projections to the write transaction — Projections must be rebuildable independently

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 backend-event-stores-1776419953 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 backend-event-stores-1776419953 技能

通过命令行安装

skillhub install backend-event-stores-1776419953

下载

⬇ 下载 event-store v1.0.0(免费)

文件大小: 6.68 KB | 发布时间: 2026-4-17 20:09

v1.0.0 最新 2026-4-17 20:09
Initial release with comprehensive guidance on designing and implementing event stores for event-sourced systems.

- Covers core event store concepts, requirements, and architectural patterns
- Provides technology comparisons for EventStoreDB, PostgreSQL, Kafka, and DynamoDB
- Includes recommended event schema/envelope design and schema evolution strategies
- Supplies a sample PostgreSQL schema for events, snapshots, and subscription checkpoints
- Offers code for a basic async Python event store (append, read_stream, read_all)
- Explains projection patterns and their role in building CQRS read models

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部