Projection Patterns

作者 wshobson46891e7e60da无许可证收录于 2026年10月8日更新于 2026年10月8日

Build read models and projections from event streams. Use when implementing CQRS read sides, building materialized views, or optimizing query performance in event-sourced systems.

AI 生成的概览

指导如何在事件溯源系统中从事件流构建 CQRS 读模型与投影。

功能
说明投影架构,展示事件如何从事件存储经由投影器写入读模型数据库。介绍实时、追赶、持久化和内联等投影类型,并列出关于幂等性、检查点、顺序和重建的最佳实践与常见误区。指向随附的参考文件以获取模板和完整示例。
适用场景
适用于实现 CQRS 读端、从事件构建物化视图或搜索索引、优化查询性能,或跨事件流聚合数据时。也适用于在事件溯源数据上设计实时仪表盘。不适用于非事件溯源系统。
运行要求
无需脚本或工具;这是纯说明型技能,并引用随附的详细文档。

Projection Patterns

Comprehensive guide to building projections and read models for event-sourced systems.

When to Use This Skill

  • Building CQRS read models
  • Creating materialized views from events
  • Optimizing query performance
  • Implementing real-time dashboards
  • Building search indexes from events
  • Aggregating data across streams

Core Concepts

1. Projection Architecture

┌─────────────┐     ┌─────────────┐     ┌─────────────┐│ Event Store │────►│ Projector   │────►│ Read Model  ││             │     │             │     │ (Database)  ││ ┌─────────┐ │     │ ┌─────────┐ │     │ ┌─────────┐ ││ │ Events  │ │     │ │ Handler │ │     │ │ Tables  │ ││ └─────────┘ │     │ │ Logic   │ │     │ │ Views   │ ││             │     │ └─────────┘ │     │ │ Cache   │ │└─────────────┘     └─────────────┘     └─────────────┘

2. Projection Types

TypeDescriptionUse Case
LiveReal-time from subscriptionCurrent state queries
CatchupProcess historical eventsRebuilding read models
PersistentStores checkpointResume after restart
InlineSame transaction as writeStrong consistency

Templates and detailed worked examples

Full template library and detailed worked examples live in references/details.md. Read that file when you need the concrete templates.

Best Practices

Do's

  • Make projections idempotent - Safe to replay
  • Use transactions - For multi-table updates
  • Store checkpoints - Resume after failures
  • Monitor lag - Alert on projection delays
  • Plan for rebuilds - Design for reconstruction

Don'ts

  • Don't couple projections - Each is independent
  • Don't skip error handling - Log and alert on failures
  • Don't ignore ordering - Events must be processed in order
  • Don't over-normalize - Denormalize for query patterns

来源与署名

来源:wshobson/agents位于plugins/backend-development/skills/projection-patterns提交46891e7

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架