Using @Indexed
ensures on the first access to an entity that declared indexes are created. Spring Data MongoDB's IndexOperations
calls createIndex(…)
. Typically, this is a no-op once the index exists with the given specification. Typically applies to applications that start up and run for quite a while.
AWS Lambda rather cleans up instances that are not hot to free resources. I'm not sure how this affects MongoDB performance when you call e.g. createIndex(…)
every minute or so. If you don't see a negative impact, then things might be fine.
Index creation on MongoDB prepares an exclusive lock (IX, intent to exclusively lock) and escalates that lock during index creation. This is can be an impact if sufficient processes try to call createIndex(…)
.
What are the alternatives?
- Keep a persistent service instance (which contradicts AWS Lambda to some extent)
- Remove
@Indexed
entirely and move index creation to an out of band process (Create the indexes externally)
- Remove
@Indexed
and create indexes programmatically (This is the recommended approach giving you the most flexibility. You can check whether the required indexes are already present and skip index creation).
See also: