Performance & Best Practices
Read on for some practical hints on how to sync with MongoDB efficiently and robustly.
Transactions
When doing multiple database write operations at once, you should always use transactions. For example, it's much more efficient to do 10 operations in a single transaction than doing these 10 operations in 10 separate transactions. This is a general rule applying to most databases and is especially true for ObjectBox and even more so for ObjectBox Sync.
Inside an ObjectBox transaction, you can write large quantities of objects in any order: ObjectBox is known for its extremely high CRUD performance and you likely won't run into bottlenecks. But when syncing to MongoDB, you may want to consider some technicalities of MongoDB for the best results. Thus, to avoid surprises later, use transactions (on the ObjectBox client) and consider the following rules inside a transaction:
Most importantly, try to avoid switching object types for write operations often. MongoDB is faster for consecutive operations of the same type (collection). Example: instead of writing 100 pairs of
PersonandAddressin a loop, first write all 100Personobjects and then all 100Addressobjects.If possible, keep
putoperations separate fromremoveoperations. Example: let's say we replace 100Addressobjects with new ones. Instead of deleting oneAddressand inserting a newAddressin a loop, first remove all 100Addressobjects before inserting the 100 newAddressobjects.The more objects you process, the more important the above recommendations get. You will most likely get away with breaking the rules with a few dozen objects without any consequence. But when processing several thousand objects in the "wrong" order, the consequences may become notable. Changes will likely be applied to MongoDB with notable delays (e.g. several seconds). In extreme cases, you may even run into MongoDB transaction timeout errors (60 seconds is the MongoDB default).
Last updated
Was this helpful?