3

As I understand, when Bitcoin Core has pruning turned on, it will flush the dbcache when it prunes unnecessary blk*.dat and rev*.dat files during ibd.

In https://bitcoincore.reviews/20827, it states:

"Pruning is a reason for us to flush the dbcache regardless of its memory usage. The maximum configured dbcache size is often not reached."

I don't understand the reasoning behind this flush. Even if you delete the *.dat files, the UTXO set should still be consistent and the ibd would function normally. I'm thinking there has to be some reason but I can't figure out why.

1 Answer 1

3

Flushing the dbcache is the mechanism by which the database on disk is brought in sync with the database in memory. Since the UTXO set constantly changes, it's more performant to cache the most recent changes in memory rather than writing everything out to disk immediately as disk I/O can be pretty slow.

Because entries contained in the dbcache are not always on disk, an event that kills bitcoind before the flush can occur (such as sudden power loss) results in those UTXO set changes being lost. But this is not catastrophic as long as we still have the blocks. We can just load up the chainstate with the most recent data, and go through those blocks again to update the chainstate.

With pruning, we are going to be deleting blocks. This introduces a situation where we have UTXO set changes in the dbcache but not on disk and the blocks originating those changes have been removed. If bitcoind were to be killed at this time, we would have no way of recovering the changes without re-downloading the blocks. By flushing the dbcache before removing the blk*.dat and undo*.dat files, we can ensure that the UTXO set on disk is up to date before the files are pruned.

4
  • 3
    Strictly speaking, I think it's only necessary to flush when pruning when the prune-point would become more recent than the last db flush, but I don't believe Bitcoin Core makes that distinction. Jan 16, 2023 at 16:36
  • So is the reason for the dbcache flush just in case bitcoind is suddenly killed? If the assumption was that bitcoind will never get killed unexpectedly, would there be no reason to flush the dbcache? Jan 17, 2023 at 4:43
  • Do I understand correctly that making that distinction might significantly speed up synchronization for pruning nodes, because they could make more use of the dbcache, @PieterWuille?
    – Murch
    Jan 17, 2023 at 15:15
  • @Murch Unless your prune size is pretty large, or the dbcache is really small, I don't think it'd make a difference. Jan 17, 2023 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.