+ All Categories
Home > Technology > Application Design per MongoDB

Application Design per MongoDB

Date post: 27-Nov-2014
Category:
Upload: codemotion
View: 661 times
Download: 5 times
Share this document with a friend
Description:
Quando si approccia MongoDB da sviluppatore, il rischio piú grande è quello di usare pattern e design legati al mondo SQL trascurando il vero cambiamento che MongoDB ci impone: cambiare il nostro modo di pensare.
Popular Tags:
89
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/ Application Design FOR MongoDB Alessandro Palumbo [email protected] http://it.linkedin.com/in/alessandropalumbo/ http://www.byte-code.com Saturday, November 30,
Transcript
Page 1: Application Design per MongoDB

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

Application Design

FOR MongoDB

Alessandro [email protected]

http://it.linkedin.com/in/alessandropalumbo/http://www.byte-code.com

Saturday, November 30,

Page 2: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

MongoDB

Saturday, November 30,

Page 3: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

MongoDB

NoSql

Saturday, November 30,

Page 4: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

MongoDB

NoSql

OPEN-source

Saturday, November 30,

Page 5: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

MongoDB

NoSql

OPEN-source

Document-OrientedJSON-style documents

Saturday, November 30,

Page 6: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

MongoDB

from humongous “huge; enormous”

NoSql

OPEN-source

Document-OrientedJSON-style documents

Saturday, November 30,

Page 7: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - 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)}

Saturday, November 30,

Page 8: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

don’t be relationaL

Saturday, November 30,

Page 9: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

don’t be relationaL

no joins

Saturday, November 30,

Page 10: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

don’t be relationaL

no joins

NO FULL transactions

Saturday, November 30,

Page 11: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

don’t be relationaL

no joins

NO FULL transactions

no SCHEMA

Saturday, November 30,

Page 12: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

don’t be relationaL

no joins

NO FULL transactions

no SCHEMA

WE CAN EMBED

Saturday, November 30,

Page 13: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

don’t be relationaL

no joins

NO FULL transactions

no SCHEMA

WE CAN EMBED

DOCUMENT LEVELTRANSACTIONS

Saturday, November 30,

Page 14: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

don’t be relationaL

no joins

NO FULL transactions

no SCHEMA

WE CAN EMBED

IS IT REALLY AN ISSUE?

DOCUMENT LEVELTRANSACTIONS

Saturday, November 30,

Page 15: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

Saturday, November 30,

Page 16: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

friendly fire(aka RTFM)

Write

Concern

READ

PREFERENCE

ATOMIC

DOCUMENT

OPERATIONS

AVOID

NATURAL

KEYS ASIDENTIFIERS

Saturday, November 30,

Page 17: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

DESIGN

DESIGN

FOR

QUERYEMBEDDED

DATA

vsReferences

DBREFS

VS

MANUALREFERENCE

DYNAMIC

SCHEMA

VSstatic

languages

PURE DRIVER

VS

MAPPINGFRAMEWORKS

BE

CAREFUL

WITH DATES

SPLIT DATA

ON

MULTIPLECOLLECTIONS

friendly fire(aka RTFM)

Write

Concern

READ

PREFERENCE

ATOMIC

DOCUMENT

OPERATIONS

AVOID

NATURAL

KEYS ASIDENTIFIERS

Saturday, November 30,

Page 18: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

DESIGN

DESIGN

FOR

QUERYEMBEDDED

DATA

vsReferences

DBREFS

VS

MANUALREFERENCE

DYNAMIC

SCHEMA

VSstatic

languages

PURE DRIVER

VS

MAPPINGFRAMEWORKS

BE

CAREFUL

WITH DATES

SPLIT DATA

ON

MULTIPLECOLLECTIONS

friendly fire(aka RTFM)

Write

Concern

READ

PREFERENCE

ATOMIC

DOCUMENT

OPERATIONS

AVOID

NATURAL

KEYS ASIDENTIFIERS

PERFORMANCE

PREALLOCATE

FIELDS?

be aware

of

the trees

PREPROCESS

HIGH

RESOLUTIONDATA

TUNING

UPDATES

ANDINSERTS

DOCUMENT

MOVING

SLOWSYOU

Saturday, November 30,

Page 19: Application Design per MongoDB

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

FRIENDLY FIRE

Saturday, November 30,

Page 20: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

ATOMIC

DOCUMENT

OPERATIONS

Saturday, November 30,

Page 21: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

ATOMIC

DOCUMENT

OPERATIONS

OPERATIONS ON MULTIPLE DOCUMENTS ARE NOT ATOMIC

Saturday, November 30,

Page 22: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

ATOMIC

DOCUMENT

OPERATIONS

OPERATIONS ON MULTIPLE DOCUMENTS ARE NOT ATOMIC

NO “ALL OR NOTHING”

