I'm building a Go application deployed on Kubernetes that uses MongoDB as the database. Currently, my application includes logic to create indexes on collections during every pod startup. While this ensures that indexes are always up-to-date, it adds overhead to the startup process and seems redundant since indexes typically don't change frequently.
My question is:
logic
on every pod startup?Currently, I have the following function in my repository to initialize indexes for a MongoDB collection:
func (p *ForbiddenD) initIndexes(ctx context.Context) {
indexes := []mongo.IndexModel{
{
Keys: bson.M{"passengerId": 1},
Options: options.Index().SetUnique(true),
},
}
_, err := p.collection.Indexes().CreateMany(ctx, indexes)
if err != nil {
return
}
}
This function runs on every application startup, which ensures that indexes exist but feels redundant and inefficient since indexes don't change often.
My goal is to avoid executing this function
every time the application starts and implement a mechanism similar to migrations in SQL-based
databases to manage indexes in MongoDB.
My setup:
What are the best practices for handling MongoDB index creation in this scenario? Is there a way to check and apply index updates only when necessary, similar to database migrations?
What you do is perfectly fine. IndexView.CreateMany()
does nothing if the index already exists. And checking if an index exists does not take any considerable time.
If you were versioning your database (or indexing state), you would still have to read the current version from the database (with a query), and execute index creation based on that. It wouldn't be measurable faster. You could save let's say 1ms of startup time, but would that really matter? Would that be worth the complexity increase? The answer is no.
If it bothers you that index check / creation is run on each startup, then move this logic outside of your app (e.g. to a tool which is run periodically, manually or on CI/CD actions). But again, you won't see any performance improvement, so don't increase complexity and just make peace with it.