SlideShare a Scribd company logo
1 of 26
Download to read offline
Application Design
FOR MongoDB
Alessandro Palumbo

apalumbo@byte-code.com

http:/
/it.linkedin.com/in/alessandropalumbo/

http:/
/www.byte-code.com

Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - apalumbo@byte-code.com - http:/
/www.byte-code.com

MongoDB
NoSql
OPEN-source
Document-Oriented

JSON-style documents
from humongous 

“huge; enormous”
Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - apalumbo@byte-code.com - http:/
/www.byte-code.com

JSON-style
documents
{

"_id" : "6c85fa4c-fa64-44e2-89c9-e5eb7f306ed7",

"code" : "CRS0001",

"name" : "Test",

"description" : "Test description",

"active" : true,

"scheduledDate" : {

"from" : ISODate("2013-09-12T00:00:00.000Z"),

"to" : ISODate("2013-10-31T00:00:00.000Z")

},


"version" : NumberLong(1)

}

Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - apalumbo@byte-code.com - http:/
/www.byte-code.com

don’t be relationaL
no joins

WE CAN EMBED

NO FULL 

transactions
no SCHEMA

DOCUMENT LEVEL

TRANSACTIONS

IS IT REALLY 

AN ISSUE?
Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - apalumbo@byte-code.com - http:/
/www.byte-code.com

friendly fire


DESIGN

PERFORMANCE

(aka RTFM)
ATOMIC

DOCUMENT

OPERATIONS

Write 

Concern

READ 

PREFERENCE

AVOID

NATURAL

KEYS AS

IDENTIFIERS


DESIGN

FOR

QUERY

DBREFS

VS

MANUAL

REFERENCE

DYNAMIC

SCHEMA

VS

static

languages

EMBEDDED

DATA

vs

References

be aware

of 

the trees

BE

CAREFUL

WITH 

DATES

PREALLOCATE

FIELDS?

SPLIT DATA

ON

MULTIPLE

COLLECTIONS

TUNING

UPDATES

AND

INSERTS

PURE DRIVER

VS

MAPPING

FRAMEWORKS

Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/

DOCUMENT

MOVING

SLOWS

YOU

PREPROCESS

HIGH 

RESOLUTION

DATA
FRIENDLY FIRE

Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - apalumbo@byte-code.com - http:/
/www.byte-code.com

ATOMIC

DOCUMENT

OPERATIONS

OPERATIONS ON MULTIPLE DOCUMENTS
ARE NOT ATOMIC
NO “ALL OR NOTHING”
EMBEDding OR APPLIcaTION TRANSACTIONS
CAN be used to handle the issue
RELATIONAL TRANSACTIONS ARE NOT
TOTALLY SAFE
Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - apalumbo@byte-code.com - http:/
/www.byte-code.com

Write 

Concern

“Describes the guarantee that
MongoDB provides when reporting
on the success of a write
operation”

IT IS SET BY THE CLIENT AND CAN BE SET FOR
EACH OPERATION
Errors Ignored

Unacknowledged

Acknowledged (*)

Journaled

Replica Acknowledged

> 1 , majority , custom using tags
Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - apalumbo@byte-code.com - http:/
/www.byte-code.com

Read

Preference

“IT describes how MongoDB clients
route read operations to members
of a replica set”

IT IS SET BY THE CLIENT AND CAN BE SET FOR
EACH OPERATION

primary (*)

primary Preferred

secondary

secondary PREFERRED

nearest
Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - apalumbo@byte-code.com - http:/
/www.byte-code.com

AVOID

NATURAL

KEYS AS

IDENTIFIERS

All collections have an index on
the id field that exists by default.
If ID IS NOT PROVIDED the driver or
the mongod will create an _id
field with an ObjectID value.

