Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

173
Polyglot Persistance Esempio di architettura basata su PostgreSQL, CouchDB, MongoDB, Redis e OrientDB DiTeDi, Udine - Italia 15-12-2012

description

Pirma parte del seminario su NoSQL al DiTeDi di Udine del 15/12/2012. Affrontato il caso di studio di un'architettura enterprise, basata su datastore relazionali (PostgreSQL) e non (CouchDB, MongoDB, Redis e OrientDB).

Transcript of Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Page 1: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Polyglot Persistance Esempio di architettura basata

su PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

DiTeDi, Udine - Italia 15-12-2012

Page 2: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

@maraspin

Page 3: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

@stefanovalle

Page 4: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

http://www.mvassociati.it/

Page 5: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

IL CONTESTO

Page 6: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

L’OBIETTIVO

Page 7: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB
Page 8: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

DATI IN TEMPO REALE

Page 9: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

REPERIMENTO FEEDBACK IMMEDIATO

Page 10: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

MENO HARDWARE NEGLI HOTEL

Page 11: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Partiamo da stack LAMP/LAPP

11

Page 12: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

PostgreSQL Backend

12

Page 13: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Chi usa PostgreSQL?

13

Page 14: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Gestione "meta" dati di: • Hotel • Applicazioni • Configurazioni • Personalizzazioni • Utenti • ACL • RELAZIONI tra essi

14

Page 15: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Modello relazionale

15

Film

TitoloNome CategoriaRating CategoriaAnnoVisioniGradimentiVoto

Page 16: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Diamo un’occhio ai dati…

16

E se voglio cambiare rating per i drammatici?

Page 17: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Normalizzazione

17

Page 18: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Schema aggiornato

18

Page 19: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Come sono memorizzati i dati

19

Page 20: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

E se vogliamo un report?

20

SELECT film_normal.* , categoria.* FROM film_normal JOIN categoria on categoria.id = film_normal.categoria_id

Page 21: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

JOIN: Operazione impegnativa

21

X

Morale: considerare sempre i benefici della normalizzazione. Ci torniamo tra poco!

Page 22: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Indici

22

EXPLAIN ANALIZE SELECT …

O(N) -> O(log N)

Page 23: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

PostgreSQL vs MySQL

23

Idea: SELECT titolo, max(visioni) FROM film;

Qual’è il film più visto?

Page 24: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

MySQL

24

SELECT titolo, max(visioni) FROM film;

Page 25: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

PostgreSQL

25

SELECT titolo, max(visioni) FROM film;

SELECT titolo, max(visioni) FROM film;

SQL error: ERROR: column "film.titolo" must appear in the GROUP BY clause or be used in an aggregate function at character 8 In statement: SELECT titolo, max(visioni) FROM film;

Page 26: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

PostgreSQL

26

SELECT titolo, max(visioni) FROM film;

SELECT titolo, max(visioni) FROM film;

SQL error: ERROR: column "film.titolo" must appear in the GROUP BY clause or be used in an aggregate function at character 8 In statement: SELECT titolo, max(visioni) FROM film;

Page 27: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

27

Disclaimer: Ciò non significa che non utilizziamo anche MySQL in determinati contesti

Ocio però!

Page 28: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Perchè cio?

28

Risultato Casuale

SELECT titolo, max(visioni) FROM film;

Page 29: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Tabelle Sparse

29

Page 30: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Possibile Soluzione

30

CREATE VIEW film AS SELECT titolo, anno, visioni FROM film_provider1 UNION SELECT titolo, anno, visioni FROM film_provider2;

Page 31: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

POI SU UPDATE…

Page 32: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Alternativa

32

Page 33: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Supporto per Ereditarietà

33

CREATE TABLE film ( id integer NOT NULL, titolo character varying(50), anno integer, visioni integer ); CREATE TABLE film_provider1 ( gradimenti integer ) INHERITS (film); CREATE TABLE film_provider2 ( voto integer ) INHERITS (film);

Page 34: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Comportamento dell’Ereditarietà

34

select * from film; select * from film_provider1;

Page 35: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Write Ahead Log (Journal) • Apri il Log • Scrivici informazioni tipo “Questo dato va

qui (offset)” • Scrivi il file • Tenta il salvataggio su disco (fsync) • Modifica i dati • Segna sul Log che le operazioni sono

