Repository navigation
Description
Is there an existing issue for this?
- I have searched the existing issues
Current behaviour
I try to understand why all .ldb on chainstate are regenearated every 60 minutes (sometimes 30 minutes). It seems to be some cron that triggers it. I would really want to avoid this as it writes 10GB of data on each 30 or 60 minutes period.
cat debug.log | grep '[leveldb] Level-0' | grep started
2026-05-15T12:18:02Z [leveldb] Level-0 table #1356: started
2026-05-15T12:18:05Z [leveldb] Level-0 table #75238: started
2026-05-15T12:18:06Z [leveldb] Level-0 table #68702: started
2026-05-15T13:18:21Z [leveldb] Level-0 table #75501: started
2026-05-15T13:48:21Z [leveldb] Level-0 table #75761: started
2026-05-15T14:18:21Z [leveldb] Level-0 table #76022: started
2026-05-15T14:48:21Z [leveldb] Level-0 table #76285: started
2026-05-15T15:18:21Z [leveldb] Level-0 table #76550: started
2026-05-15T15:48:21Z [leveldb] Level-0 table #76813: started
2026-05-15T16:48:21Z [leveldb] Level-0 table #77078: started
2026-05-15T17:48:21Z [leveldb] Level-0 table #77348: started
2026-05-15T18:48:21Z [leveldb] Level-0 table #77620: started
2026-05-15T19:48:21Z [leveldb] Level-0 table #77895: started
After each of this event logs there are many log entries like:
2026-05-15T19:48:21Z [leveldb] Delete type=2 #77536
2026-05-15T19:48:22Z [leveldb] Compacting 1@1 + 0@2 files
2026-05-15T19:48:22Z [leveldb] Generated table #77896@1: 11927 keys, 558881 bytes
2026-05-15T19:48:22Z [leveldb] compacted to: files[ 0 0 3 32 487 2 0 ]
All the process last 40 seconds and finishes like:
2026-05-15T19:49:03Z [leveldb] Delete type=2 #77898
Expected behaviour
I don't expect that chainstate db shoud be regenerated every hour. Or better if I could configure how often it does it.
Steps to reproduce
relevant bitcoin.conf:
server=1
txindex=1
dbcache=800
maxmempool=100
debug=leveldb
Relevant log output
No response
How did you obtain Bitcoin Core
Pre-built binaries
What version of Bitcoin Core are you using?
30.2
Operating system and version
ubuntu 24.04
Machine specifications
I run it inside a podman container on a ext4 fs shared.
Activity
pinheadmz commentedon May 17, 2026
I think this comment in the code answers most of your question?
bitcoin/src/validation.cpp
Lines 93 to 98 in 7802e57
I asked claude to help explain the rest of it:
Why 10 GB gets written.
The actual dirty data written per flush is small — just the UTXO changes from the last hour's blocks. The 10 GB write you're seeing is LevelDB's internal compaction, which is triggered by the flush but rewrites far more data.
With -dbcache=800 and -txindex, your coins cache gets roughly 700 MB. LevelDB is configured with
write_buffer_size = cache / 4 ≈ 175 MBWith LevelDB's default
L0_CompactionTrigger = 4,four level-0 files (≈700 MB) trigger compaction into level-1, which then cascades: level-1 (10 MB max) → level-2 (100 MB) → level-3 (1 GB) → level-4 (10 GB). The entire chainstate lives at the deeper levels, so a cascade rewrite of ~10 GB is normal.tuxArg commentedon May 18, 2026
@pinheadmz I appreciate you found exactly the lines that seem to trigger this issue. My question is why do we need this.
Just before db compation everything works fine so there's no actual urgency to do this every hour. If it can be done 24 times a day, it could probably be done just once a day too.
So, why is it fixed at 50 to 70 minutes then? It would be better to have an option to configure it.
l0rinc commentedon May 18, 2026
This was added in #30611, see the motivation in the PR description.
That's not what happens; the permanent storage is updated instead of keeping everything in memory. Can you please tell us what the problem is that you're trying to solve?
You don't need to enable LevelDB debug logging. This is normal behavior: the state is written to disk (LevelDB), which does some cleanup (background compaction) to keep disk access optimal. Again, this is a feature, not a bug. If we leave things in memory for too long, any interrupt (e.g. a crash) would wipe out that state and you would need to redo the work.
It's not, it's just updated regularly to avoid data loss. We could theoretically bump the 50/70 minute range to 90/110 minutes if users think the current interval is too frequent -- what do you think @andrewtoth?
tuxArg commentedon May 18, 2026
I've just read it and all its thread.
I run a bitcoin node and as many running one I run it continuously. What I'm trying to solve is I/O disk usage (writes in this case). Each flush to disk it writes around 10GB of data on a full node. That's 240GB a day. #30611 focused on reducing spikes and avoid redoing work after power outages. Power loss can happen but it's not frequent enough to justify optimizing for it at the expense of long running operation.
Reducing spikes is in right direction but what about total data written to disk? How much data was written before every day? I don't think it was 240GB because I would have noticed that spike.
I have enabled it to find out what was happening. 10GB every hour is still too much to be unnoticed.
It's not to avoid data loss. It was to avoid work to be redone, but we can recover that data, so it's not data loss. I think it would be better to let users to configure the interval in bitcoin.conf
andrewtoth commentedon May 18, 2026
@tuxArg did you recently upgrade from version 28 or earlier to version 30.2? In version 29 and up the leveldb max file size was increased from 2MB to 32MB, so compactions will take a long time for the first while until all 2mb files have been compacted to 32mb.
However, that doesn't explain the frequency of compactions. We now write to the chainstate leveldb every ~hour, but just writing to the db does not trigger a compaction every time.
JohnTravolski commentedon May 18, 2026
Hi, I am also concerned about this. I monitor cumulative disk writes using smartmontools since I'm running on an SSD. I don't want to kill it early since SSDs have limited writes. Previously my node was writing 20 GB / day (Core 27.0 + Fulcrum indexer), but after I upgraded to Core 31.0 it was writing about 220 GB / day, 11 times more. I used
sudo iotop -oPaand let it sit for a few hours to ensure it was attributable to thebitcoin-qtprocess.l0rinc commentedon May 18, 2026
@andrewtoth's right, it's probably the file size changes after #30039.
After it's done compacting, you will have fewer writes than before. This shouldn't kill an SSD - it took me almost 2 years to kill mine, often doing several full
reindex-chainstates per day :)GURGPqxVwj commentedon May 18, 2026
I would like to add another point here.
I read the comments above. I understand that Bitcoin Core intentionally writes the chainstate to persistent storage roughly every 50–70 minutes, and that LevelDB compaction can rewrite much more data than the actual coins cache flush.
My concern is mainly the total SSD write volume. I still do not understand how a fully synced node in normal operation can write hundreds of GB per day to the SSD. I am not talking about IBD or reindex here.
I have seen the same general pattern on two different hardware platforms:
I have also seen the same general pattern with different Bitcoin Core versions:
The current data below is from the newer system, because that setup is cleaner and easier to trust.
Current setup:
I mention both the Linux block write counter and the NVMe SMART counter because I wanted to make sure I am not just misreading one tool or measuring some local artifact. During the compaction waves, both counters increased by essentially the same amount, so the writes seem to be real device writes.
There was a large compaction wave directly after starting 31.0. That seems reasonable to me. The node had to catch up about 60 blocks and LevelDB recovery/startup activity was happening at the same time.
The more interesting part happened later, after startup/catch-up was finished.
What I repeatedly observe is this pattern:
For some time, the node behaves as I would expect. New blocks arrive, UpdateTip is logged, and the reported cache grows.
Then a point is reached where the reported UpdateTip cache stops growing. This is what I mean by "plateau" here.
The exact cache value is not always the same. In this 31.0 run the plateau was around 44.0 MiB. In earlier observations I saw the same general pattern begin at other reported cache values. So I do not think 44 MiB itself is a fixed threshold.
From that point on, new blocks are still connected, but the reported cache stays pinned.
Then LevelDB compaction waves start.
After a compaction wave, the reported cache does not continue growing again. It stays pinned, and later more compaction waves can follow.
Here is the 31.0 observation.
Shortly before the plateau, there was a normal FlushStateToDisk / BatchWrite:
After that, new blocks were connected normally. But the reported UpdateTip cache reached about 44.0 MiB and stopped growing.
In this run I observed at least 17 consecutive UpdateTips with the reported cache at about 44.0 MiB:
The txo count changed during that time, so the node was not idle. New blocks were connected, but the reported cache stayed pinned.
After this cache plateau, a large LevelDB compaction wave happened.
From my report, counted since the marker:
The SSD write counters matched this closely:
There were no storage errors in dmesg.
The compaction wave was mostly the familiar pattern of many generated ~34.5 MB .ldb files. The later part contained several lines like:
What looks important to me is that the cache did not start growing again after the large compaction wave. It stayed around 44.0 MiB for further UpdateTips, and more LevelDB activity happened later.
So to me the pattern does not look like "one cache flush, then one cleanup, then normal cache growth again". It looks more like the node reaches a state where the reported cache stops growing, and while it stays in that state, compaction waves repeat.
So what I am trying to understand is this:
Is this amount of write amplification expected for a fully synced node in normal operation?
If the answer is "yes, this is expected", then I would like to understand why a synced node can write this much data to the SSD, and whether there is a way to tune this behaviour.
I can provide selected debug.log snippets and the small write-counter reports if that would help.
andrewtoth commentedon May 18, 2026
@GURGPqxVwj
Since you have an already synced chainstate, was this synced on a pre-v29 node? If so, that explains the large compactions and the problem will resolve itself after some time. The hundreds of GB being written per day is due to the large compactions from 2mb -> 32mb leveldb files.
Regarding the cache value in the UpdateTip - this value can decrease as well after some blocks and is normal.
The cache increases when a block creates more outputs than it spends. If most transactions have the same number of inputs and outputs, the value will not increase.
Incoming mempool transactions will also increase the cache value.
Also, every ~hour the cache is written to disk, and during this process all spent entries from the cache are removed. This reduces the number of entries in the cache.
tuxArg commentedon May 18, 2026
I did. 27.1 -> 29 -> 30 -> 30.2
I also did
reindex-chainstatebefore reporting this as I thought it could be the cause. But still same behavior.I mostly see 33MB ldb files:
Well, that is the bug I'm reporting here. If 10 GB must be written then we need a way to configure how often, but if they are not meant to be rewritten every hour, then we have a bug here that triggers full compaction every time.
andrewtoth commentedon May 18, 2026
@tuxArg thanks, I see. I did measure disk usage in #30611 (comment) and did not see this issue of frequent compactions.
I will investigate and see if any nodes I run are also experiencing this behavior.
GURGPqxVwj commentedon May 18, 2026
@andrewtoth
Thanks, that explanation helps.
To answer your question: no, this chainstate was not synced on a pre-v29 node. In my setup the chainstate was created with Bitcoin Core 30.2. I never used v28 or older for this datadir. Later I upgraded only the Bitcoin Core binaries to 31.0 and kept the same datadir.
So I do not think the 2 MB -> 32 MB migration from a pre-v29 chainstate explains my case, unless I misunderstand something.
Your explanation about the UpdateTip cache makes sense. I understand now that this value does not have to grow monotonically and can decrease or stay flat depending on the blocks and cache flushes.
However, after looking at the current debug.log for longer, the large compactions do not seem to be just a one-time catch-up or startup effect. After the node was running normally on 31.0, the reported cache stayed at 44.0 MiB from height 949927 to at least height 949954, while new blocks were connected.
During this same period I see repeated large LevelDB compaction waves:
Since my marker at 09:02:59Z, the debug log shows 137 compactions and about 42.6 GiB generated/compacted LevelDB output.
So my remaining question is mainly about the amount and recurrence of this write amplification. Is that still expected for a chainstate created with 30.2, or would you expect those large compactions to settle down after some runtime?
l0rinc commentedon May 18, 2026
Thanks for the details reports!
I would also expect them to decrease, but it's normal to have some spikes temporarily. Please let us know if this continues for the following days.
Thanks.
iotamega commentedon May 18, 2026
+1 to allow this to be a configurable option. I am seeing similar issues across many nodes. Having the ability to configure this would be helpful.
ArmchairCryptologist commentedon May 18, 2026
I'm seeing similar behavior. Two full nodes with the chainstate on an SSD and the blocks on an HDD, running 30.2 and 31.0 respectively, have written on average 13.6 GB/hour and 13.8 GB/hour to their SSDs since last reboot ~12 days ago. Both of them mostly have ~32MB ldb files in the chainstate directory, so the aforementioned compaction seems to have completed, but the write rate is still the same.
Double-checked the disk stats in the hypervisor for the latter, and the write activity is still ongoing, averaging 5.4 MB/s in the last hour.
At ~330 GB/day it could take less than a year to reach the rated write endurance for some recent QLC SSDs - for example, the WD Green SN3000 500GB is rated for only 100 TB lifetime writes, which it would reach after only ~300 days at this rate - so this probably needs to be addressed.
23 remaining items
tuxArg commentedon May 19, 2026
@andrewtoth Do you think this simple patch could be applied to v30.2 tag? Or other changes should be made too?
ArmchairCryptologist commentedon May 19, 2026
Cannot confirm the exact version of the regression, but I can confirm that there seems to be no difference in behavior or average write levels between nodes running 30.2 and 31.0, they are all experiencing this issue with roughly the same intensity.
andrewtoth commentedon May 19, 2026
So, I see the measurements I made in #30611 (comment) were made before #30039 was merged. So, the compactions were likely still happening but were much less expensive. A 2 MiB file would only rewrite other 2 MiB files in lower levels. After #30039, a small ~2 MiB higher level file can rewrite many other 32 MiB files at the level below.
l0rinc commentedon May 19, 2026
Glad we reduced it from the original 128 mb...
andrewtoth commentedon May 20, 2026
Got some interesting write io data for average 24 hour period for multiple nodes from @0xB10C.
Note that
erinis running v29, so is compacting once a day. Still, 34 GB is still way too much write IO, when the actual chainstate writing is < 50 MB per day. After about ~36 hours now, my node with the above patch shows 1418.37 M written.Also note that
frankis running v31 with blocks only. The reason its write io is much less is because there are no mempool txs triggering random reads into the chainstate db. So, after a batch write, an L0 file is produced, but nothing reads it so it doesn't get scheduled for compaction. The next block that comes in will trigger the seek compaction, but after the L0 files are compacted they will stop there, since they aren't getting constantly read from at L1. Eventually the files will still get seek compacted after every block, but not as quickly as getting seek compacted by mempool txs.0xB10C commentedon May 22, 2026
I've been running https://github.com/andrewtoth/bitcoin/tree/disable_seek_compaction on one of my nodes for slightly more than 24h now. Seems promising. Note that this is total disk writes, and there's other stuff (e.g. detailed debug logs) writing to disk.
Here's a graph of hourly disk writes on two hosts that had comparable disk writes before.
jadeis the one running the patch.l0rinc commentedon May 22, 2026
@0xB10C can you also track the total chainstate size to make sure we understand how this patch will affect disk occupancy (not just speed or write count)?
sipa commentedon May 27, 2026
Should we keep this open until (a) the leveldb subtree has been updated and (b) we decide whether to do something like automatic full compaction every few days and/or at the end of initial sync?
Merge bitcoin-core#61: Disable seek compaction
Merge bitcoin#35465: coins: compact chainstate regularly
l0rinc commentedon Jun 22, 2026
Thanks for the reports!
Can we make sure the reporters are added to the release notes' contributors list?
tuxArg commentedon Jul 10, 2026
I don't see myself in the contributors list, but thanks anyway for all the hard work