ADD AN UNIQUE INDEX ON THE NATURAL KEY,
SOMETIMES THE APPLICATION REALM CAN
EVOLVE IN AN UNEXPECTED WAY
REMEMBER THAT UNIQUE INDEXES FIELDS
MUST BE PART OF THE SHARD KEY IF
SHARDING IS ENABLED
Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
DESIGN

Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - apalumbo@byte-code.com - http:/
/www.byte-code.com

DESIGN

FOR

QUERY

DOCUMENT DESIGN IS FUNCTIONAL TO
THE QUERIES THAT WILL EXISTS IN THE
APPLICATION

REFERENCE OR EMBED DOCUMENTS,

“denormalized” is not always

a bad word
your document design will affect
what kind of OPERATIONS will be safe
or not
Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - apalumbo@byte-code.com - http:/
/www.byte-code.com

EMBEDDED

DATA

vs

References

Embedded data models allow
applications to store related
pieces of information in the same
database record

USUALLY there is a “contains” relation
between the embedding and the embedded
object
The maximum BSON document size is 16
megabytes and embedding may lead to
performance issues if not correctly used
Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - apalumbo@byte-code.com - http:/
/www.byte-code.com

EMBEDDED

DATA

vs

References

Normalized data models describe
relationships using references
between documents

References provides more flexibility
than embedding but remember that
client-side applications will have to
lookup for referenced objects with
multiple queries
NO Referential integrity is supported,
references could point to a not existing
object
Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - apalumbo@byte-code.com - http:/
/www.byte-code.com

DBREFS

VS

MANUAL

REFERENCE

DBRefs are a convention for
representing a document, it will
hold the collection name, the id,
and optionally the db name

MANUAL REFERENCES are just fields
that will hold the id of the
related document, without the
collection name or the db name
MANUAL REFERENCES are suitable
for most of the use cases
Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - apalumbo@byte-code.com - http:/
/www.byte-code.com

BE

CAREFUL

WITH 

DATES

ALWAYS Use bson date when is
related to an instant of time or
you will never be able to use
operators on that fields


BSON Date is a 64-bit signed integer
that represents the number of
milliseconds since the Unix epoch
(Jan 1, 1970), Negative values
represent dates before 1970.

The official BSON specification
refers to the BSON Date type as
the UTC datetime.

Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - apalumbo@byte-code.com - http:/
/www.byte-code.com

SPLIT DATA

ON

MULTIPLE

COLLECTIONS

split data on multiple collections
to easily partition your data
(a.k.a. Multitenancy)

use collections as namespaces for
your data
remember once data is partioned it
will be more hard to aggregate if
needed

Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - apalumbo@byte-code.com - http:/
/www.byte-code.com

DYNAMIC

SCHEMA

VS

static

languages

why use dynamic schema if we are
not using a dynamic programming
language?

inheritance is not only a matter
of hierarchy, it could be also a
matter of composition
composition is the key to
introduce dynamic schema in a
static programming language

Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - apalumbo@byte-code.com - http:/
/www.byte-code.com

PURE DRIVER

VS

MAPPING

FRAMEWORKS

using the mongo driver directly
will give you great powers, but
will force you to write a lot of
boilerplate code

MAPPING FRAMEWORKS WILL HELP TO
WRITE LESS CODE, but you will sacrifice
the control on all the aspects of the
persistence

why not take the most from both?
Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
PERFORMANCE

Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - apalumbo@byte-code.com - http:/
/www.byte-code.com

be aware

of 

the trees

Indexes in MongoDB are defined at 

the collection level and can be on
any field or sub-field of the
document

Indexes are created using a b-tree and can
be of different types
Single Field

Compound

Multikey

Geospatial

TEXT (BETA)

Hashed

THEY COULD BE UNIQUE and sparse
Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - apalumbo@byte-code.com - http:/
/www.byte-code.com

DOCUMENT

MOVING

SLOWS

YOU

MONGODB handle the space
allocation of a RECORD
considering also a PADDING FACTOR