state compiute

35

http://www.depesz.com/2011/07/14/write-ahead-log-understanding-postgresql-conf-checkpoint_segments-checkpoint_timeout-checkpoint_warning/

Page 36: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Durevolezza dei Dati

36

Page 37: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

MVCC

37

Supporto AutoVacuum

Page 38: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Replicazione con LOG – 8.4

38

Page 39: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Replicazione Streaming - 9.0

39

Page 40: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Replicazione Sincrona - 9.1

40

Page 41: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Failover

41

Page 42: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Cosa portare a casa: • Solido e Affidabile (Ingres 1974) • Potenti strumenti di programmazione

(PL/PGSQL, PL/Python, PL/Java, ...) • Object-Relational Database (eredità) • Interessanti add-ons (IE fuzzystrmatch) • Replicazione Master/Slave Integrata • PostGIS • Community estesa, know how consolidato

42

Page 43: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Da dove eravamo partiti?

43

Page 44: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB
Page 45: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

E SE…

Page 46: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

DISTRIBUZIONE DATI E LOGICA

Page 47: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

47

Page 48: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

COERENZA DEI DATI

Page 49: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

DISPONIBILITÀ DEL SERVIZIO

Page 50: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Non potete avere tutto, ragazzi!

Prof. Eric Brewer http://www.cs.berkeley.edu/~brewer/

Page 51: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Se volete distribuire una base dati, qualora si venga a creare un partizionamento, dovete scegliere tra coerenza dei dati e disponibilità del servizio.

CAP Theorem, Nancy Lynch and Seth Gilbert, “Brewer's conjecture and the feasibility of consistent, available, partition-tolerant web services”, ACM SIGACT News, Volume 33 Issue 2 (2002), pg. 51-59.

Page 52: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Vediamo che significa

52

Page 53: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Vediamo che significa

53

Uno o più nodi non raggiungibili: partizonamento

Page 54: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Vediamo che significa

54

Hotel Farm

Uno o più nodi non raggiungibili: partizonamento

Page 55: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

55

Page 56: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

RINUNCIA ALLA DISPONIBILITÀ?

Page 57: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Non scordiamo neppure la latenza!

Page 58: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

CAP Theorem

58

Consistency

Availability Partition Tolerance

Page 59: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Soluzioni disponibili

59

Consistency

Availability Partition Tolerance

Categoria CP: BigTable Hbase MongoDB Redis Memcached etc.

Categoria CA: RDBMS etc.

Categoria AP: DynamoDB CouchDB Cassandra SimpleDB Tokyo Cabinet Riak Voldemort etc.

Page 60: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Soluzioni disponibili

60

Consistency

Availability Partition Tolerance

Categoria CP: BigTable Hbase MongoDB Redis Memcached etc.

Categoria CA: RDBMS etc.

Categoria AP: DynamoDB CouchDB Cassandra SimpleDB Tokyo Cabinet Riak Voldemort etc.

Page 61: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Come abbiamo scelto? • Supporto multi-master • Facilità di sincronizzazione applicazione • Flessibilità del modello dati • Semplicità

61

Page 62: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Soluzioni disponibili

62

Consistency

Availability Partition Tolerance

Categoria CP: BigTable Hbase MongoDB Redis Memcached Scalaris etc.

Categoria CA: RDBMS GreenPlum etc.

Categoria AP: DynamoDB CouchDB Cassandra SimpleDB Tokyo Cabinet Riak Voldemort etc.

Page 63: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

63

“designed with the web in mind”

Page 64: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Cos’è CouchDB? • Datastore Documentale (JSON) • Interfaccia HTTP • Replicazione master-master (con filtri) • Può contenere applicazioni Web

HTML/CSS/JS (Couchapp) • Pensato per contesti distribuiti • Un progetto dalla storia “travagliata”

64

Page 65: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Incontriamo Futon (demo live)

65

Page 66: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Schemaless

66

Page 67: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Schemaless

67

Page 68: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Replicazione Semplice

68

Supporto per modalità PULL e PUSH

Page 69: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Ma Semplice Davvero!

69

Aggiornati non solo i dati ma anche le viste (design docs)

Page 70: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Scenario di conflitto

70

CouchDB – The definitive guide – O’Reilly

Page 71: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Risoluzione del Conflitto

