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

Intelligent Automation

Performance First


🏛️ 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:

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:

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:

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'
}

Balanced Strategy (Default)

{
  priority: 'balanced',
  cache: 'moderate',
  redundancy: 'smart',
  sync: 'immediate'
}

Durable Strategy

{
  priority: 'reliability',
  cache: 'conservative',
  redundancy: 'maximum',
  sync: 'synchronous'
}

🧠 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

Cross-Tab Coordination


🔗 Related Architecture


Status: This architecture delivers world-class performance with zero configuration required.