Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Will it wear off my disk drive? #602

Open
snimavat opened this issue Dec 19, 2024 · 1 comment
Open

Will it wear off my disk drive? #602

snimavat opened this issue Dec 19, 2024 · 1 comment

Comments

@snimavat
Copy link

It seem to write to file entire db on every write operation.
So if i have a db with few 1000 entries,
and i insert a new record 1001,
it will write all 1001 records back to file

with frequent data updates, this can wear off the disk, isnt it

@movy
Copy link

movy commented Mar 10, 2025

From readme:

// Alternatively you can call db.write() explicitely later
// to write to db.json
db.data.posts.push('hello world')
await db.write()

Basically, instead of using db.update method, you separate data updates and writes, so you can choose when write-to-disk actually takes place.

In my production app I update lowdb-data all the time, but I write to disk only once in 5 mins AND whenever my app restarts / crashes. The latter is done via signal handlers:

exitEmitter.on('exit', this.#exitHandler.bind(null, { DB }))
process.on('SIGINT', this.#exitHandler.bind(null, { DB }))
process.on('SIGTERM', this.#exitHandler.bind(null, { DB }))
process.on('beforeExit', async () => await this.#exitHandler.bind(null, { DB }))

async #exitHandler({ DB }) {
    const message = '⚠️ ⚠️ ⚠️  has exited, check logs!!! Saving databases before restart ⚠️ ⚠️ ⚠️'
    await DB.writeAllDatabases()
    await sleep(1500)
    process.exit()
}

The app uses and updates ~100 different lowdb objects (heavy multithreading), and to avoid io-related stampede every 5 mins, I slightly randomise writing schedules.

I have been using lowdb for over a year and never lost any data on restart. Disk writes are neither performance nor wear-and-tear issue with such architecture.

Basically, for small objects / arrays (up to several Mb of json worth) I love it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants