massa icon indicating copy to clipboard operation
massa copied to clipboard

Ledger RocksDB batch write in single transaction

Open aoudiamoncef opened this issue 2 years ago • 3 comments

Describe the bug WriteBatch default behavior doesn't use single transaction. It provides atomicity by default when writing multiple keys.

To Reproduce Launch a batch writes with wrong operation.

Expected behavior WriteBatch should be replaced by WriteBatchWithTransaction::<true>::default(). Transactions provide a way to guarantee that a batch of writes will only be written if there are no conflicts.

Links

Write batch with transaction doc

Write batch test

aoudiamoncef avatar Sep 23 '22 07:09 aoudiamoncef

There is more details about RocksDB transactions here

IMHO transactions are safer than atomic write only. If we enable transactions, we have to choose between pessimistic vs optimistic one's.

aoudiamoncef avatar Sep 23 '22 22:09 aoudiamoncef

There is more details about RocksDB transactions here

IMHO transactions are safer than atomic write only. If we enable transactions, we have to choose between pessimistic vs optimistic one's.

According to what I read there, atomic writes with WriteBatch provide better performance than transactions but less consistency guarantees in case of simultaneous write conflicts. In Massa, only one thread is writing to rocksdb. So if I'm not mistaken, our best course would be to stick to WriteBatch since we don't need the rollback guarantees of transactions ?

damip avatar Sep 24 '22 07:09 damip

According to what I read there, atomic writes with WriteBatch provide better performance than transactions but less consistency guarantees in case of simultaneous write conflicts. In Massa, only one thread is writing to rocksdb. So if I'm not mistaken, our best course would be to stick to WriteBatch since we don't need the rollback guarantees of transactions ?

When we look at Rocks performance benchmarks it's higher than Massa throughput. So adding a transaction will not impact us*.

Have we a guaranty that we don't launch several WriteBatch's which could affect same keys ? if the WriteBatch fails to did it's atomic write ? it's a choice between performance vs consistency but the loose in performance is not that high IMHO.

I always did this in my applications to avoid weird issues.

We could enable this feature in the future if we detect consistency issues in the ledger.

aoudiamoncef avatar Sep 24 '22 08:09 aoudiamoncef

This is done, right ? @Leo-Besancon

damip avatar Jun 05 '23 09:06 damip

If I understood correctly, this would only be an issue if multiple thread try and write on the same keys. In the current implementation, the massa-db only writes from self.current_batch, which is a Arc<Mutex<WriteBatch>>, so it should be ok.

Leo-Besancon avatar Jun 05 '23 11:06 Leo-Besancon