# Patterns

> Apply Cloudflare reference architecture patterns for CRUD SaaS, collaboration, async exports, AI assistants, RAG, media, webhook ingestion, and migration. Use to turn requirements into an implementation pattern and primitive map.

- Skill: `zllovesuki/patterns` (Agent Skill)
- Install (CLI): `npx skillmds@latest add zllovesuki/patterns`
- Raw SKILL.md: https://api.skillmd.com/api/skills/zllovesuki/patterns/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: AI & ML
- Author: zllovesuki (https://skillmd.com/u/zllovesuki)
- Updated: 2026-09-22
- Page: https://skillmd.com/skills/zllovesuki/patterns

---

# Patterns

Use this skill when a user describes an application and needs a Cloudflare implementation shape.

## Pattern: CRUD SaaS

```text
Worker: API, auth, SSR/static assets
D1: tenants, users, records, permissions
R2: file uploads
KV: cached public/config reads
Queue: email/webhook/export jobs
Observability: request IDs and tenant IDs
```

Add Durable Objects only when a specific entity needs serialized coordination.

## Pattern: collaborative document or chat

```text
Worker: auth and WebSocket upgrade route
Durable Object per room/document: live state, message ordering, presence
D1: metadata, membership, audit/history summaries
R2: snapshots, exports, attachments
Queue: indexing, notifications, compaction
```

Key by `tenant:{tenantId}:doc:{docId}` or similar.

## Pattern: async export/report

```text
Worker: validate request, create job record, enqueue/start workflow
Workflow: durable ordered steps, retries, progress
D1: job status and metadata
R2: generated report
Queue: fan-out chunks when needed
```

Return HTTP 202 with `jobId`; expose a status endpoint.

## Pattern: RAG assistant

```text
Worker: upload/query APIs and auth
R2: original documents and parsed chunks
D1: document metadata, ACL, ingestion status
Workflow/Queue: parse, chunk, embed, index
Vectorize or AI Search: retrieval index
Workers AI / AI Gateway: embeddings and answer generation
```

Ensure retrieval filters enforce tenant/user authorization.

## Pattern: media or large file app

```text
Worker: auth, upload/download routing
R2: original and transformed media
D1: metadata, ownership, processing state
Queue/Workflow: transformations, notifications
Container: only if native/heavy processing is required
Realtime/WebRTC: only for live media sessions
```

## Pattern: webhook ingestion

```text
Worker: verify signature, store minimal event, enqueue
Queue: process retries/idempotency
D1: event ledger and domain records
Durable Object: only if events must serialize by account/entity
```

## Pattern selection checklist

- [ ] What is the user-visible latency budget?
- [ ] Which operations need strong consistency?
- [ ] Which data can be stale or cached?
- [ ] Which work should happen after response?
- [ ] What is the tenant isolation boundary?
- [ ] Which primitive could become a hot key?
- [ ] What will operators inspect during an incident?

## Anti-patterns

- Pattern chosen because it is familiar from another cloud rather than because it fits Cloudflare.
- No explicit latency budget.
- State split across primitives without source-of-truth rules.
- Realtime, background jobs, and relational writes all hidden in one Worker route.

