System Architecture
Sorcia is built on a modern, scalable stack designed for enterprise-grade knowledge management.Core Components
1. Frontend (Next.js)
- Framework: Next.js 16 with App Router
- UI: React 19 + Tailwind CSS + shadcn/ui
- State: React Query for server state
- Auth: Supabase Auth with JWT
2. Backend (API Routes)
- Runtime: Next.js serverless functions
- Authentication: JWT-based with Supabase
- Rate Limiting: Per-user and per-organization
- Caching: Redis for query results
3. Database (Supabase/PostgreSQL)
- Primary DB: PostgreSQL 14+
- Vector Extension: pgvector for embeddings
- Row-Level Security: Organization isolation
- Real-time: Supabase Realtime for live updates
4. AI Layer
- Embeddings: OpenAI text-embedding-3-small (1536 dimensions)
- LLM: GPT-4 Turbo for answer generation
- Fallback: Claude 3.5 Sonnet (configurable)
- Provider: Vercel AI SDK (provider-agnostic)
5. Integration Platform (Nango)
- OAuth Flows: Centralized authentication
- API Connections: Unified interface to 100+ apps
- Webhook Handling: Real-time sync triggers
- Credential Management: Encrypted storage
6. Background Jobs (Inngest)
- Document Sync: Scheduled and event-driven
- Embedding Generation: Async processing
- Webhook Processing: Reliable delivery
- Retry Logic: Exponential backoff
Data Flow
Document Ingestion
Processing
Documents are:
- Chunked into smaller segments (500 tokens)
- Cleaned and normalized
- Metadata extracted
Query Processing
Hybrid Search
Three parallel searches:
- Vector similarity (semantic)
- Full-text search (keywords)
- Metadata filtering
Search Technology
Hybrid Search
Sorcia combines three search methods:1. Vector Similarity Search
- Understanding context and meaning
- Finding synonyms and related concepts
- Language-agnostic
2. Full-Text Search
- Exact term matching
- Technical jargon and codes
- Named entities
3. Metadata Filtering
- Precise filtering
- Time-based queries
- Source-specific searches
Ranking Algorithm
Results are scored using:- Vector similarity: 0-1 (cosine similarity)
- Text relevance: ts_rank score
- Recency boost: Newer documents score higher
- Permission factor: 1.0 if full access, 0.5 if restricted
Permission System
Multi-Layer Security
Layer 1: Row-Level Security (RLS)
Database-level isolation:Layer 2: Access Control Lists (ACLs)
Document-level permissions:Layer 3: Source Permissions
Mirrors original system permissions:- Slack: Public channel? Is user a member?
- Google Drive: Does user have view access?
- Notion: Is page shared with user?
Permission Sync
Permissions are synced:- Real-time: Via webhooks when permissions change
- Scheduled: Daily full reconciliation
- On-demand: When access denied error occurs
Embedding Strategy
Document Chunking
Large documents are split into chunks:- Embedding models have token limits (8,191 for text-embedding-3-small)
- Smaller chunks = more precise matching
- Better citation granularity
Embedding Model
Current:text-embedding-3-small
- Dimensions: 1536
- Cost: $0.02 / 1M tokens
- Performance: ~62% on MTEB benchmark
Vector Index
Uses IVFFlat algorithm:- lists: Number of clusters (100 for less than 1M documents)
- Distance: Cosine similarity
- Recall: ~95% at 100 lists
Scalability
Performance Targets
| Metric | Target | Current |
|---|---|---|
| Query latency | Under 2s p95 | 1.2s p95 |
| Sync latency | Under 5min | 2-3min |
| Concurrent users | 1,000+ | Tested to 2,000 |
| Documents | 10M+ | Tested to 5M |
Optimization Strategies
Caching
Caching
- Query results cached for 5-10 minutes
- Embeddings cached permanently
- API responses cached at CDN
- Invalidation on document updates
Indexing
Indexing
- IVFFlat for approximate search (95% recall, 10x faster)
- Partial indexes on frequently filtered columns
- Composite indexes for common query patterns
Batching
Batching
- Batch embed 100 chunks at once
- Batch database inserts
- Deduplicate concurrent requests
Load Balancing
Load Balancing
- Serverless functions auto-scale
- Read replicas for queries
- Separate pools for sync vs query
Data Privacy
Encryption
- At rest: AES-256 (Supabase default)
- In transit: TLS 1.3
- Embeddings: Encrypted in database
- API keys: Hashed with bcrypt
Data Retention
- Documents: Synced from source (user controls retention)
- Query logs: 90 days (configurable)
- Audit logs: 1 year
- Embeddings: Until document deleted
Compliance
- GDPR: Data export, right to erasure
- SOC 2: Type II certified
- HIPAA: BAA available (Enterprise)
- ISO 27001: Information security