nosql, which way to go?

44
NoSQL: Which way to go? DDD Melbourne 2015 Himanshu Desai, Ahmed Elharouny & Daniel Janczak

Upload: ahmed-elharouny

Post on 19-Aug-2015

580 views

Category:

Presentations & Public Speaking


0 download

TRANSCRIPT

Page 1: NoSQL, which way to go?

NoSQL: Which way to go?

DDD Melbourne 2015

Himanshu Desai, Ahmed Elharouny & Daniel Janczak

Page 2: NoSQL, which way to go?
Page 3: NoSQL, which way to go?

About us

• Himanshu Desai (@H_Desai)• Ahmed Elharouny(@Harouny)• Daniel Janczak (@DanielJanczak)

And we all work for Readify

Page 4: NoSQL, which way to go?

Page

Why NoSQL?

Scale

Pros NoSQL technologies can offer more scalability than relational databases

ConsOften lose some benefits of relational databases e.g. database-wide transactions

Better Formats

ProsAvoiding ORMs

ConsLimited BI tooling, Data hard to share outside the application

Work in more flexible ways

Pros No fixed schemas

ConsFixed schemas help prevent errors

Page 5: NoSQL, which way to go?

Page

Document DatabaseDatabase

CollectionsCollections

Database

{ “title”: “Justin …”, “genre”: “Pop”, “price”: 36}

Document 1

{ “title”: “Don’t Spe..”, “genre”: “Techno”, “price”: 42.99}

Document 2

{ “numberOfSongs”: 2, “last”: “Jan 1, 2015”}

Document 3

Request

Application

{ }

Page 6: NoSQL, which way to go?

RavenDB

Page 7: NoSQL, which way to go?

What is RavenDB?› Open source NoSQL database for .NET

› Developed by Hibernating Rhinos Ltd

› Available› As a standalone package› As embedded version› On Azure and Amazon

Page 8: NoSQL, which way to go?

Schema-freeStore data as Plain Old C# Object (POCO)

Or directly as JSON

var author = new Author {

Name = "George Martin", Books = new[] {

new Book {Title = "A Game of Thrones"},new Book {Title = "A Clash of Kings"}

} };

session.Store(author);

{ "Name": "George Martin", "Books": [ { "Title": "A Game of Thrones", "Genre": "Fantasy"

}, { "Title": "A Clash of Kings", "Genre": "Fantasy

} ]}

Page 9: NoSQL, which way to go?

Scalable› Replication

› “Keep copies of my data somewhere else”

› Sharding› “Split my huge database into smaller parts”

› Multi-tenancy› “Single instance serves multiple customers = many

databases per server”

Page 10: NoSQL, which way to go?

Extensible› Use existing bundles

› Compression› Encryption› Expiration› Periodic Export› Quotas› Replication› Scripted Index› SQL Replication› Versioning

› Or write your own plugins› Triggers› Codecs› Tasks› Compilation Extensions› Analyzer Generators› Database configuration› Custom sorters

Page 11: NoSQL, which way to go?

Easy to use› .NET and JAVA client

› LINQ to query data

› REST API› GET http://localhost:8080/docs/bobs_address› PUT http://localhost:8080/docs/bobs_address -d "{ FirstName:

'Bob', LastName: 'Smith', Address: '5 Elm St' }“› And so on…

Page 12: NoSQL, which way to go?

Easy to manage› .Built-in management tool – no need to download anything extra

Page 13: NoSQL, which way to go?

Optimized › Transactional (ACID) on document operations

› In plain English “your data is safe”

› But only BASE for queries› “Basically Available, Soft state, Eventual Consistency”› In plain English:

› you will always get the results of your queries as fast as possible

› but the results won’t always be up to date until database finishes indexing

› If you want them up to date, you have to demand it explicitly

Page 14: NoSQL, which way to go?

Eventual consistency Good or bad ?Newspaper Good! Very few writes, thousands of