71

CouchDB – The definitive guide – O’Reilly

Page 72: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Versioni • ID / _rev • Meglio generare da se gli ID (sequenziali) • Aggiornamenti atomici sui documenti • Aggiornamento ottimista, MVCC • Coerenza Eventuale

72

Page 73: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

POSSIAMO RISOLVERE I CONFLITTI

Page 74: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

O CERCARE DI EVITARLI

Page 75: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Progettiamo per evitarli

75

{

"titolo": "Il Padrino",

"sommario": "..."

"visioni": 3873,

"gradimenti": 185,

}

Provider Camera 1

Camera 2

Page 76: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Progettiamo per evitarli

76

{

"titolo": "Il Padrino",

"sommario": "...",

"visioni": 3873,

"gradimenti": 185,

}

Provider Camera 1

Camera 2

{

"titolo": "Il Padrino",

"sommario": "...",

"visioni": 3873,

"gradimenti": 185,

}

Page 77: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Progettiamo per evitarli

77

{

"titolo": "Il Padrino",

"sommario": "..."

}

Provider Camera 1

Camera 2

{

"titolo": "Il Padrino",

"visioni": 3873,

"gradimenti": 185,

}

MVCC

Page 78: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Progettiamo per evitarli

78

{

"titolo": "Il Padrino",

"sommario": "..."

}

Provider Camera 1

Camera 2

{

"titolo": "Il Padrino",

"visioni": 3873,

"gradimenti": 185,

}

{

"titolo": "Il Padrino",

"visioni": 3873,

"gradimenti": 185,

}

{

"titolo": "Il Padrino",

"sommario": "..."

}

Page 79: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

79

Page 80: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

80

Page 81: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

CouchApps

81

Page 82: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

LOGICA LATO CLIENT

Page 83: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

SVILUPPATORI

SERVER-SIDE

Page 84: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

84

Page 85: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

RELAZIONI

METADATI

Page 86: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Classico Backend

86

Page 87: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

WebApps Generate Server Side

87

Page 88: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

88

Page 89: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

E QUINDI PRESENTATE AI CLIENT

Page 90: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Alto consumo di disco

90

0

2000

4000

6000

8000

10000

12000

14000

16000

DB Size (MB)

NB Quanto sopra su update!

Page 91: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Btree+, che Succede?

91

Questione di affidabilità, performance

Page 92: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

COMPATTIAMO QUANDO POSSIBILE…

Page 93: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Indicatori di Performance • 350 evt/sec in inserimento • 3000 evt/sec se in batch mode • 100 evt/sec su update • 10 evt/sec durante compattazione

93

• NB: dati forniti unicamente per dare ordini di grandezza. Test eseguiti su server entry level IBM x3550 M3, 1x3.60 GHz Xeon, 4GB RAM, Dischi SAS in RAID 0

• CouchDB configurato con httpd max_connections = 2048, export ERL_MAX_PORTS=4096, export ERL_FLAGS="+A 4«, fsync

Page 94: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

94

Page 95: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

95

Page 96: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

DI COSA AVREMMO BISOGNO?

Page 97: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

SHARDING

Page 98: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Sharding

98

Page 99: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Da considerare: • Inerentemente più rischioso • Più onerosa la manutenzione e il backup

degli shard • Delicato decidere con quali criteri

• In SQL: “Join” tra shards costosi.

Replicazione delle tabelle “globali” – es. categorie film

99

Page 100: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Una possibile soluzione

100

Page 101: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Recap su CouchDB: • Ottima soluzione per contesti distribuiti • Non adatto per situazioni con frequenti

aggiornamenti e carichi (ingenti) costanti • API REST aiuta a scalare le letture • Bigcouch in aiuto sulle scritture • Architetture innovative con le Couchapp • Si possono usare strumenti Tipici Web:

HAproxy, Nginx

101

Page 102: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Da Tenere a Mente: • Attenzione alla generazione degli ID,

specie se distribuiti • Lo spazio su disco cresce e la

compattazione «inchioda» lo strumento • I «driver» disponibili non sempre sono di

alta qualità...

102

Page 103: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Da Tenere sotto controllo: • TouchDB

database CouchDB compatibile, adatto all’embedding in device mobili

• PouchDB libreria js: come avere CouchDB nel browser

103