WHEN AN UPDATED DOCUMENT DOES
NOT FIT IN THE RECORD SPACE IT WILL
BE MOVED
DYNAMIC SCHEMA IS THE FIRST CAUSE
OF DOCUMENT MOVING

Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - apalumbo@byte-code.com - http:/
/www.byte-code.com

PREALLOCATE

FIELDS?

FIELDS PREALLOCATION CAN FIX THE
DOCUMENT MOVING ISSUES IN SOME USE
CASES

Default values must be used to
preallocate, this MUST BE HANDLEDin the
application
NULL is not a default value :-) as it has
its own type

Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - apalumbo@byte-code.com - http:/
/www.byte-code.com

PREPROCESS

HIGH 

RESOLUTION

DATA

MONGODB let you store the
maximum resolution of your data

MAP REDUCE and aggregation ARE ok

but you could also preprocess and
have aggregated data that you can
use for your queries

MONGODB rocks for business
intelligence

Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - apalumbo@byte-code.com - http:/
/www.byte-code.com

TUNING

UPDATES

AND

INSERTS

MongoDB stores BSON documents
as a sequence of fields and values,
not as aN hash table

WRITING THE FIRST FIELD OF A DOCUMENT (OR
A NESTED DOCUMENT) is considerably
faster than writing THE LAST

Intra-Document Hierarchy could help to
handle the issue

Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/
Any questions?

Except where otherwise noted, this work is licensed under: http:/
/creativecommons.org/licenses/by/3.0/

More Related Content

Similar to Application Design for MongoDB

GDB - a tough nut to crack: only a few bugs found by PVS-Studio
GDB - a tough nut to crack: only a few bugs found by PVS-StudioGDB - a tough nut to crack: only a few bugs found by PVS-Studio
GDB - a tough nut to crack: only a few bugs found by PVS-StudioPVS-Studio
 
Fix: static code analysis into our project
Fix: static code analysis into our project Fix: static code analysis into our project
Fix: static code analysis into our project noelchris3
 
PVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's codePVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's codePVS-Studio
 
Past, Present and Future of Data Processing in Apache Hadoop
Past, Present and Future of Data Processing in Apache HadoopPast, Present and Future of Data Processing in Apache Hadoop
Past, Present and Future of Data Processing in Apache HadoopCodemotion
 
Introduction to MongoDB (Webinar Jan 2011)
Introduction to MongoDB (Webinar Jan 2011)Introduction to MongoDB (Webinar Jan 2011)
Introduction to MongoDB (Webinar Jan 2011)MongoDB
 
MongoDB Introduction, Installation & Execution
MongoDB Introduction, Installation & ExecutionMongoDB Introduction, Installation & Execution
MongoDB Introduction, Installation & ExecutioniFour Technolab Pvt. Ltd.
 
Intro To Mongo Db
Intro To Mongo DbIntro To Mongo Db
Intro To Mongo Dbchriskite
 
Porting your favourite cmdline tool to Android
Porting your favourite cmdline tool to AndroidPorting your favourite cmdline tool to Android
Porting your favourite cmdline tool to AndroidVlatko Kosturjak
 
Code Documentation. That ugly thing...
Code Documentation. That ugly thing...Code Documentation. That ugly thing...
Code Documentation. That ugly thing...Christos Manios
 
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDBJava Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDBMongoDB
 
BedCon 2013 - Java Persistenz-Frameworks für MongoDB
BedCon 2013 - Java Persistenz-Frameworks für MongoDBBedCon 2013 - Java Persistenz-Frameworks für MongoDB
BedCon 2013 - Java Persistenz-Frameworks für MongoDBTobias Trelle
 
MongoDB.local Austin 2018: MongoDB Ops Manager + Kubernetes
MongoDB.local Austin 2018: MongoDB Ops Manager + KubernetesMongoDB.local Austin 2018: MongoDB Ops Manager + Kubernetes
MongoDB.local Austin 2018: MongoDB Ops Manager + KubernetesMongoDB
 