Saturday, November 30,

Page 23: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - 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

Saturday, November 30,

Page 24: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - 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

Saturday, November 30,

Page 25: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

“Describes the guarantee that

MongoDB provides when reporting on the success of a write

operation”

Write

Concern

Saturday, November 30,

Page 26: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

“Describes the guarantee that

MongoDB provides when reporting on the success of a write

operation”

Write

Concern

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

Saturday, November 30,

Page 27: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

“Describes the guarantee that

MongoDB provides when reporting on the success of a write

operation”

Write

Concern

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

Errors Ignored

Saturday, November 30,

Page 28: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

“Describes the guarantee that

MongoDB provides when reporting on the success of a write

operation”

Write

Concern

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

Errors Ignored Unacknowledged

Saturday, November 30,

Page 29: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

“Describes the guarantee that

MongoDB provides when reporting on the success of a write

operation”

Write

Concern

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

Errors Ignored Unacknowledged

Acknowledged (*)

Saturday, November 30,

Page 30: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

“Describes the guarantee that

MongoDB provides when reporting on the success of a write

operation”

Write

Concern

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

Errors Ignored Unacknowledged

Acknowledged (*) Journaled

Saturday, November 30,

Page 31: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

“Describes the guarantee that

MongoDB provides when reporting on the success of a write

operation”

Write

Concern

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

Saturday, November 30,

Page 32: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

“IT describes how MongoDB clients

route read operations to members

of a replica set”

Read

Preference

Saturday, November 30,

Page 33: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

“IT describes how MongoDB clients

route read operations to members

of a replica set”

Read

Preference

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

Saturday, November 30,

Page 34: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

“IT describes how MongoDB clients

route read operations to members

of a replica set”

Read

Preference

primary (*)

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

Saturday, November 30,

Page 35: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

“IT describes how MongoDB clients

route read operations to members

of a replica set”

Read

Preference

primary (*) primary Preferred

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

Saturday, November 30,

Page 36: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

“IT describes how MongoDB clients

route read operations to members

of a replica set”

Read

Preference

primary (*) primary Preferred

secondary

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

Saturday, November 30,

Page 37: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

“IT describes how MongoDB clients

route read operations to members

of a replica set”

Read

Preference

primary (*) primary Preferred

secondary secondary PREFERRED

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

Saturday, November 30,

Page 38: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

“IT describes how MongoDB clients

route read operations to members

of a replica set”

Read

Preference

primary (*)

nearest

primary Preferred

secondary secondary PREFERRED

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

Saturday, November 30,

Page 39: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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.

AVOID

NATURAL

KEYS ASIDENTIFIERS

Saturday, November 30,

Page 40: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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.

AVOID

NATURAL

KEYS ASIDENTIFIERS

ADD AN UNIQUE INDEX ON THE NATURAL KEY, SOMETIMES THE APPLICATION REALM CAN EVOLVE IN AN UNEXPECTED WAY

Saturday, November 30,

Page 41: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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.

AVOID

NATURAL

KEYS ASIDENTIFIERS

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

Saturday, November 30,

Page 42: Application Design per MongoDB

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

DESIGN

Saturday, November 30,

Page 43: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

DESIGN

FOR

QUERY

Saturday, November 30,

Page 44: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

DESIGN

FOR

QUERY

REFERENCE OR EMBED DOCUMENTS,

“denormalized” is not always

a bad word

Saturday, November 30,

Page 45: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

DESIGN

FOR

QUERY

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

Saturday, November 30,

Page 46: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

EMBEDDED

DATA

vsReferences

Saturday, November 30,

Page 47: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

EMBEDDED

DATA

vsReferences

USUALLY there is a “contains” relation

between the embedding and the embedded object

Saturday, November 30,

Page 48: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

EMBEDDED

DATA

vsReferences

The maximum BSON document size is 16 megabytes and embedding may lead to performance issues if not correctly used

USUALLY there is a “contains” relation

between the embedding and the embedded object

Saturday, November 30,

Page 49: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

Normalized data models describe relationships using references between documents

EMBEDDED

DATA

vsReferences

Saturday, November 30,

Page 50: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

Normalized data models describe relationships using references between documents

EMBEDDED

DATA

vsReferences

References provides more flexibility than embedding but remember that client-side applications will have to lookup for referenced objects with multiple queries

Saturday, November 30,

Page 51: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

Normalized data models describe relationships using references between documents

EMBEDDED

DATA

vsReferences

NO Referential integrity is supported, references could point to a not existing object

References provides more flexibility than embedding but remember that client-side applications will have to lookup for referenced objects with multiple queries

Saturday, November 30,

Page 52: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

DBREFS

VS

MANUALREFERENCE

Saturday, November 30,

Page 53: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

DBREFS

VS

MANUALREFERENCE