reads, doesn’t have to be immediateProduct signup Good! All applicant information held

togetherBanking system What if the credits and debits

sometimes don’t match?Patient drug monitoring

What if the drug doses are not updated on time?

Page 15: NoSQL, which way to go?

Our test datapublic class Author {

public string Name {get; set;} public string Surname {get; set;} public Book[] Books {get; set;}

} public class Book {

public string Title {get; set;} public string Genre {get; set;} public DateTime? PublishedOn {get; set;} public decimal? Price {get; set;}

}

Page 16: NoSQL, which way to go?

Our test data continues…• George Martin

• A Game of Thrones, Fantasy, 6/8/1996, $12.34• A Clash of Kings, Fantasy, 16/11/1998

• Dan Simmons• Hyperion, Science Fiction, 1/1/1989, $34.56• Terror, Thriller, 8/1/2007, $23.45

• John Tolkien• Hobbit, Fantasy, 21/9/1937, $12.34

• Stephen King• The Stand, Horror, 1/9/1978, $12.34

Page 17: NoSQL, which way to go?

A quick look at code

Code samples:

https://github.com/danieljanczak/ddd-melb2015-ravendb-samples

Page 18: NoSQL, which way to go?

MongoDB

Page 19: NoSQL, which way to go?

A scalable, high-performance, open source, schema-free, document-oriented database.

- mongodb.org

What is MongoDB?

Page 20: NoSQL, which way to go?

MongoDB Popularity

Source: db-engins.com

Page 21: NoSQL, which way to go?

MongoDB FeaturesThe best features of key/values stores, document databases and relational databases in one.

• Open source

Page 22: NoSQL, which way to go?

MongoDB FeaturesThe best features of key/values stores, document databases and relational databases in one.

• Open source• Cross platform

Page 23: NoSQL, which way to go?

The best features of key/values stores, document databases and relational databases in one.

MongoDB Features

• Open source• Cross platform• Strong indexing

Page 24: NoSQL, which way to go?

The best features of key/values stores, document databases and relational databases in one.

MongoDB Features

• Open source• Cross platform• Strong indexing• Strong query language

Page 25: NoSQL, which way to go?

The best features of key/values stores, document databases and relational databases in one.

MongoDB Features

• Open source• Cross platform• Strong indexing• Strong query language• Replication

Page 26: NoSQL, which way to go?

The best features of key/values stores, document databases and relational databases in one.

MongoDB Features

• Open source• Cross platform• Strong indexing• Strong query language• Replication• Sharding

Page 27: NoSQL, which way to go?

The best features of key/values stores, document databases and relational databases in one.

MongoDB Features

• Open source• Cross platform• Strong indexing• Strong query language• Replication• Sharding• Native drivers

Page 28: NoSQL, which way to go?

The best features of key/values stores, document databases and relational databases in one.

MongoDB Features

• Open source• Cross platform• Strong indexing• Strong query language• Replication• Sharding• Native drivers• File storage (GridFS)

Page 29: NoSQL, which way to go?

The best features of key/values stores, document databases and relational databases in one.

MongoDB Features

• Open source• Cross platform• Strong indexing• Strong query language• Replication• Sharding• Native drivers• File storage (GridFS)• Aggregation framework + MapReduce

Page 30: NoSQL, which way to go?

MongoDB Getting Started1

DOWNLOADMongoDB 64 Bit for Windows(Link)

2

Create Data Directory

3

Start MongoDB

Page 32: NoSQL, which way to go?

DocumentDB

Page 33: NoSQL, which way to go?

How would you access it?

Application

Database

Collections

StoredProcedures Triggers UDFs

Documents

Libraries: .NET, JavaScript, Node.js, Python,

…REST

Collections

Database

Page 34: NoSQL, which way to go?

DocumentDB Working with data

RESTful access methods

For Create/Read/Update/Delete (CRUD) operations

DocumentDB SQL

A query language with SQL-derived syntax

