Event Store Design
Comprehensive guide to designing event stores for event-sourced applications.
Do not use this skill when
- The task is unrelated to event store design
- You need a different domain or tool outside this scope
Instructions
- Clarify goals, constraints, and required inputs.
- Apply relevant best practices and validate outcomes.
- Provide actionable steps and verification.
- If detailed examples are required, open
resources/implementation-playbook.md.
Use this skill when
- Designing event sourcing infrastructure
- Choosing between event store technologies
- Implementing custom event stores
- Optimizing event storage and retrieval
- Setting up event store schemas
- Planning for event store scaling
Core Concepts
1. Event Store Architecture
┌─────────────────────────────────────────────────────┐
│ Event Store │
├─────────────────────────────────────────────────────┤
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Stream 1 │ │ Stream 2 │ │ Stream 3 │ │
│ │ (Aggregate) │ │ (Aggregate) │ │ (Aggregate) │ │
│ ├─────────────┤ ├─────────────┤ ├─────────────┤ │
│ │ Event 1 │ │ Event 1 │ │ Event 1 │ │
│ │ Event 2 │ │ Event 2 │ │ Event 2 │ │
│ │ Event 3 │ │ ... │ │ Event 3 │ │
│ │ ... │ │ │ │ Event 4 │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
├─────────────────────────────────────────────────────┤
│ Global Position: 1 → 2 → 3 → 4 → 5 → 6 → ... │
└─────────────────────────────────────────────────────┘
2. Event Store Requirements
| Requirement |
Description |
| Append-only |
Events 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
| Technology |
Best For |
Limitations |
| EventStoreDB |
Pure event sourcing |
Single-purpose |
| PostgreSQL |
Existing Postgres stack |
Manual implementation |
| Kafka |
High-throughput streaming |
Not ideal for per-stream queries |
| DynamoDB |
Serverless, AWS-native |
Query limitations |
| Marten |
.NET ecosystems |
.NET specific |
Templates
Template 1: PostgreSQL Event Store Schema
-- Events table
CREATE TABLE events (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
stream_id VARCHAR(255) NOT NULL,
stream_type VARCHAR(255) NOT NULL,
event_type VARCHAR(255) NOT NULL,
event_data JSONB NOT NULL,
metadata JSONB DEFAULT '{}',
version BIGINT NOT NULL,
global_position BIGSERIAL,
created_at TIMESTAMPTZ DEFAULT NOW(),
CONSTRAINT unique_stream_version UNIQUE (stream_id, version)
);
-- Index for stream queries
CREATE INDEX idx_events_stream_id ON events(stream_id, version);
-- Index for global subscription
CREATE INDEX idx_events_global_position ON events(global_position);
-- Index for event type queries
CREATE INDEX idx_events_event_type ON events(event_type);
-- Index for time-based queries
CREATE INDEX idx_events_created_at ON events(created_at);
-- Snapshots table
CREATE TABLE snapshots (
stream_id VARCHAR(255) PRIMARY KEY,
stream_type VARCHAR(255) NOT NULL,
snapshot_data JSONB NOT NULL,
version BIGINT NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Subscriptions checkpoint table
CREATE TABLE subscription_checkpoints (
subscription_id VARCHAR(255) PRIMARY KEY,
last_position BIGINT NOT NULL DEFAULT 0,
updated_at TIMESTAMPTZ DEFAULT NOW()
);
Template 2: Python Event Store Implementation
from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import Any, Optional, List
from uuid import UUID, uuid4
import json
import asyncio
import asyncpg
@dataclass
class Event:
stream_id: str
event_type: str
data: dict
metadata: dict = field(default_factory=dict)
event_id: UUID = field(default_factory=uuid4)
version: Optional[int] = None
global_position: Optional[int] = None
created_at: datetime = field(default_factory=lambda: datetime.now(timezone.utc))
class EventStore:
def __init__(self, pool: asyncpg.Pool):
self.pool = pool
async def append_events(
self,
stream_id: str,
stream_type: str,
events: List[Event],
expected_version: Optional[int] = None
) -> List[Event]:
"""Append events to a stream with optimistic concurrency."""
async with self.pool
1---2name: event-store-design3description: Design and implement event stores for event-sourced systems. Use when building event sourcing infrastructure, choosing event store technologies, or implementing event persistence patterns. 4category: Creative & Media5source: antigravity6tags: [python, ai, template, design, aws, azure, rag, cro]7url: https://github.com/sickn33/antigravity-awesome-skills/tree/main/skills/event-store-design8metadata:9 source: https://skillmd.com/skills/ranbot-ai/event-store-design10---111213# Event Store Design1415Comprehensive guide to designing event stores for event-sourced applications.1617## Do not use this skill when1819- The task is unrelated to event store design20- You need a different domain or tool outside this scope2122## Instructions2324- Clarify goals, constraints, and required inputs.25- Apply relevant best practices and validate outcomes.26- Provide actionable steps and verification.27- If detailed examples are required, open `resources/implementation-playbook.md`.2829## Use this skill when3031- Designing event sourcing infrastructure32- Choosing between event store technologies33- Implementing custom event stores34- Optimizing event storage and retrieval35- Setting up event store schemas36- Planning for event store scaling3738## Core Concepts3940### 1. Event Store Architecture4142```43┌─────────────────────────────────────────────────────┐44│ Event Store │45├─────────────────────────────────────────────────────┤46│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │47│ │ Stream 1 │ │ Stream 2 │ │ Stream 3 │ │48│ │ (Aggregate) │ │ (Aggregate) │ │ (Aggregate) │ │49│ ├─────────────┤ ├─────────────┤ ├─────────────┤ │50│ │ Event 1 │ │ Event 1 │ │ Event 1 │ │51│ │ Event 2 │ │ Event 2 │ │ Event 2 │ │52│ │ Event 3 │ │ ... │ │ Event 3 │ │53│ │ ... │ │ │ │ Event 4 │ │54│ └─────────────┘ └─────────────┘ └─────────────┘ │55├─────────────────────────────────────────────────────┤56│ Global Position: 1 → 2 → 3 → 4 → 5 → 6 → ... │57└─────────────────────────────────────────────────────┘58```5960### 2. Event Store Requirements6162| Requirement | Description |63| ----------------- | ---------------------------------- |64| **Append-only** | Events are immutable, only appends |65| **Ordered** | Per-stream and global ordering |66| **Versioned** | Optimistic concurrency control |67| **Subscriptions** | Real-time event notifications |68| **Idempotent** | Handle duplicate writes safely |6970## Technology Comparison7172| Technology | Best For | Limitations |73| ---------------- | ------------------------- | -------------------------------- |74| **EventStoreDB** | Pure event sourcing | Single-purpose |75| **PostgreSQL** | Existing Postgres stack | Manual implementation |76| **Kafka** | High-throughput streaming | Not ideal for per-stream queries |77| **DynamoDB** | Serverless, AWS-native | Query limitations |78| **Marten** | .NET ecosystems | .NET specific |7980## Templates8182### Template 1: PostgreSQL Event Store Schema8384```sql85-- Events table86CREATE TABLE events (87 id UUID PRIMARY KEY DEFAULT gen_random_uuid(),88 stream_id VARCHAR(255) NOT NULL,89 stream_type VARCHAR(255) NOT NULL,90 event_type VARCHAR(255) NOT NULL,91 event_data JSONB NOT NULL,92 metadata JSONB DEFAULT '{}',93 version BIGINT NOT NULL,94 global_position BIGSERIAL,95 created_at TIMESTAMPTZ DEFAULT NOW(),9697 CONSTRAINT unique_stream_version UNIQUE (stream_id, version)98);99100-- Index for stream queries101CREATE INDEX idx_events_stream_id ON events(stream_id, version);102103-- Index for global subscription104CREATE INDEX idx_events_global_position ON events(global_position);105106-- Index for event type queries107CREATE INDEX idx_events_event_type ON events(event_type);108109-- Index for time-based queries110CREATE INDEX idx_events_created_at ON events(created_at);111112-- Snapshots table113CREATE TABLE snapshots (114 stream_id VARCHAR(255) PRIMARY KEY,115 stream_type VARCHAR(255) NOT NULL,116 snapshot_data JSONB NOT NULL,117 version BIGINT NOT NULL,118 created_at TIMESTAMPTZ DEFAULT NOW()119);120121-- Subscriptions checkpoint table122CREATE TABLE subscription_checkpoints (123 subscription_id VARCHAR(255) PRIMARY KEY,124 last_position BIGINT NOT NULL DEFAULT 0,125 updated_at TIMESTAMPTZ DEFAULT NOW()126);127```128129### Template 2: Python Event Store Implementation130131```python132from dataclasses import dataclass, field133from datetime import datetime, timezone134from typing import Any, Optional, List135from uuid import UUID, uuid4136import json137import asyncio138import asyncpg139140@dataclass141class Event:142 stream_id: str143 event_type: str144 data: dict145 metadata: dict = field(default_factory=dict)146 event_id: UUID = field(default_factory=uuid4)147 version: Optional[int] = None148 global_position: Optional[int] = None149 created_at: datetime = field(default_factory=lambda: datetime.now(timezone.utc))150151152class EventStore:153 def __init__(self, pool: asyncpg.Pool):154 self.pool = pool155156 async def append_events(157 self,158 stream_id: str,159 stream_type: str,160 events: List[Event],161 expected_version: Optional[int] = None162 ) -> List[Event]:163 """Append events to a stream with optimistic concurrency."""164 async with self.pool