storage-sync

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Storage Sync

存储同步

Synchronize memories between Turso (durable) and redb (cache) storage layers.
在Turso(持久化存储)与redb(缓存存储)层之间同步数据。

When to Sync

同步时机

  1. On startup - After system initialization
  2. Periodic - Scheduled background sync
  3. Cache staleness - When redb appears outdated
  4. Recovery - After storage failures
  5. Manual trigger - When explicitly requested
  1. 启动时 - 系统初始化完成后
  2. 定期同步 - 后台定时同步
  3. 缓存过期 - 当redb缓存数据过时
  4. 故障恢复 - 存储故障修复后
  5. 手动触发 - 当明确发起同步请求时

Sync Process

同步流程

  1. Check connection health:
    rust
    turso_client.ping().await?;
    redb_env.check_integrity()?;
  2. Fetch latest from Turso:
    rust
    let episodes = turso_client
        .query("SELECT * FROM episodes ORDER BY timestamp DESC LIMIT ?")
        .bind(max_episodes_cache)
        .await?;
  3. Update redb cache:
    rust
    tokio::task::spawn_blocking(move || {
        let write_txn = redb.begin_write()?;
        let mut table = write_txn.open_table(EPISODES_TABLE)?;
        for episode in episodes {
            table.insert(episode.id.as_bytes(), episode.to_bytes())?;
        }
        write_txn.commit()?;
    });
  4. Sync patterns and embeddings if enabled
  1. 检查连接健康状态:
    rust
    turso_client.ping().await?;
    redb_env.check_integrity()?;
  2. 从Turso获取最新数据:
    rust
    let episodes = turso_client
        .query("SELECT * FROM episodes ORDER BY timestamp DESC LIMIT ?")
        .bind(max_episodes_cache)
        .await?;
  3. 更新redb缓存:
    rust
    tokio::task::spawn_blocking(move || {
        let write_txn = redb.begin_write()?;
        let mut table = write_txn.open_table(EPISODES_TABLE)?;
        for episode in episodes {
            table.insert(episode.id.as_bytes(), episode.to_bytes())?;
        }
        write_txn.commit()?;
    });
  4. 同步模式与嵌入数据(若启用)

Configuration

配置

rust
pub struct SyncConfig {
    pub max_episodes_cache: usize,  // Default: 1000
    pub batch_size: usize,          // Default: 100
    pub sync_patterns: bool,        // Default: true
    pub sync_embeddings: bool,      // Default: true
}
rust
pub struct SyncConfig {
    pub max_episodes_cache: usize,  // 默认值: 1000
    pub batch_size: usize,          // 默认值: 100
    pub sync_patterns: bool,        // 默认值: true
    pub sync_embeddings: bool,      // 默认值: true
}

Error Handling

错误处理

ErrorHandling
Turso unavailableSkip sync, log warning, retry later
redb corruptionAttempt repair, rebuild from Turso
Partial syncTrack progress, resume from last point
错误类型处理方式
Turso不可用跳过同步,记录警告,稍后重试
redb数据损坏尝试修复,从Turso重建缓存
部分同步失败跟踪同步进度,从断点恢复

Performance Tips

性能优化建议

  • Batch small operations
  • Incremental sync (only changes)
  • Parallel fetch with Tokio
  • Write-ahead preparation
  • 批量处理小型操作
  • 增量同步(仅同步变更数据)
  • 使用Tokio并行获取数据
  • 提前准备写入操作

Validation

验证步骤

After sync, verify:
  • Episode count matches
  • Latest episodes present
  • Pattern counts consistent
  • No orphaned embeddings
同步完成后,需验证:
  • 数据记录数量匹配
  • 最新数据已同步
  • 模式数量一致
  • 无孤立的嵌入数据

Troubleshooting

故障排除

IssueSolution
Slow syncsReduce cache size, increase batch
redb lock errorsUse dedicated write task
Memory pressureStream large results, smaller batches
问题解决方案
同步速度慢减小缓存容量,增大批量处理大小
redb锁错误使用独立的写入任务
内存压力大流式处理大结果集,减小批量大小