Course: Data Science and Database Technologies
Source: Beyond relational databases, Daniele Apiletti (Data Base and Data Mining Group, Politecnico di Torino), local path Beyond-relational-databases-18-12.pdf, 76 PDF pages. Lecture number 21 is a local organizational number because the source filename carries no lecture number (20 is reserved for 20-ElasticSearch_v2.pdf).
Lecture summary
NoSQL (“Not Only SQL”) emerged to address workloads where rigid, predefined schemas, vertical scaling, and complex cross-node joins become bottlenecks. Rather than one uniform data model, non-relational systems trade native joins and fixed schemas for horizontal scalability across commodity servers and four specialized storage families: key-value (Redis, Memcached), column-oriented (Cassandra, HBase), graph (Neo4j), and document (MongoDB, CouchDB). Using CouchDB as a running model, queries are materialized as sorted views built with MapReduce—where pure Map(doc) functions run independently per document (enabling parallel execution and incremental updates) and Reduce(key, values) / Rereduce aggregate sorted key-value outputs. Because CouchDB lacks multi-document ACID transactions, operations that must remain atomic—such as bank transfers—store one immutable transaction document per event and derive balances via MapReduce rather than updating two separate account documents.
Distributing and replicating data across nodes (Master-Slave vs. Master-Master; synchronous vs. asynchronous) exposes the fundamental tradeoff of the CAP Theorem (Brewer; Gilbert & Lynch): during a network partition (P), a system cannot guarantee both single-copy Consistency (C) and Availability (A), forcing a choice between CP (block out-of-sync sides, e.g. MongoDB/HBase) and AP (stay available with eventual consistency, e.g. CouchDB/Cassandra), while “Beyond CAP” notes that partitions are rare and C vs. A varies continuously per operation. This contrasts ACID (C = integrity constraints) with BASE (Basically Available, Soft state, Eventually consistent), where concurrent updates on divergent nodes require application-level conflict logic backed by deterministic DB winner selection (CouchDB picks the longest revision history, breaking ties by highest _rev in ASCII order) over an HTTP RESTful API.
In practice, teams combine specialized engines via Polyglot Persistence—such as MongoDB (flexible JSON sub-documents vs. ObjectId denormalization), ElasticSearch (text search), and Cassandra (IoT sensor streams)—alongside Big Data processing frameworks. Apache Hadoop anchors low-cost commodity clusters on Hadoop Common, HDFS, YARN, and MapReduce, while Apache Spark accelerates iterative, SQL, streaming, ML, and graph workloads up to 100× in memory via an in-memory DAG execution engine.
Learning route
- Revisit prerequisites: Reliability Manager Log Records Undo Redo and Checkpoints, Distributed Transaction Classification and Two-Phase Commit, and Big Data Storage and Processing Models.
- Read NoSQL Data Models MapReduce Rereduce and Views for Relational vs. NoSQL tradeoffs, the 4 NoSQL families, MapReduce/Rereduce (including the
AVGrereduce trap), CouchDB views, and the NoSQL Banking Design Recipe with idempotency. - Read Replication CAP Theorem ACID vs BASE and Conflict Resolution for Master-Slave vs. Master-Master, Sync vs. Async replication, CAP and Beyond CAP, ACID vs. BASE, CouchDB conflict resolution, HTTP REST API, and an added end-to-end two-node wallet trace.
- Read MongoDB Polyglot Persistence Hadoop and Apache Spark for MongoDB document design tradeoffs, Polyglot Persistence, the Hadoop stack (
HDFS,YARN,MapReduce), and Apache Spark.
Source map
| PDF pages | Material |
|---|---|
| 1–5 | Title (Daniele Apiletti, DBMG), #NoSQL origin (1998 Strozzi, 2009 Oskarsson), NoSQL main features, and Relational vs. Non-Relational comparison |
| 6–13 | Four NoSQL types (Key-Value, Column-Oriented, Graph, Document) and CouchDB architecture (Erlang, REST JSON API, replication, MapReduce) |
| 14–29 | MapReduce principles, pure Map(doc) independence, 8-doc exam dataset examples (1, 2, Rereduce AVG trap, 3a CFU-weighted, 3b distributed count), and CouchDB materialized views |
| 30–34 | Replication: goals, Master-Slave vs. Master-Master, Synchronous (2PC-like / majority tradeoff) vs. Asynchronous (data loss / reconciliation) |
| 35–47 | CAP Theorem (C, A, P; Brewer, Gilbert & Lynch), CA vs. CP vs. AP, Werner Vogels quote, Beyond CAP (2012), and ACID vs. BASE |
| 48–52 | Conflict resolution problem (hotel double-booking), CouchDB deterministic winner (_rev history + ASCII tie-break), and HTTP RESTful API (200, 201, 409) |
| 53–57 | MongoDB intro, why MongoDB, Document Data Design (embedded vs. ObjectId references / denormalization), and Polyglot Persistence |
| 58–67 | Apache Hadoop history and overview, 4 core components (Common, HDFS, YARN, MapReduce), related Apache projects, Apache Spark (in-memory DAG), and why Hadoop |
| 68–76 | Banking design recipe: lost-message failure with two account docs vs. atomic single transaction doc + MapReduce balance calculation |
Worked examples from the source
1. Exam MapReduce & the Rereduce AVG trap (Slides 18–28)
Over 8 university exam documents (Id, Exam, Student, AYear, Mark, CFU):
- Average per exam (Slide 23):
emit(doc.exam, doc.mark)+sum(values)/len(values)→Database: 26.67([29, 26, 25]). - Composite key
[exam, AYear]+ Rereduce trap (Slides 24–25): Grouping by[doc.exam, doc.AYear]gives[Database, 2014-15]: 25.5(2 exams) and[Database, 2015-16]: 29(1 exam). CallingRereduceatgroup level = 1averages[25.5, 29]to27.25instead of26.67because counts are unequal (fixable by returning[sum, count]intermediates). - CFU-weighted student average (Slide 27):
emit(doc.student, [doc.mark, doc.CFU])→S123456: 25.6,S654321: 23.9,s987654: 25.
2. Banking design recipe in CouchDB (Slides 68–75)
Updating two separate account documents for a $100 transfer fails if the credit message is lost in transit (Slides 71–73). Storing one atomic transaction document {id: "transaction001", from: "bank_account_001", to: "bank_account_002", qty: 100} (Slide 74) and computing balances via MapReduce (emit(from, -amount), emit(to, +amount), sum(values)) guarantees the bank always balances (900 and 1100, Slide 75). (See NoSQL Data Models MapReduce Rereduce and Views for why this also preserves idempotency, and Replication CAP Theorem ACID vs BASE and Conflict Resolution for an added two-node wallet walkthrough.)
Revision prompts
- Compare Relational vs. NoSQL databases across data model, schema, scaling, joins, and query interfaces, and give one concrete use case for each of the four NoSQL families.
- Explain why
Map(doc)cannot read outside the current document, and show whyRereducefails on rawAVG(Slide 25) unless[sum, count]is emitted. - Explain why the Banking Design Recipe stores one transfer document instead of two account documents, and explain why it does not break idempotency.
- Contrast Synchronous vs. Asynchronous replication,
Cin ACID vs.Cin CAP, andCPvs.APbehavior during a network partition (plus Brewer’s 2012 “Beyond CAP” refinements). - Trace CouchDB’s deterministic conflict-winner rule (
_revhistory length, then ASCII order) and explain why application logic is still needed. - Compare MongoDB embedded vs. referenced documents, explain Polyglot Persistence, and contrast Hadoop MapReduce vs. Apache Spark.
Source and scope
Daniele Apiletti (Data Base and Data Mining Group, Politecnico di Torino), Beyond relational databases, local path Beyond-relational-databases-18-12.pdf, 76 PDF pages. This note paraphrases and organizes the source material; added study clarifications and the end-to-end two-node wallet walkthrough in the linked concept notes are labeled explicitly.