Page 104: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Ulteriori Esigenze

104

Page 105: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

“L’aggregatore”

105

RSS

Previsioni meteo

Vimeo

Eventi

...

Aggregatore

Page 106: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Architettura dell’aggregatore

106

RSS

Previsioni meteo

Vimeo

Eventi

...

REST API Crawler

CDN

Page 107: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Cosa scegliamo?

107

RSS

Previsioni meteo

Vimeo

Eventi

...

REST API Crawler

CDN

?

Page 108: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Cosa ci serve? • Flessibilità del modello dati • Semplicità • Facilità di dialogo con PHP • Scalabilità sulle letture • Flessibilità di query

108

Page 109: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Perché no CouchDB? • Flessibilità del modello dati • Semplicità • Facilità di dialogo con PHP • Scalabilità sulle letture • Flessibilità di query

109

Page 110: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

INTERAGIRE CON COUCHDB

Page 111: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Quante visioni hanno avuto i film in totale?

Page 112: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

MAP REDUCE

Page 113: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

113

{

"_id": "39c7c57daddba704c2b04606de001373",

"info_type": "view",

"movie": "The Gladiator",

"views": 37

}

{

"_id": "39c7c57daddba704c2b04606de000a2f",

"info_type": "view",

"movie": "Spiderman",

"views": 10

}

{

"_id": "39c7c57daddba704c2b04606de000c92",

"info_type": "view",

"movie": "Shrek",

"views": 25

}

Page 114: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

114

{

"movie": "Spiderman",

"views": 10

}

{

"movie": "The Gladiator",

"views": 37

}

{

"movie": "Shrek",

"views": 25

}

Page 115: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

115

{

"movie": "Spiderman",

"views": 10

}

{

"movie": "The Gladiator",

"views": 37

}

{

"movie": "Shrek",

"views": 25

}

10 37 25

Page 116: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

116

{

"movie": "Spiderman",

"views": 10

}

{

"movie": "The Gladiator",

"views": 37

}

{

"movie": "Shrek",

"views": 25

}

10 37 25

47 25

Page 117: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

117

{

"movie": "Spiderman",

"views": 10

}

{

"movie": "The Gladiator",

"views": 37

}

{

"movie": "Shrek",

"views": 25

}

10 37 25

47 25

72

Page 118: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

118

{

"movie": "Spiderman",

"views": 10

}

{

"movie": "The Gladiator",

"views": 37

}

{

"movie": "Shrek",

"views": 25

}

10 37 25

47 25

72

MAP

Page 119: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

119

{

"movie": "Spiderman",

"views": 10

}

{

"movie": "The Gladiator",

"views": 37

}

{

"movie": "Shrek",

"views": 25

}

10 37 25

47 25

72

REDUCE

Page 120: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

In CouchDB

120

function(doc) {

if (doc.info_type == 'view') {

emit(doc.movie, doc.views);

}

}

Map:

{

"_id": "39c7c57...",

"info_type": "view",

"movie": "The Gladiator",

"views": 37

}

Page 121: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Cosa fa il map?

121

Map: function(doc) {

if (doc.info_type == 'view') {

emit(doc.movie, doc.views);

}

}

{

"movie": "Spiderman",

"views": 10

}

{

"movie": "The Gladiator",

"views": 37

}

{

"movie": "Shrek",

"views": 25

}

10 37 25

Page 122: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Cosa fa il reduce?

122

Reduce: function (key, values, rereduce) {

return sum(values);

}

10 37 25

47 25

Page 123: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Dov’è il problema?

123

function(doc) {

if (doc.info_type == 'config') {

for (param in doc.params) {

emit(param, doc.params[param]);

}

}

}

Map:

Reduce: .... Non possiamo avere parametri in input!

Viste precalcolate su ciascun documento

Page 124: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

124

“Hu(mongo)us”

Page 125: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

About MongoDB • Datastore Documentale (JSON) • Database / Collezioni • Protocollo Binario • Tanto Marketing • Veloce quasi come /dev/null :-P • Update in place -> locks! • Flessibilità nelle query • Autosharding

125

Page 126: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Interagire con il DB è banale

126

Page 127: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Attenzione!

127

Page 128: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Query

128

// operatori di confronto

db.notizie.find({tipo: {$ne: ‘cnn’},

ora_reperimento: {$gt: 1352505039}

});

