57
1 Evolvable Application Development with MongoDB Gerd Teniers Bart Wullems for .NET develop

Techorama - Evolvable Application Development with MongoDB

Embed Size (px)

DESCRIPTION

Introduction to MongoDB for .NET developers.

Citation preview

Page 1: Techorama  - Evolvable Application Development with MongoDB

1

Evolvable Application Development with MongoDBGerd Teniers

Bart Wullems

for .NET developers

Page 2: Techorama  - Evolvable Application Development with MongoDB

WARNING – This session is rated as a ‘Grandma session’ (=Level 200)

Page 3: Techorama  - Evolvable Application Development with MongoDB

3 goals of this presentations

When you leave this presentation you should have learnedHow easy it is to get started using MongoDB

How using MongoDB changes the way you design and build your applications

How MongoDB’s flexibility supports evolutionary design

That giving speakers beer before a session is never a good idea

Page 4: Techorama  - Evolvable Application Development with MongoDB

What is not cool?

White socks & sandals

Page 5: Techorama  - Evolvable Application Development with MongoDB

What is not cool?

Dancing like Miley Cyrus

Page 6: Techorama  - Evolvable Application Development with MongoDB

What is not cool?

Relational databases

Page 7: Techorama  - Evolvable Application Development with MongoDB

What is cool?

Short pants and very large socks

Page 8: Techorama  - Evolvable Application Development with MongoDB

What is cool?

Dancing like Psy

Page 9: Techorama  - Evolvable Application Development with MongoDB

What is cool?

NO-SQL (=Not Only SQL)

Page 10: Techorama  - Evolvable Application Development with MongoDB

ThoughtWork Technology Radar

Page 11: Techorama  - Evolvable Application Development with MongoDB

Entity Framework 7 will support No-SQL

Page 12: Techorama  - Evolvable Application Development with MongoDB

Gartner

Page 13: Techorama  - Evolvable Application Development with MongoDB

What is MongoDB?

Page 14: Techorama  - Evolvable Application Development with MongoDB

MongoDB

HuMongous

General purpose database

Document oriented database using JSON document syntax

Features:

- Flexibility

- Power

- Scaling

- Ease of Use

- Built-in Javascript

Users: Craigslist, eBay, Foursquare, SourceForge, and The New York Times.

Page 15: Techorama  - Evolvable Application Development with MongoDB

Written in C++

Extensive use of memory-mapped files

i.e. read-through write-through memory caching.

Runs nearly everywhere

Data serialized as BSON (fast parsing)

Full support for primary & secondary indexes

Document model = less work

High Performance

Page 16: Techorama  - Evolvable Application Development with MongoDB

MongoDB Database Architecture: Document

{

_id: ObjectId("5099803df3f4948bd2f98391"),

name: { first: "Alan", last: "Turing" },

birth: new Date('Jun 23, 1912'),

death: new Date('Jun 07, 1954'),

contribs: [ "Turing machine", "Turing test", "Turingery" ],

views : NumberLong(1250000)

}

Page 17: Techorama  - Evolvable Application Development with MongoDB

MongoDB Database Architecture: Collection

Logical group of documents

May or may not share same keys

Schema is dynamic/application maintained

Page 18: Techorama  - Evolvable Application Development with MongoDB

Why should I use it?(or how do I convince my boss?)

Developer productivity

Avoid ORM pain, no mapping needed

Performance(again)

Scaling out is easy(or at least easier)

Optimized for reads

Flexibility

Dynamic schema

Page 19: Techorama  - Evolvable Application Development with MongoDB

How to run it?

Exe

Windows service

Azure

3rd party commercial hosting

Page 20: Techorama  - Evolvable Application Development with MongoDB

How to talk to it?

Mongo shell Official and non official drivers>12 languages supported

Page 21: Techorama  - Evolvable Application Development with MongoDB

DEMO 1 - PROTOTYPING

Page 22: Techorama  - Evolvable Application Development with MongoDB

Schema design

Page 23: Techorama  - Evolvable Application Development with MongoDB

23

First step in any application is determine your domain/entities

Page 24: Techorama  - Evolvable Application Development with MongoDB

In a relational based appWe would start by doing schema design

Page 25: Techorama  - Evolvable Application Development with MongoDB

In a MongoDB based appWe start building our appand let the schema evolve

Page 26: Techorama  - Evolvable Application Development with MongoDB

Comparison

Album- id- artistid- title

Track- no- name- unitPrice- popularity

Artist- id- name

Album- _id- title- artist

- tracks[]

- _id- name

Relational Document db

Page 27: Techorama  - Evolvable Application Development with MongoDB

Modeling

Page 28: Techorama  - Evolvable Application Development with MongoDB

Modeling

Start from application-specific queries

“What questions do I have?” vs “What answers”

“Data like the application wants it”

Base parent documents on

The most common usage

What do I want returned?

Page 29: Techorama  - Evolvable Application Development with MongoDB

Modeling

Embedding vs Linking vs Hybrid

Album- _id- artist

- cover

- _id- name

Artist- _id- name- photo

Page 30: Techorama  - Evolvable Application Development with MongoDB

Product

Single collection inheritance

Product- _id- price

Book- author- title

Album- artist- title

Jeans- size- color

- _id- price- author- title

Relational Document db

- _id- price- size- color

Page 31: Techorama  - Evolvable Application Development with MongoDB

Product

Single collection inheritance

Product- _id- price

Book- author- title

Album- artist- title

Jeans- size- color

_type: Book- _id- price- author- title

Relational Document db

_type: Jeans- _id- price- size- color

