If you want to update this page or add new content, please submit a pull request to the Homepage.
Architecture Patterns
When integrating Yorkie into your application, you may need to decide how to manage document lifecycle and data persistence, especially when you already have an existing database. This guide presents three recommended architectural patterns based on different Source of Truth (SoT) strategies.
Understanding these patterns will help you design the optimal architecture for your use case, whether you want to maintain real-time collaborative state separately from persistent storage or use Yorkie as the primary data store.
Before You Begin
If you're new to Yorkie, we recommend:
- Complete the Getting Started guide first
- Understand basic Client and Document concepts
- Familiarize yourself with key terms in the Glossary
- Then return here to choose your architecture pattern
Pattern A: External DB as SoT with Server-Managed Lifecycle
In this pattern, your existing database serves as the Source of Truth, while your server manages the document lifecycle through Yorkie's Admin API. This approach provides centralized control over document creation and deletion, making it suitable for applications that require strict server-side governance.
Key Characteristics:
- Server creates and deletes documents via Admin API
- Clients edit documents directly through Yorkie SDK during collaboration
- Explicit "Save" action persists content to your database
- Server maintains full control over document lifecycle
Security Note: When using Admin API for server-managed lifecycle, secure your API endpoints properly. See Auth Webhook for authentication setup.
Document Creation Flow:
- Client requests document creation from your server
- Server creates a new document through Yorkie Admin API
- Server stores document metadata in your database
- Server returns document key to client
- Client attaches to the document using the provided key
Document Editing Flow:
- Clients attach to the document via Yorkie SDK
- Real-time collaborative editing happens through Yorkie
- Server is not involved during active collaboration
- All changes are automatically synchronized between clients
Document Saving Flow:
- User clicks "Save" button
- Client sends document content to your server
- Server persists the content to your database
- Server returns save confirmation
Document Deletion Flow:
- Client requests document deletion from your server
- Server removes document via Yorkie Admin API
- Server deletes document from your database
- Server returns deletion confirmation
When to Use This Pattern:
- You need strict server-side control over document lifecycle
- Your application requires explicit save/persist actions
- You want to maintain clear separation between real-time and persistent state
- You have existing database infrastructure you want to preserve
Pattern B: External DB as SoT with Client-Only Lifecycle Management
This pattern also uses your external database as the Source of Truth, but delegates document lifecycle management to the client side. This serverless approach simplifies your backend architecture and leverages Yorkie's auto-cleanup features.
Key Characteristics:
- Server only provides document keys and initial content
- Clients create and manage documents independently
- Automatic document cleanup via
RemoveOnDetachproject setting (configure via CLI or Dashboard) - Explicit "Save" action persists content to your database
Document Creation Flow:
- Client requests document key and initial content from your server
- Client creates document locally with received key
- Client attaches document with initial content using the
initialRootoption:
1const doc = new yorkie.Document(key); // Key received from server2await client.attach(doc, {3 initialRoot: contents // Initial content from server4});
Document Editing Flow:
- Client requests document key from your server
- Client creates and attaches to document:
1const doc = new yorkie.Document(key); // Key received from server2await client.attach(doc);
- Real-time collaborative editing happens through Yorkie
- Server is not involved during active collaboration
Editing Completion Flow:
- Client finishes editing and detaches from document
- With project-level
RemoveOnDetachoption enabled, document is automatically removed when no clients are attached - This automatic cleanup reduces server management overhead
Document Saving Flow:
- User clicks "Save" button
- Client sends document content to your server
- Server persists the content to your database
- Server returns save confirmation
When to Use This Pattern:
- You want to minimize server-side complexity
- Your application benefits from client-driven architecture
- You need automatic document cleanup
- You still require explicit save actions but want simpler lifecycle management
Pattern C: Yorkie as Source of Truth
In this pattern, Yorkie serves as the primary Source of Truth for your document content. Your database only stores document metadata (e.g., title, owner, created date) while all content lives in Yorkie. This approach provides the most seamless real-time experience.
Key Characteristics:
- Yorkie stores and manages all document content
- Your database stores only document metadata
- No explicit "Save" button needed
- All changes are automatically persisted in real-time
- Simplified architecture with single source of truth
How It Works: Yorkie uses CRDT (Conflict-free Replicated Data Types) to ensure automatic conflict resolution, enabling seamless real-time collaboration without manual conflict handling.
Document Editing Flow:
- Client attaches to document via Yorkie SDK
- All edits are automatically synchronized and persisted
- No manual save action required
- Changes are immediately available to all connected clients
Document Deletion Flow:
- Client or server removes document via Yorkie Admin API
- Server removes document metadata from your database
- Document content is automatically removed from Yorkie
When to Use This Pattern:
- You're building a new application without legacy database constraints
- You want true real-time collaboration without manual saves
- Document content naturally lives in Yorkie (e.g., collaborative editors, whiteboards)
- You prefer simplified architecture with automatic persistence
- Your metadata storage needs are minimal
Choosing the Right Pattern
Consider these factors when selecting an architecture pattern:
| Factor | Pattern A | Pattern B | Pattern C |
|---|---|---|---|
| Server Control | High | Low | Medium |
| Implementation Complexity | Medium | Low | Low |
| Save Action Required | Yes | Yes | No |
| Best For | Enterprise apps with strict governance | Serverless architectures | Real-time-first applications |
| Database Load | Controlled by saves | Controlled by saves | Minimal (metadata only) |
Start with Pattern C if you're building a new collaborative application from scratch. Choose Pattern A if you need tight server-side control and have existing database infrastructure. Choose Pattern B if you want a simpler client-driven approach while maintaining external persistence.
See It In Action
Explore real-world examples implementing these patterns:
- Simultaneous Cursors Example - Pattern C with real-time presence
- ToDo MVC Example - Real-time collaboration
- Quill Editor Example - Collaborative text editing
- View All Examples
Related Documentation
For more information about Yorkie's API and features:
- JS SDK Documentation - Client and Document API reference
- Admin API - Admin API for server-side management
- Security Configuration - Auth Webhook and access control
- CLI - Command-line tools for project management
- Glossary - Key terminology and concepts