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
from typing import Any, Optional, List
from uuid import UUID, uuid4
import json
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=datetime.utcnow)
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.acquire() as conn:
async with c
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/sickn33/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 datetime134from typing import Any, Optional, List135from uuid import UUID, uuid4136import json137import asyncpg138139@dataclass140class Event:141 stream_id: str142 event_type: str143 data: dict144 metadata: dict = field(default_factory=dict)145 event_id: UUID = field(default_factory=uuid4)146 version: Optional[int] = None147 global_position: Optional[int] = None148 created_at: datetime = field(default_factory=datetime.utcnow)149150151class EventStore:152 def __init__(self, pool: asyncpg.Pool):153 self.pool = pool154155 async def append_events(156 self,157 stream_id: str,158 stream_type: str,159 events: List[Event],160 expected_version: Optional[int] = None161 ) -> List[Event]:162 """Append events to a stream with optimistic concurrency."""163 async with self.pool.acquire() as conn:164 async with c