Page 32: Techorama  - Evolvable Application Development with MongoDB

One-to-many

Embedded array / array keys

Some queries get harder

You can index arrays!

Normalized approach

More flexibility

A lot less performance

BlogPost- _id- content- tags: {“foo”, “bar”}- comments: {“id1”, “id2”}

Page 33: Techorama  - Evolvable Application Development with MongoDB

Demo 2 – MODELING

Page 34: Techorama  - Evolvable Application Development with MongoDB

CRUD

Page 35: Techorama  - Evolvable Application Development with MongoDB

CRUD operations

Create: insert, save

Read: find, findOne

Update: update, save

Delete: remove, drop

Page 36: Techorama  - Evolvable Application Development with MongoDB

ACID Transactions

No support for multi-document transactions commit/rollback

Atomic operations on document levelMultiple actions inside the same document

Incl. embedded documents

By keeping transaction support extremely simple, MongoDB can provide greater performance

especially for partitioned or replicated systems

Page 37: Techorama  - Evolvable Application Development with MongoDB

Demo 3 – CRUD

Page 38: Techorama  - Evolvable Application Development with MongoDB

GridFS

Page 39: Techorama  - Evolvable Application Development with MongoDB

Storing binary documents

Although MongoDB is a document database, it’s not good for documents :-S

Document != .PNG & .PDF files

Document size is limited

Max document size is 16MB

Recommended document size <250KB

Solution is GridFS

Mechanism for storing large binary files in MongoDB

Stores metadata in a single document inside the fs.files collection

Splits files into chunks and stores them inside the fs.chunks collection

GridFS implementation is handled completely by the client driver

Page 40: Techorama  - Evolvable Application Development with MongoDB

Demo 4 – Evolving your domain model ------------& GRIDFS

Page 41: Techorama  - Evolvable Application Development with MongoDB

Evolving your domain model

Great for small changes!

Hot swapping

Minimal impact on your application and database

Avoid Migrations

Handle changes in your application instead of your database

Page 42: Techorama  - Evolvable Application Development with MongoDB

Performance

Page 43: Techorama  - Evolvable Application Development with MongoDB

Avoid table collections scans by using indexes

> db.albums.ensureIndex({title: 1})

Compound indexes

Index on multiple fields

> db.albums.ensureIndex({title: 1, year: 1})

Indexes have their price

Every write takes longer

Max 64 indexes on a collection

Try to limit them

Indexes are useful as the number of records you want to return are limited

If you return >30% of a collection, check if a table scan is faster

Creating indexes

Page 44: Techorama  - Evolvable Application Development with MongoDB

Aggregations with the Aggregation Framework

$project Select() $unwind SelectMany() $match Where() $group GroupBy() $sort OrderBy() $skip Skip() $limit Take()

Largely replaces the original Map/Reduce

Much faster!

Implemented in a multi-threaded C ++

No support in LINQ-provider yet (but in development)

Page 45: Techorama  - Evolvable Application Development with MongoDB

Demo 5 – Optimizations

Page 46: Techorama  - Evolvable Application Development with MongoDB

Conclusion

Page 47: Techorama  - Evolvable Application Development with MongoDB

Benefits

Scalable: good for a lot of data & traffic

Horizontal scaling: to more nodes

Good for web-apps

Performance

No joins and constraints

Dev/user friendly

Data is modeled to how the app is going to use it

No conversion between object oriented > relational

No static schema = agile

Evolvable

Page 48: Techorama  - Evolvable Application Development with MongoDB

Drawbacks

Forget what you have learned

New way of building and designing your application

Can collect garbage

No data integrity checks

Add a clean-up job

Database model is determined by usage

Requires insight in the usage

Page 49: Techorama  - Evolvable Application Development with MongoDB

https://github.com/wullemsb/DemoTechoramaMongoDb

Page 50: Techorama  - Evolvable Application Development with MongoDB

Things we didn’t talk about

Click icon to add picture

Page 51: Techorama  - Evolvable Application Development with MongoDB

Things we didn’t talk about…

Security

- HTTPS/SSL Compile the code yourself

Eventual Consistency

Geospatial features

Realtime Aggregation

Page 52: Techorama  - Evolvable Application Development with MongoDB

Things we didn’t talk about…

Many to Many

- Multiple approaches References on 1 site

References on both sites

Page 53: Techorama  - Evolvable Application Development with MongoDB

Things we didn’t talk about…

Write Concerns

- Acknowledged vs Unacknowledged writes

- Stick with acknowledged writes(=default)

Page 54: Techorama  - Evolvable Application Development with MongoDB

Things we didn’t talk about…

GridFS disadvantages

- Slower performance: accessing files from MongoDB will not be as fast as going directly through the filesystem.

- You can only modify documents by deleting them and resaving the whole thing.

- Drivers are required

Page 55: Techorama  - Evolvable Application Development with MongoDB

Things we didn’t talk about…

Schema Migrations

- Avoid it

- Make your app backwards compatible

- Add version field to your documents

Page 56: Techorama  - Evolvable Application Development with MongoDB

Things we didn’t talk about…

Why you should not use regexes

- Slow!

Advanced Indexing

- Indexing objects and Arrays

- Unique vs Sparse Indexes

- Geospatial Indexes

- Full Text Indexes

MapReduce

- Avoid it

- Very slow in MongoDB

- Use Aggregation FW instead

Page 57: Techorama  - Evolvable Application Development with MongoDB

Things we didn’t talk about…

Sharding Based on a shard key (= field)

Commands are sent to the shard that includes the relevant range of the data

Data is evenly distributed across the shards

Automatic reallocation of data when adding or removing servers