Reduce channel database size
Reduce LND DB size, like a BO$$!
Are you tired of having a sluggish node that's slow and takes a long time to restart? Do you want a lightweight and extremely responsive node?
You have come to the right place, my friends. Let's make it happen!!
Part 1 - Updating lnd.conf
First things first, lets ssh into your node and find the lnd.conf , we will first need to edit this file and add a few settings. For umbrel users the directory is something like umbrel/umbrel/lnd/lnd.conf
cd umbrel/lnd
sudo nano lnd.conf
Under [Application Options]
Add the following lines
gc-canceled-invoices-on-startup=true
gc-canceled-invoices-on-the-fly=true
Then we will need to make an additional category, under [Application Options] to add the new bolt settings. scroll down the file find an empty space and create [bolt] section.
[bolt]
db.bolt.auto-compact=true
db.bolt.auto-compact-min-age=0
Once done go ahead and save the file by hitting ctrl + x, yes, enter to save the newly edited lnd.conf file.
Part 2 - Results for channel update count
Now that the lnd.conf settings are out of the way, we can run this command. Please note that if you are running Umbrel the command below will work, if not on Umbrel please make sure you have the correct path before lncli listchannels. Thanks to ⚡G-Spot-21_69_420⚡ for mentioning this command.
sudo ~/umbrel/bin/lncli listchannels | jq -r '.[][]| [.num_updates, .remote_pubkey] | @tsv' | sort -rn | head -10
You can change the -10 from the line above to -20 to bring top 20 nodes with the most channel updates.
By running this command we will get the top 10 channels that have the most channel updates. (These updates cause your channel.db file to get very very large) This should be concerning to all serious node runners because it brings sluggishness, connection issues, and over all makes your node not as responsive.
After running the command above, your results will look something like this. (The top command will only bring you 10 results) You may change it to 20 results if you want to clean more bloat.
52227 PubKey
48209 PubKey
38230 PubKey
You will see that the first result has 52,227 channel updates, a good rule of thumb would be to pick all the channels above 50K channel updates and note down the pubkey of the listed channels and also the channel capacity in a notepad or equivalent. Doing this will make things neat organized and less of a headache when it comes time to close and reopen all these channels.
Once you you are done picking out the channels with the most updates and happy with it. That's great, now you really get down and dirty with your node by closing the channels that have the most updates.
Part 3 - Closing Channels
We will then close these channels and re-open. Closing them would have to be done manually one by one. I know it sounds boring and not really fun, but if you're serious and care about a healthy node it must be done. You will also realize how fast the lnd restarts get and how responsive your node becomes.
Part 4 - Before restarting LND
Now lets go to the directory where your channel.db is located. I want you to take a note of your channel.db size.
cd /umbrel/lnd/data/graph/mainnet
ls -l
Take note of the the channel.db size. We will come back to this directory after restarting your lnd to see how much space we saved.
If you have bos installed there is one last command that you will need to run to maximize your database file space savings before actually restarting lnd.
bos clean-failed-payments
You will see the results as something like this.
deleting_failed_payments: true
failed_payments: 53
searching_for_failed_payments: 2022-01-26T06:08:54.331Z
total_failed_payments_deleted: 53
Part 5 - Restarting LND
We can do this in many ways, choose which ever is more convenient for you. Make sure to do a channel back up once all your channels are closed, You never know.
For Umbrel users:
cd umbrel/scripts
sudo ./stop wait for it shut down then run sudo ./start
or you can just run (Restarts LND only)
cd umbrel && docker-compose restart lnd
or you can just quickly restart from your dashboard if you're too lazy to run commands ;)
Now let's wait for lnd to start and sync up to the network.
We will go back to the directory where the channel.db file was stored by running this.
cd /umbrel/lnd/data/graph/mainnet
then
ls -l
You should see a drastic difference in the file size of your channel.db at least a few gigs in savings depending on how often your rebalance and how big your node is.
Part 6 - Re-Opening Channels
For re-opening these channels back if you have bos installed you're in luck. You may copy and paste the channels and there capacities that you saved in a notepad earlier. Run this command to batch open all the channels that you closed earlier. You will also save huge on opening fees since the batch open will be done in one big transaction.
[Example] bos open pubkey --amount 1000000 pubkey2 --amount 5000000 pubkey3 --amount 2000000 pubkey4 --amount 2169420
If you don't have bos installed then you will have to open the channels with your favorite method manually. Using ThunderHub, RTL, and I'm sure there's many other platforms and methods that can be used for this.
Part 7 - Finishing notes
This is a tested and proven method, my node has about 290 + channels and almost 30 BTC capacity. My channel.db was over 8 gigs!! I realized the performance of my node degrading day by day. By doing everything that I wrote in this article I was able to shave a good 7 gigs of bloated bs that accumulated in a period of 6 months. I know it seems like a lot of steps but it is well worth it.
I was literally able to do all of this under 1 hour, I got lucky because the mempool was very low so almost all the channels closed within 10 mins and the same with opening them.
Cost:
Closing 20 channels: 20 x 223 = 4,460 sats
Opening 20 channels with bos open: 1,000 sats
Outcome:
Reduced node database size from 8GB to 1.01GB
Phat savings like a BO$$$$! I hope this is as helpful to you as much as it was for me. Thanks for reading, and if you wanna connect to my node my pubkey is beneath.
Phatsats PubKey: 034b025cd658515f37bb125b6ef040f428b0ce678253c805358e9db60c40d9f96a