Skip to content

WRITE-WAL-001: WAL sem group commit — fsync a cada 4 writes limita throughput #362

@ElioNeto

Description

@ElioNeto

🟡 Médio | Performance | WAL

Problema

O WAL usa batch sync a cada WAL_SYNC_INTERVAL (4) writes. Embora melhor que fsync por write, ainda está longe do throughput máximo possível. Group commit (acumular writes de múltiplos clientes antes de fsync) melhoraria throughput em ~4-10x.

Impacto

  • Write throughput limitado pela frequência de fsync
  • Em SSD NVMe com 100μs por fsync, máximo teórico = 4/0.0001 = 40.000 ops/s
  • Com group commit (intervalo de 1ms): até 1000x mais writes por fsync

Evidência

src/storage/wal.rs:116-125: WAL_SYNC_INTERVAL = 4
src/storage/wal.rs:214-221: flush + sync_all a cada N writes

Correção recomendada

Group commit com timer:

struct GroupCommit {
    pending: Arc<Mutex<Vec<PendingWrite>>>,
    notifier: Arc<Condvar>,
}

impl GroupCommit {
    fn submit(&self, record: LogRecord) -> Result<()> {
        let mut batch = self.pending.lock();
        batch.push(PendingWrite { record, result: None });
        if batch.len() >= GROUP_COMMIT_BATCH_SIZE {
            self.flush_batch(&mut batch);
        } else {
            // Timer-based: flush após 1ms se não encher antes
            thread::sleep(Duration::from_millis(1));
            if !batch.is_empty() {
                self.flush_batch(&mut batch);
            }
        }
    }

    fn flush_batch(&self, batch: &mut Vec<PendingWrite>) {
        // Serializar todos, escrever no WAL, fsync uma vez
        let wal = self.wal.lock();
        for write in batch.iter() {
            wal.write_raw(&serialize(&write.record));
        }
        wal.sync_all()?; // Um fsync para N writes
        batch.clear();
    }
}

Parâmetros configuráveis:

  • GROUP_COMMIT_BATCH_SIZE: default 64 writes
  • GROUP_COMMIT_INTERVAL_MS: default 1ms

Esforço: Alto (12h)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions