These are the slides I was using when delivering the meetup described at https://www.meetup.com/lifemichael/events/292574718/ You can find the video of this meetup at https://youtu.be/qT9NmgPU1j8
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
MongoDB Design Patterns
1. MongoDB
Haim Michael
June 6th
, 2023
All logos, trade marks and brand names used in this presentation belong
to the respective owners.
life
michae
l
Design Patterns
3. What is Design Pattern
● “In software engineering, a software design pattern is a
general, reusable solution to a commonly occurring
problem”. (Wikipedia)
4. Best Practices
● The design patterns usually formalize best practices that
the programmer can use to solve common problems when
designing an application or system.
5. MongoDB Patterns Summary
● You can find a summary of all patterns that evolved during
the years at https://tinyurl.com/lifemichaelmongodbpatterns
7. Computed Description
● When having the need in a repeatedly calculation of data in
our application, and when most of the interaction with the
database is for reading data (not writing) then we might
improve the performance by writing the result of the
repeated calculation to our database, and retrieve it when
needed.
https://www.mongodb.com/blog/post/building-with-patterns-the-computed-pattern
9. Computed Pros
● The CPU workload is reduced. The calculations are
performed less frequently.
● The calculated data is already on the database. The
queries becomes simpler and faster.
12. Approximation Description
● When expensive operations are required for maintaining
our data and the precision is not the highest priority, we can
ease our platform by avoiding the expensive operations by
maintaining approximate values in the application layer.
https://www.mongodb.com/blog/post/building-with-patterns-the-approximation-pattern
13. Approximation Pros
● We get fewer writes operation to the database, and the
performance improves.
● The numbers we maintain are still valid and the user
experience is not damaged.
14. Approximation Cons
● The exact numbers are not represented, and in specific
cases it might damage the user experience.
16. Description
● When we want to keep separated collections (e.g. Invoices,
customers, orders, etc.) and at the same time we need
some of the data one collection holds when fetching data
from another collection (e.g. The invoice we issue includes
customer name and customer id) we can avoid join queries,
and we can avoid huge duplicated data by duplicating just
the required data.
https://www.mongodb.com/blog/post/building-with-patterns-the-extended-reference-pattern
18. Extended Reference Pros
● By adding those frequently accessed fields into the main
document, the performance is improved (thanks to less join
queries).
19. Extended Reference Cons
● The data duplication is a side effect of this design pattern. If
the duplicated data changes over time it might cause
inconsistency in our database.
21. Bucket Description
● When having data coming as a stream, instead of storing
that data in separated small documents we better pack
these small documents into bigger ones.
https://www.mongodb.com/blog/post/building-with-patterns-the-bucket-pattern
23. Bucket Pros
● The overall total number of documents the collection holds
is reduced.
● Thanks for having multiple documents packed in bigger
ones, the performance is improved.
● Accessing the data might become simpler thanks for a
having documents that each one of them packs multiple
smaller data items.