Example:SELECT c.BooksFROM Authors c WHERE c.name = “George"

Executing logic in the DB ( Javascript)

Stored procedures

Triggers

User-defined functions (UDFs)- Allow extending

DocumentDB SQL

Page 35: NoSQL, which way to go?

Sharding and Transaction

Collection

Collection

Collection

Database

JSON

JSON

JSON

JSON

JSON

JSON

JSON

JSON

Collection

JSON

JSON

JSON

The unit of Sharding is a collection is a

collection

Atomic transactions can span only a single collection

collection

Page 36: NoSQL, which way to go?

.NET Development

• You need following from the portal after provision a database• Endpoint Url• Authorization key

• Nu-get package• Install-Package Microsoft.Azure.DocumentDB

• Use them to create DocumentClient

Page 37: NoSQL, which way to go?

Demo

Page 38: NoSQL, which way to go?

Page

In a nutshell

Page 39: NoSQL, which way to go?

In a nutshell - ACID

• RavenDB• Fully transactional writes on document level, even between collections• Reads use eventual consistency• Support for DTS

• MongoDB• Only supports ACID transactions at the document level.• Multi-collection (table) transactions not supported.• Users can choose the level of consistency using (WriteConcern and ReadPreference)

• DocumentDB• Full ACID support on the collection level• 4 levels of consistency, Extremely rich query model.

Page 40: NoSQL, which way to go?

In a nutshell - availability and tooling• RavenDB

• On-prem (Windows), Cloud (RavenHQ) on Azure and Amazon, Embedded version / unit testing• Built in web based Management Studio• .NET and JAVA drivers, REST API• Possible access through LinqPad

• MongoDB• On-prem (Windows, Linx, Mac), Cloud (i.e MongoLab)• MongoDB Cloud Manager (monitoring, automated management)• Mongo Shell• Lots of 3rd party GUIs i.e. Robomongo

• DocumentDB• Accessed from virtually “any” system. Available in “Azure” only at this point.• Azure Web Portal• DocumentDB Studio• LinqPad• SDK available for various platform and REST based access

Page 41: NoSQL, which way to go?

In a nutshell – querying and indexing• RavenDB

• Only very basic queries supported without static indexes• Map-reduce• Full text search• Eventual consistency by default

• MongoDB• Strong indexing (_id indexed by default, custom indexes, compound, unique, text). Async indexing.• Query from primary by default (Strong consistency) • Query from “nearest” if needed (Eventual consistency)• When writing you choose when to consider write operation successful.

• DocumentDB• Very rich query model – Supports Linq to objects, Lambda expression and SQL style syntax. Supports

Stored procedure, triggers and UDFs• Auto index by default.• Support lazy indexing if required.• 4 levels of consistency to choose from (Strong, Session, Bounded Stalesness,Eventual consistency

Page 42: NoSQL, which way to go?

In a nutshell - performance

• RavenDB• Writes directly to disk (safety over speed)• Indexing in the background• Query results are instantly available in indexes• May need to wait for specific update

• MongoDB• TCP/IP based communication.• Uses BSON (lightweight, traversable, efficient).• MMAPv1 storage engine uses OS memory mapped files.• Wiredtiger storage engine (7,000,000 op/second 28 threads).• Choose level of performance vs consistency.

• DocumentDB• 3 Levels of performance offered. • Normalised CPU, memory, Disk and network as Request Units (RU)• TCP/HTTPS based access available

Page 43: NoSQL, which way to go?

In a nutshell – gotchas!

• RavenDB• Need to reset underlying Lucene indexes – they do crash• Database defaults (winning numbers: 15, 50, 16384; 128, 1024)• Static indexes• Eventual consistency

• MongoDB• 32-bit limitations• Max document size 16 MB• Modeling currency

• DocumentDB• Max response size : 1MB• Max request size including attachment: 512KB.• Consistency is set at database level not at collection level.

Page 44: NoSQL, which way to go?

Thank you