Build a notepad application with PHP, MongoDB, and IBM Bluemix - by Vikram Va...
Build a notepad application with PHP, MongoDB, and IBM Bluemix - by Vikram Va...Build a notepad application with PHP, MongoDB, and IBM Bluemix - by Vikram Va...
Build a notepad application with PHP, MongoDB, and IBM Bluemix - by Vikram Va...Carlos Tomas
 
Back to Basics 2017: Mí primera aplicación MongoDB
Back to Basics 2017: Mí primera aplicación MongoDBBack to Basics 2017: Mí primera aplicación MongoDB
Back to Basics 2017: Mí primera aplicación MongoDBMongoDB
 
Compose Camp S1.pptx
Compose Camp S1.pptxCompose Camp S1.pptx
Compose Camp S1.pptxGDSCSIT
 
Build your first MongoDB App in Ruby @ StrangeLoop 2013
Build your first MongoDB App in Ruby @ StrangeLoop 2013Build your first MongoDB App in Ruby @ StrangeLoop 2013
Build your first MongoDB App in Ruby @ StrangeLoop 2013Steven Francia
 

Similar to Application Design for MongoDB (20)

GDB - a tough nut to crack: only a few bugs found by PVS-Studio
GDB - a tough nut to crack: only a few bugs found by PVS-StudioGDB - a tough nut to crack: only a few bugs found by PVS-Studio
GDB - a tough nut to crack: only a few bugs found by PVS-Studio
 
Composer
ComposerComposer
Composer
 
Fix: static code analysis into our project
Fix: static code analysis into our project Fix: static code analysis into our project
Fix: static code analysis into our project
 
PVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's codePVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's code
 
Past, Present and Future of Data Processing in Apache Hadoop
Past, Present and Future of Data Processing in Apache HadoopPast, Present and Future of Data Processing in Apache Hadoop
Past, Present and Future of Data Processing in Apache Hadoop
 
Introduction to MongoDB (Webinar Jan 2011)
Introduction to MongoDB (Webinar Jan 2011)Introduction to MongoDB (Webinar Jan 2011)
Introduction to MongoDB (Webinar Jan 2011)
 
MongoDB Introduction, Installation & Execution
MongoDB Introduction, Installation & ExecutionMongoDB Introduction, Installation & Execution
MongoDB Introduction, Installation & Execution
 
Mdb dn 2016_09_34_features
Mdb dn 2016_09_34_featuresMdb dn 2016_09_34_features
Mdb dn 2016_09_34_features
 
Intro To Mongo Db
Intro To Mongo DbIntro To Mongo Db
Intro To Mongo Db
 
Porting your favourite cmdline tool to Android
Porting your favourite cmdline tool to AndroidPorting your favourite cmdline tool to Android
Porting your favourite cmdline tool to Android
 
MongoDB & Chirp
MongoDB & ChirpMongoDB & Chirp
MongoDB & Chirp
 
Code Documentation. That ugly thing...
Code Documentation. That ugly thing...Code Documentation. That ugly thing...
Code Documentation. That ugly thing...
 
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDBJava Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
 
MongoDB and Node.js
MongoDB and Node.jsMongoDB and Node.js
MongoDB and Node.js
 
BedCon 2013 - Java Persistenz-Frameworks für MongoDB
BedCon 2013 - Java Persistenz-Frameworks für MongoDBBedCon 2013 - Java Persistenz-Frameworks für MongoDB
BedCon 2013 - Java Persistenz-Frameworks für MongoDB
 
MongoDB.local Austin 2018: MongoDB Ops Manager + Kubernetes
MongoDB.local Austin 2018: MongoDB Ops Manager + KubernetesMongoDB.local Austin 2018: MongoDB Ops Manager + Kubernetes
MongoDB.local Austin 2018: MongoDB Ops Manager + Kubernetes
 