// eventi di oggi e (necessariamente) domani

db.evento.find( { data: { $all: [ ‘2012-11-10’,

‘2012-11-11’ ] } } );

// eventi di oggi e/o domani

db.evento.find( { data : { $in : [‘2012-11-10’

‘2012-11-11’ ] } } );

// eventi di una giornata sola

db.evento.find( { data : { $size: 1 } } );

// cerca dentro gli array

db.previsioni.find( { ‘settimana.temp_max’ : 20 } );

Page 129: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Performance VS Durevolezza

129

Le impostazioni predefinite (32bit e <v2.0) sono “rischiose” (no journal)

Page 130: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Performance VS Durevolezza

130

MongoDB a 32bit NON VA USATO in PRODUZIONE!

Page 131: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Replica Set

131

http://docs.mongodb.org/manual/core/replication/

Page 132: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Automated Failover

132

Page 133: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Recap su MongoDB: • Semplice (arma a doppio taglio!) • Linguaggio d’interrogazione potente • Ottima documentazione • Flessibilità nella configurazione • Driver disponibili per molti linguaggi • Sviluppo molto attivo • Supporto

133

Page 134: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Fare attenzione a: • Opzione safe, journaling • Si inchioda negli ordinamenti se non

definiti indici • Attenzione ai nomi! • Non farsi coinvolgere e cercare di

costruirci sopra DB relazionali

134

Page 135: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

135

Page 136: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

TUTTO A POSTO SIGNORE!

Page 137: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Cosa abbiamo considerato? • Performance • Semplicità • Expiration automatico dei valori • Gestione del cold start • Non è un problema la perdita di dati

-> Ok persistenza volatile, no replicazione

137

Page 138: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Perchè no Couch/Mongo? • Couch occupa troppo spazio, troppo lento • MongoDB poteva starci (soprattutto

perché TTL introdotte da poco – 12/09); • Ma ci sono soluzioni più semplici…

138

Page 139: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

139

“An open source, advanced key-value store”

Page 140: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

About Redis • Key/Value++ • Progetto Italiano (Salvatore Sanfilippo) • Protocollo Binario • Sponsorizzato da VMWare • Gestione expiry delle chiavi

140

Page 141: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Esempi di query

141

// Memorizzare un valore

> SET status ok

// Collezioni

> SADD luci_accese camera bagno

// Valore con scadenza prefissata

> SET status ok

> EXPIRE status 10 // in secondi

Page 142: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Sorted Sets

142

Page 143: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Recap su Redis: • E’ molto veloce • Salva su RAM (snapshot su disco) • E’ un’ottima soluzione di caching,

evitando problema cold start • Necessario avere abbastanza RAM • Può essere usato per delegare alcuni

calcoli costosi

143

Page 144: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

144

Page 145: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

COME STANNO ANDANDO LE COSE?

Page 146: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Cosa ci serve? • Gestire grandi moli di dati • Modello di dati che supporti correlazione

146

Page 147: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Com’è fatta una nostra app?

147

Page 148: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

E una sessione d’uso?

148

Page 149: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

E una sessione d’uso?

149

Page 150: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

E una sessione d’uso?

150

Page 151: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

E una sessione d’uso?

151

Page 152: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Cosa ci ricorda tutto ciò?

152

Page 153: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

G=(V,E)

Page 154: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Un semplcie grafo

154

Page 155: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Con strumenti visti prima… • Query di Couch poco flessibili • Redis assenza indici secondari • MongoDB e Postgres forse ok ma limiti

nel traversare nodi non immediatamente adiacenti

155

Page 156: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Troviamo le visite a partire dalla

dashboard in un determinato periodo

Page 157: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Con RDBMS

157

SELECT p1 . * , p2 . * , v . *

FROM pagina AS p1

JOIN visita AS v ON v.pagina_da = p1.id

JOIN pagina AS p2 ON v.pagina_a = p2.id

WHERE p1.id =1

AND v.data_visita >0

AND v.data_visita <4

Page 158: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Il problema…

158

SELECT p1 . * , p2 . * , v . *

FROM pagina AS p1

JOIN visita AS v ON v.pagina_da = p1.id

JOIN pagina AS p2 ON v.pagina_a = p2.id

WHERE p1.id =1

