System Architecture & Design
Ecosistema completo para organización y gestión de contenido con interfaces espaciales
🏗️ Storage System Architecture
Complete architectural overview of our intelligent storage system, covering core design principles, component interactions, and system philosophy.
🎯 Design Philosophy
Offline-First Architecture
- Repository L1 Cache: 1-2ms access (fastest application-level cache) ✨ NEW!
- RAM Cache Layer: 0.3ms access (instant)
- Local Storage Priority: All data operations happen locally first
- Optional Cloud Sync: Sync when needed, never blocks user
- Background Operations: Cloud sync happens asynchronously
- Repository Pattern: Complete CRUD + Query + Batch operations ✨ NEW!
Intelligent Automation
- Zero Configuration: Works perfectly out of the box
- Adaptive Learning: System optimizes based on usage patterns
- Automatic Decisions: Storage adapter selection, caching, redundancy
- Self-Healing: Automatic error recovery and failover
Performance First
- Sub-2ms Operations: Repository L1 cache hits in 1-2ms ✨ NEW!
- Sub-10ms Operations: 95% of operations complete in < 10ms
- 4-Level Caching: Repository L1 → GgStorage LRU → Micro-cache → Primary storage ✨ UPDATED!
- Batch Processing: Automatic operation batching (10x faster) ✨ ENHANCED!
- Smart Prefetching: Predictive data loading
🏛️ System Architecture (Updated November 4, 2025)
┌─────────────────────────────────────────────────────────────┐
│ APPLICATION LAYER │
│ Components │ Services │ Flash Persistence │ NGXS │
└────────────────────────────────┬────────────────────────────┘
│
┌────────────────────────────────▼────────────────────────────┐
│ REPOSITORY PATTERN LAYER ✨ NEW! │
│ BaseRepository / OfflineFirstRepository │
│ • L1 Cache (1-2ms) │
│ • CRUD + Query + Batch + Utilities │
│ • Cache Statistics (Signals) │
│ • Event Publishing │
└────────────────────────────────┬────────────────────────────┘
│
┌────────────────────────────────▼────────────────────────────┐
│ INTELLIGENT CORE │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Policy │ │ Adaptive │ │ Real-Time │ │
│ │ Engine │ │ Engine │ │ Monitor │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
└────────────────────────────────┬────────────────────────────┘
│
┌────────────────────────────────▼────────────────────────────┐
│ CACHING LAYER │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ RAM │ │ Micro │ │ Memory │ │ Query │ │
│ │ Cache │ │ Cache │ │ Cache │ │ Cache │ │
│ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │
└────────────────────────────────┬────────────────────────────┘
│
┌────────────────────────────────▼────────────────────────────┐
│ STORAGE LAYER │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ OPFS │ │ IndexedDB│ │localStorage│ │
│ │ (20GB) │ │ (1GB) │ │ (10MB) │ │
│ └──────────┘ └──────────┘ └──────────┘ │
└────────────────────────────────┬────────────────────────────┘
│
┌────────────────────────────────▼────────────────────────────┐
│ SYNC LAYER (Production: MongoDB) │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ MongoDB │ │ Socket.IO│ │ Yjs │ │
│ │ (Data) │ │ (Events) │ │ (CRDT) │ │
│ └──────────┘ └──────────┘ └──────────┘ │
│ ✅ Production Stack (NestJS + Mongoose + WebSocket) │
└─────────────────────────────────────────────────────────────┘
🧩 Core Components
1. GgStorage - Core Storage Engine
class GgStorage {
// Main API
async get<T>(key: string): Promise<T | null>
async put<T>(key: string, value: T): Promise<boolean>
async remove(key: string): Promise<boolean>
async list(prefix?: string): Promise<string[]>
async clear(prefix?: string): Promise<void>
// Intelligence
private policyEngine: PolicyEngine
private adaptiveEngine: AdaptiveEngine
private monitor: RealTimeMonitor
// Multi-layer caching
private ramCache: Map<string, any>
private microCache: MicroCache
// Storage adapters
private opfsAdapter?: OpfsAdapter
private indexedDBAdapter: IndexedDBAdapter
private localStorageAdapter: LocalStorageAdapter
}
Responsibilities:
- Primary storage interface
- Intelligent adapter selection
- Multi-level cache management
- Performance optimization
- Cross-tab synchronization
2. Policy Engine - Decision Making
class PolicyEngine {
// Analyze data and decide optimal storage
analyzeAndRoute(key: string, value: any): StorageDecision {
const size = this.estimateSize(value)
const pattern = this.analyzeKeyPattern(key)
const importance = this.determineImportance(pattern)
return {
primaryAdapter: this.selectPrimaryAdapter(size, importance),
redundancy: this.shouldRedundant(importance),
cacheStrategy: this.selectCacheStrategy(pattern),
ttl: this.calculateTTL(pattern)
}
}
}
Key Pattern Recognition:
user/*,auth/*,settings/*→ Critical (redundant)cache/*,temp/*→ Performance-optimizeddata/*→ Best available adapter- Custom patterns supported
3. Adaptive Engine - Learning & Optimization
class AdaptiveEngine {
// Learn from usage patterns
trackOperation(key: string, operation: string, duration: number): void
// Optimize based on learning
optimizeStrategy(): void {
const hotData = this.identifyHotData()
const coldData = this.identifyColdData()
// Move hot data to faster storage
this.promoteHotData(hotData)
// Move cold data to slower storage
this.demoteColdData(coldData)
}
}
Optimization Strategies:
- Access frequency tracking
- Performance pattern recognition
- Automatic data promotion/demotion
- Cache size optimization
4. Real-Time Monitor - Analytics
class RealTimeMonitor {
// Performance metrics
getMetrics(): PerformanceMetrics {
return {
cacheHitRate: this.calculateHitRate(),
avgDuration: this.calculateAvgDuration(),
operationCount: this.totalOperations,
adapterUsage: this.getAdapterDistribution(),
healthScore: this.calculateHealthScore()
}
}
// Real-time insights
generateInsights(): StorageInsights[]
}
5. Storage Registry - Backend Management
class StorageRegistry {
// Register storage backends
register(name: string, adapter: StorageAdapter): void
// Get optimal backend
getOptimal(requirements: Requirements): StorageAdapter
// Health monitoring
checkHealth(): Map<string, HealthStatus>
}
🔄 Data Flow
Write Operation
1. Application calls storage.put(key, value)
2. Policy Engine analyzes (key, value, size, pattern)
3. Decision: adapter, redundancy, cache strategy
4. Write to RAM cache (instant)
5. Write to micro-cache if critical
6. Write to primary adapter (async)
7. Write to redundant adapter if needed (async)
8. Broadcast to other tabs via BroadcastChannel
9. Sync to cloud if configured (async, queued)
Read Operation
1. Application calls storage.get(key)
2. Check RAM cache → if hit, return (0.3ms)
3. Check micro-cache → if hit, return (~1ms)
4. Check memory cache → if hit, return (~2ms)
5. Query primary adapter → return and cache (~5-20ms)
6. On miss, check redundant adapters
7. Update caches for future access
8. Track access for adaptive optimization
🎯 Storage Strategies
Speed Strategy
{
priority: 'performance',
cache: 'aggressive',
redundancy: 'minimal',
sync: 'deferred'
}
- Maximum RAM cache
- Queued writes to storage
- Minimal redundancy
- Best for: Games, real-time apps
Balanced Strategy (Default)
{
priority: 'balanced',
cache: 'moderate',
redundancy: 'smart',
sync: 'immediate'
}
- Optimal cache size
- Immediate critical writes
- Smart redundancy
- Best for: Most applications
Durable Strategy
{
priority: 'reliability',
cache: 'conservative',
redundancy: 'maximum',
sync: 'synchronous'
}
- Triple redundancy
- Synchronous writes
- Maximum safety
- Best for: Financial, medical apps
🧠 Intelligent Features
Automatic Adapter Selection
// System decides based on data characteristics
storage.put('user/profile', largeObject)
// → OPFS (large data, high importance)
storage.put('cache/api/users', apiResponse)
// → IndexedDB (structured, medium size)
storage.put('temp/upload', tempData)
// → OPFS (large, temporary)
Smart Redundancy
// Critical data automatically mirrored
storage.put('auth/session/token', token)
// Writes to: IndexedDB + localStorage + RAM cache
// Cache data, single storage
storage.put('cache/api/response', data)
// Writes to: IndexedDB only
Adaptive Caching
// System learns: user/settings accessed frequently
// Promotes to RAM cache automatically
// Next access: 0.3ms instead of 20ms
📊 Performance Characteristics
Operation Latency
| Operation | RAM Cache | Micro Cache | IndexedDB | OPFS |
|---|---|---|---|---|
| Read | 0.3ms | ~1ms | 2-5ms | 5-20ms |
| Write | 0.5ms | ~2ms | 5-10ms | 10-30ms |
| Delete | 0.3ms | ~1ms | 2-5ms | 5-15ms |
Storage Capacity
| Storage | Capacity | Speed | Persistence | Use Case |
|---|---|---|---|---|
| RAM | ~50MB | 0.3ms | Session | Hot data |
| Micro | ~10MB | ~1ms | Permanent | Critical |
| IndexedDB | ~1GB | 5ms | Permanent | Structured |
| OPFS | ~20GB | 15ms | Permanent | Large files |
🔌 Extension Points
Custom Storage Adapters
interface StorageAdapter {
get(key: string): Promise<any>
put(key: string, value: any): Promise<boolean>
remove(key: string): Promise<boolean>
list(prefix?: string): Promise<string[]>
clear(): Promise<void>
}
// Register custom adapter
registry.register('my-custom', new MyCustomAdapter())
Custom Policy Rules
policyEngine.addRule({
pattern: /^myapp\/critical\/.*/,
adapter: 'indexedDB',
redundancy: true,
ttl: Infinity
})
🛡️ Reliability Features
Automatic Failover
Primary adapter fails → Fallback to redundant adapter
All adapters fail → In-memory fallback
Recovery → Auto-resync from cloud
Data Integrity
- Checksums for large data
- Transaction support where available
- Automatic corruption detection
- Self-healing capabilities
Cross-Tab Coordination
- BroadcastChannel for real-time sync
- Namespace isolation
- Conflict-free updates
- Leader election for coordination
🔗 Related Architecture
- Intelligent Storage - AI features
- Adapters & Policy - Adapter system
- Performance - Performance design
- Events & Reactivity - Event system
Status: This architecture delivers world-class performance with zero configuration required.