Best practices for closing badger db


(Jack Miller) #1

Hi all.

I’m wondering if there is a best practice when using badger db on a long running instance when it comes to closing the database.

Ideally I think we want to close the db before program exit, even with badger being crash resilient.

And just as a final question, is it wise to leave the db open or should we close it after writing/reading?

Thanks


(Navjeet Sandhu) #2

I close it by handling the syscall.SIGINT, syscall.SIGTERM. E.g.

func init() {
// Create channel to listen to OS interrupt signals
shutdownCh = make(chan os.Signal, 1)
signal.Notify(shutdownCh, syscall.SIGINT, syscall.SIGTERM)
go onExit()

// Call to badger.Open()
}

func onExit() {
<-shutdownCh
db.Close()
}


(Manish R Jain) #3

If SyncWrites=true, then whether you close it or not, your data would be safe. But, it is definitely advisable to close the DB, so it has chance to flush out inmemory tables, making things faster on the next Open.