Bad Pattern: Large Documents
When it comes to managing complex data structures, choosing the right pattern can significantly impact the efficiency and effectiveness of your app.
You can embed a map in another map to create an embedded MAP comprised of multiple, hierarchical levels.
For example, consider a people collection that contains documents with the following schema:
![Document image Document image](https://images.archbee.com/qoRkNxW5fJ81r_NqVpc8C/qwuv59DhKOS3OBfV1viYF_badpatternschema.png?format=webp)
Such a schema translates to a document with a nested map cars where each car can be arbitrarily large:
The previous approach results in slow replication performance as follows:
- In scenarios with a limited internet connection or exclusive use of Bluetooth Low Energy (LE) for syncing across connected peers, replicating documents that contain large amounts of data experiences a slowdown. For instance, relying solely on Bluetooth LE for a document of typical size results in a maximum replication rate of 20 KB per second. Consequently, a document of 250 KB or more may take 10 seconds or more to replicate for the first time between devices.
- A slow replication rate causes a loading spinner to display to your end users until the replication process completes.
The reason that a slow replication rate causes a loading spinner during the replication process is that the callback is unable to render the returned data.
The end‑to‑end replication process requires breaking down documents into smaller parts before syncing them across the mesh. Only once the client receives all the smaller parts and reconstructs them, is the document returned.
Instead of using a single document to encode all of your large datasets, use a series of smaller documents.
Documents that exceed 5 MB do not sync with other connected peers.
If a document exceeds 250 KB in size, stdout warning prints to the console.