MANUAL REFERENCES are just fields that will hold the id of the related document, without the collection name or the db name

Saturday, November 30,

Page 54: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

DBREFS

VS

MANUALREFERENCE

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

Saturday, November 30,

Page 55: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - 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

Saturday, November 30,

Page 56: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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.

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

Saturday, November 30,

Page 57: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

SPLIT DATA

ON

MULTIPLECOLLECTIONS

Saturday, November 30,

Page 58: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

SPLIT DATA

ON

MULTIPLECOLLECTIONS

use collections as namespaces for your data

Saturday, November 30,

Page 59: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

SPLIT DATA

ON

MULTIPLECOLLECTIONS

use collections as namespaces for your data

remember once data is partioned it will be more hard to aggregate if needed

Saturday, November 30,

Page 60: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

DYNAMIC

SCHEMA

VSstatic

languages

Saturday, November 30,

Page 61: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

DYNAMIC

SCHEMA

VSstatic

languages

inheritance is not only a matter of hierarchy, it could be also a matter of composition

Saturday, November 30,

Page 62: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

DYNAMIC

SCHEMA

VSstatic

languages

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

Saturday, November 30,

Page 63: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

PURE DRIVER

VS

MAPPINGFRAMEWORKS

Saturday, November 30,

Page 64: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

PURE DRIVER

VS

MAPPINGFRAMEWORKS

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

Saturday, November 30,

Page 65: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

PURE DRIVER

VS

MAPPINGFRAMEWORKS

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?

Saturday, November 30,

Page 66: Application Design per MongoDB

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

PERFORMANCE

Saturday, November 30,

Page 67: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document

be aware

of

the trees

Saturday, November 30,

Page 68: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document

be aware

of

the trees

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

Saturday, November 30,

Page 69: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document

be aware

of

the trees

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

Single Field

Saturday, November 30,

Page 70: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document

be aware

of

the trees

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

Single Field Compound

Saturday, November 30,

Page 71: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document

be aware

of

the trees

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

Single Field Compound

Multikey

Saturday, November 30,

Page 72: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document

be aware

of

the trees

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

Single Field Compound

Multikey Geospatial

Saturday, November 30,

Page 73: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document

be aware

of

the trees

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

Single Field Compound

Multikey Geospatial

TEXT (BETA)

Saturday, November 30,

Page 74: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document

be aware

of

the trees

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

Single Field Compound

Multikey Geospatial

TEXT (BETA) Hashed

Saturday, November 30,

Page 75: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document

be aware

of

the trees

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

Saturday, November 30,

Page 76: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

DOCUMENT

MOVING

SLOWSYOU

Saturday, November 30,

Page 77: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

DOCUMENT

MOVING

SLOWSYOU

WHEN AN UPDATED DOCUMENT DOES NOT FIT IN THE RECORD SPACE IT WILL BE MOVED

Saturday, November 30,

Page 78: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

DOCUMENT

MOVING

SLOWSYOU

WHEN AN UPDATED DOCUMENT DOES NOT FIT IN THE RECORD SPACE IT WILL BE MOVED

DYNAMIC SCHEMA IS THE FIRST CAUSE OF DOCUMENT MOVING

Saturday, November 30,

Page 79: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

PREALLOCATE

FIELDS?

Saturday, November 30,

Page 80: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

PREALLOCATE

FIELDS?

Default values must be used to preallocate, this MUST BE HANDLEDin the application

Saturday, November 30,

Page 81: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

PREALLOCATE

FIELDS?

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

Saturday, November 30,

Page 82: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

MONGODB let you store the maximum resolution of your data

PREPROCESS

HIGH

RESOLUTIONDATA

Saturday, November 30,

Page 83: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

MONGODB let you store the maximum resolution of your data

PREPROCESS

HIGH

RESOLUTIONDATA

MAP REDUCE and aggregation ARE okbut you could also preprocess and have aggregated data that you can use for your queries

Saturday, November 30,

Page 84: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

MONGODB let you store the maximum resolution of your data

PREPROCESS

HIGH

RESOLUTIONDATA

MAP REDUCE and aggregation ARE okbut you could also preprocess and have aggregated data that you can use for your queries

MONGODB rocks for business intelligence

Saturday, November 30,

Page 85: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

TUNING

UPDATES

ANDINSERTS

Saturday, November 30,

Page 86: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

TUNING

UPDATES

ANDINSERTS

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

Saturday, November 30,

Page 87: Application Design per MongoDB

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

Alessandro Palumbo - [email protected] - http://www.byte-code.com

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

TUNING

UPDATES

ANDINSERTS

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

Saturday, November 30,

Page 88: Application Design per MongoDB

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

Any questions?

Saturday, November 30,

Page 89: Application Design per MongoDB

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

Any questions?

Saturday, November 30,


Recommended