|
| 1 | +# 3-Tier Data Handling System for StackMemory |
| 2 | + |
| 3 | +## Overview |
| 4 | +Implement intelligent data tier management that automatically moves frame data between local SQLite, hosted Redis cache, and hosted PostgreSQL based on usage patterns, age, and performance requirements. |
| 5 | + |
| 6 | +## Architecture |
| 7 | + |
| 8 | +### Tier 1: Local SQLite (Hot Data) |
| 9 | +- **Purpose**: Active session data, recent frames (<24h), high-frequency access |
| 10 | +- **Location**: `~/.stackmemory/frames.db` |
| 11 | +- **Characteristics**: Immediate access, no network latency |
| 12 | +- **Size Limit**: 500MB max |
| 13 | +- **Retention**: Last 1000 frames or 24 hours |
| 14 | + |
| 15 | +### Tier 2: Hosted Redis (Warm Cache) |
| 16 | +- **Purpose**: Recent project data (1-7 days), cross-session sharing |
| 17 | +- **Location**: Railway Redis instance or external Redis |
| 18 | +- **Characteristics**: Fast network access, shared between machines |
| 19 | +- **Size Limit**: 1GB total |
| 20 | +- **Retention**: 7 days with LRU eviction |
| 21 | +- **TTL**: 24 hours default, extended on access |
| 22 | + |
| 23 | +### Tier 3: Hosted PostgreSQL (Cold Storage) |
| 24 | +- **Purpose**: Long-term archival, analytics, full-text search |
| 25 | +- **Location**: Railway Postgres or external instance |
| 26 | +- **Characteristics**: Persistent, queryable, compressed |
| 27 | +- **Size Limit**: Unlimited (cost-optimized) |
| 28 | +- **Retention**: Indefinite with configurable policies |
| 29 | + |
| 30 | +## Data Flow |
| 31 | + |
| 32 | +``` |
| 33 | +New Frame → SQLite (Tier 1) |
| 34 | + ↓ (after 24h or 1000 frames) |
| 35 | +Redis (Tier 2) ← Access Pattern Analysis |
| 36 | + ↓ (after 7 days or size pressure) |
| 37 | +PostgreSQL (Tier 3) |
| 38 | +``` |
| 39 | + |
| 40 | +## Implementation Requirements |
| 41 | + |
| 42 | +### Core Components |
| 43 | + |
| 44 | +1. **TierManager Class** (`src/core/storage/tier-manager.ts`) |
| 45 | + - Orchestrates data movement between tiers |
| 46 | + - Implements eviction policies |
| 47 | + - Handles tier failure scenarios |
| 48 | + - Provides unified query interface |
| 49 | + |
| 50 | +2. **RedisAdapter Class** (`src/core/storage/redis-adapter.ts`) |
| 51 | + - Redis connection management with reconnection logic |
| 52 | + - Data serialization/deserialization |
| 53 | + - TTL management and renewal |
| 54 | + - Batch operations for efficiency |
| 55 | + |
| 56 | +3. **PostgresAdapter Class** (`src/core/storage/postgres-adapter.ts`) |
| 57 | + - Connection pooling and prepared statements |
| 58 | + - Schema migration management |
| 59 | + - Full-text search capabilities |
| 60 | + - Compression for large frame data |
| 61 | + |
| 62 | +4. **TierConfiguration** (`src/core/config/tier-config.ts`) |
| 63 | + - Environment-based tier selection |
| 64 | + - Connection string management |
| 65 | + - Policy configuration (TTL, size limits, retention) |
| 66 | + |
| 67 | +### Database Schemas |
| 68 | + |
| 69 | +#### SQLite (Existing + Extensions) |
| 70 | +```sql |
| 71 | +-- Extend existing frames table |
| 72 | +ALTER TABLE frames ADD COLUMN tier_status TEXT DEFAULT 'hot'; |
| 73 | +ALTER TABLE frames ADD COLUMN last_accessed INTEGER; |
| 74 | +ALTER TABLE frames ADD COLUMN access_count INTEGER DEFAULT 1; |
| 75 | + |
| 76 | +-- New tier tracking table |
| 77 | +CREATE TABLE tier_metadata ( |
| 78 | + frame_id TEXT PRIMARY KEY, |
| 79 | + tier_level INTEGER, -- 1=SQLite, 2=Redis, 3=Postgres |
| 80 | + promoted_at INTEGER, |
| 81 | + access_pattern TEXT, |
| 82 | + size_bytes INTEGER |
| 83 | +); |
| 84 | +``` |
| 85 | + |
| 86 | +#### Redis Schema |
| 87 | +``` |
| 88 | +Key Pattern: sm:frame:{frame_id} |
| 89 | +Value: Compressed JSON frame data |
| 90 | +TTL: 86400 seconds (24h), renewed on access |
| 91 | +Metadata: sm:meta:{frame_id} -> {tier_level, accessed_at, access_count} |
| 92 | +``` |
| 93 | + |
| 94 | +#### PostgreSQL Schema |
| 95 | +```sql |
| 96 | +CREATE TABLE frames_archive ( |
| 97 | + frame_id UUID PRIMARY KEY, |
| 98 | + session_id UUID, |
| 99 | + project_id TEXT, |
| 100 | + frame_data JSONB, |
| 101 | + frame_data_compressed BYTEA, |
| 102 | + created_at TIMESTAMP, |
| 103 | + archived_at TIMESTAMP DEFAULT NOW(), |
| 104 | + last_accessed TIMESTAMP, |
| 105 | + access_count INTEGER DEFAULT 0, |
| 106 | + size_bytes INTEGER, |
| 107 | + search_vector TSVECTOR |
| 108 | +); |
| 109 | + |
| 110 | +CREATE INDEX idx_frames_project ON frames_archive(project_id); |
| 111 | +CREATE INDEX idx_frames_session ON frames_archive(session_id); |
| 112 | +CREATE INDEX idx_frames_search ON frames_archive USING GIN(search_vector); |
| 113 | +CREATE INDEX idx_frames_created ON frames_archive(created_at); |
| 114 | +``` |
| 115 | + |
| 116 | +### API Interface |
| 117 | + |
| 118 | +#### New CLI Commands |
| 119 | +```bash |
| 120 | +stackmemory storage status # Show tier distribution |
| 121 | +stackmemory storage migrate --tier 2 # Force migration to Redis |
| 122 | +stackmemory storage migrate --tier 3 # Force migration to Postgres |
| 123 | +stackmemory storage retrieve <frame_id> # Get from any tier |
| 124 | +stackmemory storage compact # Run cleanup/compaction |
| 125 | +stackmemory storage stats # Performance statistics |
| 126 | +``` |
| 127 | + |
| 128 | +#### Configuration |
| 129 | +```typescript |
| 130 | +interface TierConfig { |
| 131 | + enabled: boolean; |
| 132 | + tiers: { |
| 133 | + sqlite: { |
| 134 | + maxSize: number; // 500MB |
| 135 | + maxFrames: number; // 1000 |
| 136 | + maxAge: number; // 24h in seconds |
| 137 | + }; |
| 138 | + redis: { |
| 139 | + enabled: boolean; |
| 140 | + connectionString: string; |
| 141 | + ttl: number; // 7 days |
| 142 | + maxSize: number; // 1GB |
| 143 | + }; |
| 144 | + postgres: { |
| 145 | + enabled: boolean; |
| 146 | + connectionString: string; |
| 147 | + compressionLevel: number; |
| 148 | + retentionDays?: number; // Optional cleanup |
| 149 | + }; |
| 150 | + }; |
| 151 | + policies: { |
| 152 | + promotionThreshold: number; // Access count to promote |
| 153 | + evictionStrategy: 'LRU' | 'LFU' | 'AGE'; |
| 154 | + backgroundSync: boolean; |
| 155 | + syncInterval: number; // seconds |
| 156 | + }; |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +### Performance Requirements |
| 161 | +- Local SQLite queries: <10ms |
| 162 | +- Redis operations: <50ms |
| 163 | +- PostgreSQL queries: <200ms |
| 164 | +- Background tier migration: <5% CPU overhead |
| 165 | +- Failed tier fallback: <100ms additional latency |
| 166 | + |
| 167 | +### Integration Points |
| 168 | + |
| 169 | +1. **Frame Manager Integration** |
| 170 | + - Modify `FrameManager` to use `TierManager` for all frame operations |
| 171 | + - Transparent tier selection based on frame age/access patterns |
| 172 | + - Automatic promotion of frequently accessed frames |
| 173 | + |
| 174 | +2. **Context Bridge Integration** |
| 175 | + - Update context retrieval to search across all tiers |
| 176 | + - Implement intelligent pre-fetching for related frames |
| 177 | + - Background warming of likely-needed frames |
| 178 | + |
| 179 | +3. **CLI Integration** |
| 180 | + - Add tier information to `stackmemory status` |
| 181 | + - Include storage metrics in monitoring commands |
| 182 | + - Configuration management through existing config system |
| 183 | + |
| 184 | +4. **Error Handling** |
| 185 | + - Graceful degradation when Redis/Postgres unavailable |
| 186 | + - Automatic retry with exponential backoff |
| 187 | + - Local-only mode fallback |
| 188 | + |
| 189 | +### Testing Strategy |
| 190 | + |
| 191 | +1. **Unit Tests** |
| 192 | + - Each adapter with mocked connections |
| 193 | + - Tier promotion/demotion logic |
| 194 | + - Configuration validation |
| 195 | + |
| 196 | +2. **Integration Tests** |
| 197 | + - End-to-end data flow testing |
| 198 | + - Failover scenarios (Redis down, Postgres down) |
| 199 | + - Performance benchmarks |
| 200 | + |
| 201 | +3. **Load Tests** |
| 202 | + - High-frequency frame creation |
| 203 | + - Concurrent access patterns |
| 204 | + - Memory usage under pressure |
| 205 | + |
| 206 | +## Success Criteria |
| 207 | + |
| 208 | +1. **Functionality** |
| 209 | + - ✅ Transparent tier management (user doesn't know which tier) |
| 210 | + - ✅ Data never lost (even with tier failures) |
| 211 | + - ✅ Query performance within SLA limits |
| 212 | + - ✅ Background migration doesn't impact user experience |
| 213 | + |
| 214 | +2. **Performance** |
| 215 | + - ✅ 90% of queries served from local SQLite |
| 216 | + - ✅ <100ms latency for cross-tier operations |
| 217 | + - ✅ Graceful handling of 10k+ frames |
| 218 | + |
| 219 | +3. **Reliability** |
| 220 | + - ✅ Survives Redis/Postgres outages |
| 221 | + - ✅ No data corruption during tier migrations |
| 222 | + - ✅ Self-healing tier synchronization |
| 223 | + |
| 224 | +## Implementation Phases |
| 225 | + |
| 226 | +### Phase 1: Foundation (Week 1) |
| 227 | +- TierManager and adapter interfaces |
| 228 | +- Basic SQLite extension |
| 229 | +- Configuration system |
| 230 | +- Unit tests |
| 231 | + |
| 232 | +### Phase 2: Redis Integration (Week 2) |
| 233 | +- RedisAdapter implementation |
| 234 | +- Tier 1 → Tier 2 migration |
| 235 | +- CLI commands |
| 236 | +- Integration tests |
| 237 | + |
| 238 | +### Phase 3: PostgreSQL Integration (Week 3) |
| 239 | +- PostgresAdapter implementation |
| 240 | +- Full 3-tier workflow |
| 241 | +- Search capabilities |
| 242 | +- Performance optimization |
| 243 | + |
| 244 | +### Phase 4: Production Ready (Week 4) |
| 245 | +- Error handling and resilience |
| 246 | +- Monitoring and metrics |
| 247 | +- Documentation |
| 248 | +- Load testing |
| 249 | + |
| 250 | +## Files to Create/Modify |
| 251 | + |
| 252 | +### New Files |
| 253 | +- `src/core/storage/tier-manager.ts` |
| 254 | +- `src/core/storage/redis-adapter.ts` |
| 255 | +- `src/core/storage/postgres-adapter.ts` |
| 256 | +- `src/core/config/tier-config.ts` |
| 257 | +- `src/cli/commands/storage.ts` |
| 258 | + |
| 259 | +### Modified Files |
| 260 | +- `src/core/context/frame-manager.ts` (integrate TierManager) |
| 261 | +- `src/core/context/context-bridge.ts` (cross-tier queries) |
| 262 | +- `src/core/config/config-manager.ts` (tier configuration) |
| 263 | +- `src/cli/index.ts` (add storage commands) |
| 264 | +- `package.json` (add Redis + Postgres dependencies) |
| 265 | + |
| 266 | +### Test Files |
| 267 | +- `src/core/storage/__tests__/tier-manager.test.ts` |
| 268 | +- `src/core/storage/__tests__/redis-adapter.test.ts` |
| 269 | +- `src/core/storage/__tests__/postgres-adapter.test.ts` |
| 270 | +- `tests/integration/tier-integration.test.ts` |
| 271 | + |
| 272 | +## Dependencies to Add |
| 273 | +```json |
| 274 | +{ |
| 275 | + "redis": "^4.6.0", |
| 276 | + "pg": "^8.11.0", |
| 277 | + "@types/pg": "^8.10.0", |
| 278 | + "compression": "^1.7.4", |
| 279 | + "uuid": "^9.0.0" |
| 280 | +} |
| 281 | +``` |
| 282 | + |
| 283 | +This feature represents a significant architectural enhancement that will improve StackMemory's scalability, performance, and multi-machine collaboration capabilities. |
0 commit comments