Skip to content

Commit 9032c0b

Browse files
committed
docs: added a user guide for db migration
1 parent 1a30ac2 commit 9032c0b

File tree

1 file changed

+213
-0
lines changed

1 file changed

+213
-0
lines changed

docs/db_migration_guide.md

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
# `LND Database Upgrade Guide`: A Two-Stage Migration for Node Operators
2+
3+
*Table of Contents*
4+
- [Overview](#overview)
5+
- [Operation Modes](#operation-modes)
6+
- [Stage 1: Migration from bbolt to SQLite/Postgres (kvdb)](#stage-1-migration-from-bbolt-to-sqlitepostgres-kvdb)
7+
- [Choosing Your Target Backend](#choosing-your-target-backend)
8+
- [Postgres kvdb Migration](#postgres-kvdb-migration)
9+
- [SQLite kvdb Migration](#sqlite-kvdb-migration)
10+
- [Stage 2: Migration from kvdb to Relational Database](#stage-2-migration-from-kvdb-to-relational-database)
11+
- [Subsystem Readiness](#subsystem-readiness)
12+
- [Known Limitations and Edge Cases](#known-limitations-and-edge-cases)
13+
- [Best Practices for Node Operators](#best-practices-for-node-operators)
14+
- [Choosing the Right Path](#choosing-the-right-path)
15+
- [Timing Your Migration](#timing-your-migration)
16+
- [Backup and Validation](#backup-and-validation)
17+
- [Implementation Examples](#implementation-examples)
18+
- [Migrating to SQLite kvdb](#migrating-to-sqlite-kvdb-once-available)
19+
- [Migrating Invoices to Relational Backend](#migrating-invoices-to-relational-backend)
20+
- [Future Improvements](#future-improvements)
21+
- [Conclusion](#conclusion)
22+
23+
---
24+
25+
## Overview
26+
27+
The `LND Database Upgrade` process enables node operators to migrate from the legacy **bbolt** key-value store to modern **SQLite** or **PostgreSQL** backends—first in **kvdb mode**, then ultimately to a **relational database** format. This two-stage migration addresses long-standing performance bottlenecks, and lays the foundation for scalable, maintainable node operations.
28+
29+
This guide explains the migration path, backend trade-offs, subsystem readiness, and best practices for safely upgrading your LND database.
30+
31+
---
32+
33+
## Operation Modes
34+
35+
The LND database upgrade operates in two sequential stages, each with distinct tooling and implications:
36+
37+
```mermaid
38+
flowchart TD
39+
subgraph "Step 1: Migration from bbolt to kvdb (via lndinit)"
40+
direction TB
41+
Bbolt["Bbolt (kvdb)"] --> SQLite["SQLite (kvdb)"]
42+
Bbolt --> Postgres["Postgres (kvdb)"]
43+
end
44+
45+
subgraph "Step 2: Migration from kvdb to Native SQL (via lnd)"
46+
direction TB
47+
SQLite --> M1_Sqlite["Migration #1 (lnd v0.19)\nInvoices"]
48+
M1_Sqlite --> M2_Sqlite["Migration #2 (lnd v0.20)\nGraph"]
49+
M2_Sqlite --> M3_Sqlite["Migration #3 (lnd v0.21)\nPayments"]
50+
51+
Postgres --> M1_Postgres["Migration #1 (lnd v0.19)\nInvoices"]
52+
M1_Postgres --> M2_Postgres["Migration #2 (lnd v0.20)\nGraph"]
53+
M2_Postgres --> M3_Postgres["Migration #3 (lnd v0.21)\nPayments"]
54+
end
55+
56+
style Bbolt fill:#e2e3e5,stroke:#383d41
57+
style SQLite fill:#d4edda,stroke:#155724
58+
style M1_Sqlite fill:#d4edda,stroke:#155724
59+
style M2_Sqlite fill:#d4edda,stroke:#155724
60+
style M3_Sqlite fill:#d4edda,stroke:#155724
61+
style Postgres fill:#cce5ff,stroke:#004085
62+
style M1_Postgres fill:#cce5ff,stroke:#004085
63+
style M2_Postgres fill:#cce5ff,stroke:#004085
64+
style M3_Postgres fill:#cce5ff,stroke:#004085
65+
66+
```
67+
68+
- **Stage 1**: Migrate from bbolt to a SQL-based **kvdb** backend using the [lndinit](https://github.com/lightninglabs/lndinit/blob/main/docs/data-migration.md) tool.
69+
- **Stage 2**: Incrementally migrate subsystem data (invoices, graph, payments, etc.) from **kvdb** to SQL native **relational tables** as support becomes available.
70+
71+
---
72+
73+
## Stage 1: Migration from bbolt to SQLite/Postgres (kvdb)
74+
75+
LND cannot run with mixed backends, so all users must first leave bbolt behind. This stage uses the `lndinit` utility to perform an offline migration.
76+
77+
### Choosing Your Target Backend
78+
79+
| Backend | Performance (in kvdb mode) | Default in LND? | Long-Term Viability |
80+
|-----------|--------------------------|------------------|----------------------|
81+
| Postgres | Mediocre | No ||
82+
| SQLite | Good | **Yes (future)** ||
83+
84+
> 💡 **Recommendation**: Unless you require Postgres for infrastructure reasons, **migrate to SQLite kvdb** as your interim backend.
85+
86+
### Postgres kvdb Migration
87+
88+
A migration script is available via `lndinit`:
89+
90+
- [Postgres migration script](https://github.com/lightninglabs/lndinit/blob/main/docs/data-migration.md#using-postgres-as-the-destination-remote-database)
91+
- **Caveat**: Users report degraded performance in kvdb mode. Only proceed if you plan to **immediately follow with Stage 2** to migrate the available data stores to relational DB.
92+
This will mitigate the poor Postgres performance on kvdb.
93+
94+
### SQLite kvdb Migration
95+
96+
- [SQLite migration script](https://github.com/lightninglabs/lndinit/blob/main/docs/data-migration.md#using-sqlite-as-the-destination-remote-database)
97+
- **Advantage**: Maintains good performance while waiting for full relational migration.
98+
99+
---
100+
101+
## Stage 2: Migration from kvdb to Relational Database
102+
103+
This stage unlocks true SQL performance by restructuring data into relational tables. Migration is **per-subsystem** and **incremental**.
104+
105+
### Subsystem Readiness
106+
107+
| Subsystem | Relational Backend | Migration Script | Status |
108+
|---------------------|--------------------|------------------|--------|
109+
| Invoices | ✅ Available || Available with **v0.19** |
110+
| Graph | ✅ Available || Available with **v0.20** |
111+
| Payments | 🚧 In Progress | Planned | Targetted with **v0.21**|
112+
| Btcwallet | 🚧 In Progress | Planned | Targetted with **v0.21**|
113+
| Forwarding History | ❌ TBD | ❌ TBD | Future work |
114+
115+
---
116+
117+
## Known Limitations and Edge Cases
118+
119+
- **No mixed backends**: LND requires a single consistent backend. You cannot run invoices in relational mode while payments remain in kvdb *unless* both are on the same SQL engine (e.g., SQLite).
120+
- **Data loss risk**: Always **back up your `data/` directory** before migration.
121+
- **Downtime required**: Stage 1 requires LND to be offline. Stage 2 is done at startup, requiring a LND restart.
122+
- **Postgres kvdb performance**: Postgres performance on kvdb is sub-optimal,
123+
recommended to make the stage 2 migration immediately to avoid performance bottlenecks.
124+
- **No Option to move between Postgres and SQLite**: Analyze carefully to make a determination between the two DB choices, as there is no option to move to a different DB after the migration is complete.
125+
---
126+
127+
## Best Practices for Node Operators
128+
129+
### Choosing the Right Path
130+
131+
- **For most users**: Wait for **SQLite kvdb migration script**, then migrate. Later, adopt relational backends subsystem-by-subsystem.
132+
- **Enterprise/Postgres users**: Delay migration until **payments relational backend** is ready, then perform **Stage 1 + Stage 2 in quick succession**.
133+
134+
### Timing Your Migration
135+
136+
- Perform migrations during **low-activity periods**.
137+
- Monitor LND release notes for relational db support of different subsystems.
138+
139+
### Backup and Validation
140+
141+
1. Stop LND.
142+
2. Backup entire `~/.lnd/` directory.
143+
3. Run migration with `lndinit`.
144+
4. Start LND with new backend flags, to execute stage 2 migrations.
145+
5. Validate node health: channels, balance, invoice/payment history.
146+
147+
---
148+
149+
## Implementation Examples
150+
151+
### Migrating to SQLite kvdb (once available)
152+
153+
```bash
154+
# Stop LND
155+
lnd --shutdown
156+
157+
# Backup
158+
cp -r ~/.lnd ~/lnd-backup-$(date +%Y%m%d)
159+
160+
# Run migration (e.g. sqlite)
161+
lndinit --debuglevel info migrate-db \
162+
--source.bolt.data-dir /home/myuser/.lnd/data \
163+
--dest.backend sqlite \
164+
--dest.sqlite.data-dir /home/myuser/.lnd/data --network mainnet
165+
166+
# Start LND with SQLite backend
167+
lnd --db.backend=sqlite
168+
```
169+
170+
> 📝 Add `db.backend=sqlite` to your `lnd.conf` to make it persistent.
171+
172+
### Migrating Invoices to Relational Backend
173+
174+
Once on LND v0.19+ with SQLite/Postgres:
175+
176+
```bash
177+
# Ensure backend is set
178+
echo "db.backend=sqlite" >> ~/.lnd/lnd.conf
179+
180+
# Start LND — invoice migration runs automatically
181+
lnd
182+
```
183+
184+
Check logs for:
185+
```
186+
Migrating invoices from kvdb to relational format...
187+
Invoice migration completed successfully.
188+
```
189+
190+
---
191+
192+
## Future Improvements
193+
194+
The LND team is actively working on:
195+
196+
- **Payments relational backend** and migration tooling (Stage 2)
197+
- **Btcwallet relational backend** and migration tooling (Stage 2)
198+
- **Forwarding history** relational schema (long-term)
199+
- **Automatic detection** of migration readiness in `lnd`
200+
201+
Node operators should monitor:
202+
- [LND GitHub Releases](https://github.com/lightningnetwork/lnd/releases)
203+
- [lndinit repository](https://github.com/lightninglabs/lndinit)
204+
205+
---
206+
207+
## Conclusion
208+
209+
The LND database upgrade is a strategic two-stage process designed to eliminate bbolt performance limitations while ensuring data integrity and operational continuity.
210+
211+
By **first migrating to SQLite/Postgres kvdb** and **then adopting relational backends incrementally**, node operators can achieve significant performance gains—especially for payment-heavy workloads—without rushing into unstable configurations.
212+
213+
Choose your path wisely, back up rigorously, and stay informed. The future of LND is relational, and this guide ensures you get there safely.

0 commit comments

Comments
 (0)