AND v.data_visita >0

AND v.data_visita <4

O(log N) O(log M) O(log N)

Page 159: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Invece con un grafo:

159

O(1)

O(1)

Adiacenza senza necessità di consultare un indice!

Page 160: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

160

“Multiple datamodels support”

Page 161: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Modello Dati

161

Page 162: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

https://github.com/tinkerpop/blueprints/wiki/Property-Graph-Model

Page 163: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Interagire con OrientDB • Protocollo binario / REST • JSON • Tinkerpop Stack: Gremlin, Pipes

• SQL(+)

163

Page 164: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Select titolo, in.out.in.nome from #16:5

Page 165: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Replica Master-Master

Sync/Async - Configurabile 165

Page 166: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Recap su OrientDB:

• Ottima soluzione per gestire relazioni • Traversa 5 Milioni+ di record/sec • Progetto Italiano (Luca Garulli) • ACID • Supporta ereditarietà • Semplice da installare e avviare

166

Page 167: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

167

Page 168: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Ringraziamento

168

Il know how utilizzato per la preparazione di questa presentazione è stato in buona parte acquisito durante lo sviluppo del sistema Hotel OnAir di concezione e proprietà di VDA Multimedia Spa, cui il case study di cui si è fatto accenno fa riferimento. Ringraziamo il team leader del progetto, Stefano Brenelli, e tutto il team di sviluppo, per l'interessante, edificante e proficua collaborazione. In particolare ringraziamo: Carlo Anselmi, Maurizio Battistella, Manuel Bitto, Nicola Busanello, Antonino Murador, Antonio Parrella, Nicola Pressi, Riccardo Zamuner, Michele Zanon, Tiziano Zonta. Ringraziamo anche i colleghi Diego Drigani e Dario Tion, nonchè tutto il PUG Friuli per i sempre utili confronti e consigli.

Page 169: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

DOMANDE?

Page 170: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

http://friuli.grusp.org/

Page 171: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

http://www.ditedi.it/

Page 172: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Image Credits • http://www.flickr.com/photos/leandrociuffo/4128603357/ • http://www.flickr.com/photos/uggboy/8043043095/ • http://www.flickr.com/photos/kky/704056791/ • http://www.flickr.com/photos/shareandenjoy/7074965023/ • http://www.flickr.com/photos/ivyfield/4497654857/ • http://www.flickr.com/photos/craigmarren/6086280669/ • http://www.flickr.com/photos/getbutterfly/6317955134/ • http://www.flickr.com/photos/62698615@N08/5948655391/ • http://20bits.com/article/interview-questions-database-indexes • http://www.flickr.com/photos/iita-media-library/4808291918/ • http://www.flickr.com/photos/47108884@N07/6949078701/ • http://www.flickr.com/photos/rrrodrigo/4139483872/ • http://www.flickr.com/photos/latt/509790815/ • http://www.flickr.com/photos/spine/525988813/ • http://www.flickr.com/photos/latt/509790815/ • http://www.flickr.com/photos/antmoose/116845021/ • http://www.flickr.com/photos/charleylhasa/3374499088/ • http://www.flickr.com/photos/mcascherrypoint/6277623762/ • http://www.flickr.com/photos/superk8/2801590280/ • http://www.flickr.com/photos/cawley/1067814030/ • http://www.flickr.com/photos/mr_g_travels/863720665/ • http://www.flickr.com/photos/cortesdecima/6099516346/ • http://www.flickr.com/photos/portofsandiego/7777282856/ • http://www.flickr.com/photos/22750018@N05/4268345597/ • http://www.flickr.com/photos/petereed/132829587/ • http://www.flickr.com/photos/myfruit/7031263653/ • http://www.flickr.com/photos/maugiart/5014963068/ • http://www.flickr.com/photos/freefoto/3844247553/ • http://www.flickr.com/photos/incognito_rico/69242116/ • http://tinkerpop.com/ • http://www.slideshare.net/lvca/orientdb-distributed-architecture-11h • ttp://www.flickr.com/photos/dobs/4128798936/

172

Page 173: Polyglot Persistance con PostgreSQL, CouchDB, MongoDB, Redis e OrientDB

Grazie per l’attenzione

Stefano Maraspin @maraspin [email protected]

Stefano Valle @stefanovalle [email protected]