Server Storage

JSON File Storage System

Storage Model

The server uses a simple JSON file-based storage system for chat persistence.

src/server/
├── chatSyncApi.js     # HTTP API server
├── chatStore.js       # Storage operations
└── chatStore.json     # Data file (created automatically)

File Structure

[
  {
    "id": "chat-123",
    "title": "Example Chat",
    "messages": [
      {
        "id": "msg-456",
        "role": "user",
        "content": "Hello",
        "timestamp": "2024-01-15T10:30:00.000Z"
      }
    ],
    "createdAt": "2024-01-15T10:00:00.000Z",
    "updatedAt": "2024-01-15T10:30:00.000Z",
    "lastModified": "2024-01-15T10:30:00.000Z",
    "model": "gpt-4",
    "provider": "openai",
    "isArchived": false
  }
]

Storage Operations

File I/O Implementation

Read Operations

Write Operations

Atomic Operations

  • Read-Modify-Write: All operations follow atomic pattern

  • File Locking: Synchronous operations prevent concurrent access

  • Error Recovery: Corrupted files default to empty state

Core Storage Functions

getAllChats()

  • Returns complete chat array

  • Handles missing file gracefully

  • Recovers from JSON corruption

addOrUpdateChats(newChats)

  • Merge Strategy: ID-based deduplication

  • Conflict Resolution: Last-write-wins (no timestamp comparison)

  • Batch Operations: Processes multiple chats in single write

Data Consistency Model

Consistency Guarantees

  • Atomic Writes: Complete file replacement ensures consistency

  • Read-After-Write: Immediate consistency for single server

  • No Transactions: Simple model without ACID guarantees

Conflict Resolution

Resolution Strategy:

  • Incoming chat completely replaces existing chat

  • No field-level merging or timestamp comparison

  • Client is authoritative for chat state

Concurrency Handling

  • Single Process: No concurrent access protection

  • Synchronous I/O: Blocking operations prevent race conditions

  • File System: OS-level file locking provides basic safety

File Management

File Path Resolution

File Creation

Error Handling

Performance Characteristics

Read Performance

  • Cold Read: Disk I/O + JSON parsing

  • File Size Impact: Linear with number of chats

  • Typical Latency: <10ms for small files (<1MB)

  • Memory Usage: Entire file loaded into memory

Write Performance

  • Full File Rewrite: Always writes complete JSON

  • No Incremental Updates: Simple but inefficient for large datasets

  • Formatting Overhead: Pretty-printed JSON (2-space indentation)

  • Atomic Safety: Prevents partial writes

Storage Efficiency

  • JSON Overhead: ~20-30% storage overhead vs binary

  • Pretty Printing: Additional ~15% for readability

  • Compression: Not implemented (could reduce by ~70%)

Scalability Limits

Current Limitations

  • File Size: Single file grows with chat count

  • Memory Usage: Entire dataset loaded for each operation

  • Concurrent Access: No multi-process support

  • Backup/Recovery: Manual file management required

Scaling Thresholds

  • Small Scale: <1,000 chats, <10MB file - Good performance

  • Medium Scale: <10,000 chats, <100MB file - Acceptable performance

  • Large Scale: >10,000 chats, >100MB file - Performance degradation

Performance Degradation

Migration Considerations

Database Migration Path

For production scaling, consider migration to:

SQL Database (PostgreSQL)

NoSQL Database (MongoDB)

Key-Value Store (Redis)

Backup and Recovery

Manual Backup

Automated Backup

Recovery Procedures

  1. Corruption Recovery: Delete corrupted file, restart with empty array

  2. Backup Restoration: Copy backup file to chatStore.json

  3. Data Loss Prevention: Regular backups before major operations

Security Considerations

File System Security

  • File Permissions: Restrict access to server process user

  • Directory Security: Secure server directory permissions

  • Backup Security: Encrypt backup files for sensitive data

Data Protection

Access Control

  • No Authentication: Current implementation has no access control

  • File-Level Security: Relies on OS file permissions

  • Network Security: HTTPS recommended for data in transit

Monitoring and Observability

File System Monitoring

Operation Logging

Health Checks

  • File Existence: Verify store file exists and is readable

  • JSON Validity: Parse test to ensure file isn't corrupted

  • Disk Space: Monitor available storage space

  • Performance: Track read/write operation latency

Last updated