Build a notepad application with PHP, MongoDB, and IBM Bluemix - by Vikram Va...
Build a notepad application with PHP, MongoDB, and IBM Bluemix - by Vikram Va...Build a notepad application with PHP, MongoDB, and IBM Bluemix - by Vikram Va...
Build a notepad application with PHP, MongoDB, and IBM Bluemix - by Vikram Va...
 
Back to Basics 2017: Mí primera aplicación MongoDB
Back to Basics 2017: Mí primera aplicación MongoDBBack to Basics 2017: Mí primera aplicación MongoDB
Back to Basics 2017: Mí primera aplicación MongoDB
 
Compose Camp S1.pptx
Compose Camp S1.pptxCompose Camp S1.pptx
Compose Camp S1.pptx
 
Build your first MongoDB App in Ruby @ StrangeLoop 2013
Build your first MongoDB App in Ruby @ StrangeLoop 2013Build your first MongoDB App in Ruby @ StrangeLoop 2013
Build your first MongoDB App in Ruby @ StrangeLoop 2013
 

Recently uploaded

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 

Recently uploaded (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 

Application Design for MongoDB

  • 1. Application Design FOR MongoDB Alessandro Palumbo apalumbo@byte-code.com http:/ /it.linkedin.com/in/alessandropalumbo/ http:/ /www.byte-code.com Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 2. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com MongoDB NoSql OPEN-source Document-Oriented JSON-style documents from humongous “huge; enormous” Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 3. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com JSON-style documents { "_id" : "6c85fa4c-fa64-44e2-89c9-e5eb7f306ed7", "code" : "CRS0001", "name" : "Test", "description" : "Test description", "active" : true, "scheduledDate" : { "from" : ISODate("2013-09-12T00:00:00.000Z"), "to" : ISODate("2013-10-31T00:00:00.000Z") }, "version" : NumberLong(1) } Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 4. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com don’t be relationaL no joins WE CAN EMBED NO FULL transactions no SCHEMA DOCUMENT LEVEL TRANSACTIONS IS IT REALLY AN ISSUE? Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 5. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com friendly fire DESIGN PERFORMANCE (aka RTFM) ATOMIC DOCUMENT OPERATIONS Write Concern READ PREFERENCE AVOID NATURAL KEYS AS IDENTIFIERS DESIGN FOR QUERY DBREFS VS MANUAL REFERENCE DYNAMIC SCHEMA VS static languages EMBEDDED DATA vs References be aware of the trees BE CAREFUL WITH DATES PREALLOCATE FIELDS? SPLIT DATA ON MULTIPLE COLLECTIONS TUNING UPDATES AND INSERTS PURE DRIVER VS MAPPING FRAMEWORKS Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ DOCUMENT MOVING SLOWS YOU PREPROCESS HIGH RESOLUTION DATA
  • 6. FRIENDLY FIRE Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 7. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com ATOMIC DOCUMENT OPERATIONS OPERATIONS ON MULTIPLE DOCUMENTS ARE NOT ATOMIC NO “ALL OR NOTHING” EMBEDding OR APPLIcaTION TRANSACTIONS CAN be used to handle the issue RELATIONAL TRANSACTIONS ARE NOT TOTALLY SAFE Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 8. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com Write Concern “Describes the guarantee that MongoDB provides when reporting on the success of a write operation” IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION Errors Ignored Unacknowledged Acknowledged (*) Journaled Replica Acknowledged > 1 , majority , custom using tags Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 9. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com Read Preference “IT describes how MongoDB clients route read operations to members of a replica set” IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION primary (*) primary Preferred secondary secondary PREFERRED nearest Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 10. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com AVOID NATURAL KEYS AS IDENTIFIERS All collections have an index on the id field that exists by default. If ID IS NOT PROVIDED the driver or the mongod will create an _id field with an ObjectID value. ADD AN UNIQUE INDEX ON THE NATURAL KEY, SOMETIMES THE APPLICATION REALM CAN EVOLVE IN AN UNEXPECTED WAY REMEMBER THAT UNIQUE INDEXES FIELDS MUST BE PART OF THE SHARD KEY IF SHARDING IS ENABLED Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 11. DESIGN Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 12. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com DESIGN FOR QUERY DOCUMENT DESIGN IS FUNCTIONAL TO THE QUERIES THAT WILL EXISTS IN THE APPLICATION REFERENCE OR EMBED DOCUMENTS, “denormalized” is not always a bad word your document design will affect what kind of OPERATIONS will be safe or not Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 13. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com EMBEDDED DATA vs References Embedded data models allow applications to store related pieces of information in the same database record USUALLY there is a “contains” relation between the embedding and the embedded object The maximum BSON document size is 16 megabytes and embedding may lead to performance issues if not correctly used Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 14. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com EMBEDDED DATA vs References Normalized data models describe relationships using references between documents References provides more flexibility than embedding but remember that client-side applications will have to lookup for referenced objects with multiple queries NO Referential integrity is supported, references could point to a not existing object Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 15. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com DBREFS VS MANUAL REFERENCE DBRefs are a convention for representing a document, it will hold the collection name, the id, and optionally the db name MANUAL REFERENCES are just fields that will hold the id of the related document, without the collection name or the db name MANUAL REFERENCES are suitable for most of the use cases Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 16. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com BE CAREFUL WITH DATES ALWAYS Use bson date when is related to an instant of time or you will never be able to use operators on that fields BSON Date is a 64-bit signed integer that represents the number of milliseconds since the Unix epoch (Jan 1, 1970), Negative values represent dates before 1970. The official BSON specification refers to the BSON Date type as the UTC datetime. Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 17. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com SPLIT DATA ON MULTIPLE COLLECTIONS split data on multiple collections to easily partition your data (a.k.a. Multitenancy) use collections as namespaces for your data remember once data is partioned it will be more hard to aggregate if needed Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 18. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com DYNAMIC SCHEMA VS static languages why use dynamic schema if we are not using a dynamic programming language? inheritance is not only a matter of hierarchy, it could be also a matter of composition composition is the key to introduce dynamic schema in a static programming language Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 19. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com PURE DRIVER VS MAPPING FRAMEWORKS using the mongo driver directly will give you great powers, but will force you to write a lot of boilerplate code MAPPING FRAMEWORKS WILL HELP TO WRITE LESS CODE, but you will sacrifice the control on all the aspects of the persistence why not take the most from both? Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 20. PERFORMANCE Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 21. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com be aware of the trees Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document Indexes are created using a b-tree and can be of different types Single Field Compound Multikey Geospatial TEXT (BETA) Hashed THEY COULD BE UNIQUE and sparse Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 22. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com DOCUMENT MOVING SLOWS YOU MONGODB handle the space allocation of a RECORD considering also a PADDING FACTOR WHEN AN UPDATED DOCUMENT DOES NOT FIT IN THE RECORD SPACE IT WILL BE MOVED DYNAMIC SCHEMA IS THE FIRST CAUSE OF DOCUMENT MOVING Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 23. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com PREALLOCATE FIELDS? FIELDS PREALLOCATION CAN FIX THE DOCUMENT MOVING ISSUES IN SOME USE CASES Default values must be used to preallocate, this MUST BE HANDLEDin the application NULL is not a default value :-) as it has its own type Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 24. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com PREPROCESS HIGH RESOLUTION DATA MONGODB let you store the maximum resolution of your data MAP REDUCE and aggregation ARE ok but you could also preprocess and have aggregated data that you can use for your queries MONGODB rocks for business intelligence Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 25. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com TUNING UPDATES AND INSERTS MongoDB stores BSON documents as a sequence of fields and values, not as aN hash table WRITING THE FIRST FIELD OF A DOCUMENT (OR A NESTED DOCUMENT) is considerably faster than writing THE LAST Intra-Document Hierarchy could help to handle the issue Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/
  • 26. Any questions? Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/