Lecture: Lecture 21 - Beyond Relational Databases
Related: NoSQL Data Models MapReduce Rereduce and Views · Replication CAP Theorem ACID vs BASE and Conflict Resolution · Big Data Storage and Processing Models · MongoDB BSON Documents Collections and CRUD Operations · MongoDB Compass and Aggregation Pipelines · ElasticSearch Data Model Inverted Index Analyzers and Query DSL

MongoDB and document data design (Slides 53–56)

MongoDB is a leading open-source document-oriented NoSQL database known for native horizontal scalability (out-of-the-box sharding and replica sets), fast inserts, end-to-end JSON/BSON documents, and rich query capabilities (Slides 53–55).

Document data design tradeoff (Slide 56)

Because documents map directly to developer-language objects (supporting nested sub-documents, lists, timestamps, and maps), MongoDB offers two ways to model related data—each with a clear consequence:

  1. Embedded sub-documents (preferred when owned together): Store contact and access nested directly inside the user document. Everything is fetched in a single read with zero cross-document joins.
  2. Document references (ObjectId links): Store user, contact, and access as separate documents pointing to user_id: <ObjectId1>.
    • Limitation: Relations across separate documents are inefficient because MongoDB has no native relational join, pushing designs toward denormalization (duplicating fields inside documents for read speed).
    • Design pitfall: Avoid the temptation to force highly structured, tightly relational data into a schema-free document model when a relational database fits better.

Polyglot persistence (Slide 57)

Modern applications rarely rely on a single database engine for every workload. Polyglot persistence means combining multiple specialized databases inside one application, matching each subsystem to the engine built for its access pattern (Slide 57):

  • ElasticSearch → full-text search
  • MongoDB → flexible non-relational document storage
  • Cassandra → massive write throughput for IoT sensor streams

(And an RDBMS alongside them when strict multi-table ACID transactions and complex joins are required!)

The Apache Hadoop ecosystem (Slides 58–65, 67)

Born out of Google’s GFS (2003), MapReduce (2004), and BigTable (2006) papers, Apache Hadoop (started at Yahoo in 2005; moved to Apache in 2006; famously sorted 1 TB in 209 seconds in 2008 as a historical benchmark) became the de facto open-source Big Data platform built on clusters of low-cost commodity hardware (Slides 58–59, 67).

Four core components (Slides 60–64)

ComponentRole in the Hadoop stack
Hadoop CommonShared utilities and libraries supporting all other Hadoop modules
HDFS (Hadoop Distributed File System)Distributed, fault-tolerant storage layer providing high-throughput access to massive datasets across commodity nodes
YARN (MapReduce V2 / Resource Manager)Cluster resource management and job scheduling layer sitting above HDFS
Hadoop MapReduceYARN-based batch parallel processing engine running Map and Reduce tasks close to HDFS blocks

(Familiarity — Slide 65: Surrounding Apache projects in the stack): Hive (SQL / data warehouse queries), Pig (high-level data-flow scripting), HBase (scalable distributed columnar store), Cassandra (multi-master NoSQL store), Tez (DAG execution engine replacing raw MapReduce under Hive/Pig), Spark (in-memory compute engine), Storm (stream processing), Mahout (scalable ML library), ZooKeeper (distributed coordination), Avro (serialization), Ambari (cluster management UI), and Chukwa (log/data collection).

Why Hadoop (Slide 67)

  • Storage: Distributed, fault-tolerant, heterogeneous huge-data storage.
  • Processing: Parallel, scalable, multi-purpose (batch, streaming, modeling, KPI analytics) in Java, Python, Scala, or R.
  • High availability & scalability: Handles node failures by design and grows by adding low-cost commodity nodes instead of replacing hardware with expensive supercomputers.

Apache Spark: fast DAG and in-memory computing (Slide 66)

Apache Spark is a fast, general-purpose compute engine for large-scale data processing that addresses disk-heavy bottlenecks in classic Hadoop MapReduce (Slide 66):

  • Speed: Executes programs up to 100× faster than Hadoop MapReduce in memory (or 10× faster on disk) thanks to an advanced DAG (Directed Acyclic Graph) execution engine and in-memory computing (avoiding repeated HDFS disk writes between every MapReduce stage).
  • Ease of use: APIs in Java, Scala, Python, and R, with over 80 high-level operators (flatMap, map, reduceByKey, etc.) plus interactive shells.
  • Generality: Seamlessly combines four unified libraries in one application: Spark SQL / DataFrames, Spark Streaming, MLlib (machine learning), and GraphX (graph computation).
  • Runs everywhere: Runs on Hadoop (YARN), Apache Mesos, standalone clusters, or the cloud, reading from HDFS, Cassandra, HBase, and S3.

Common confusions

  • Treating Hadoop MapReduce and Apache Spark as completely unrelated: Both process distributed datasets across clusters (often sharing HDFS and YARN), but Spark replaces multi-stage disk-bound MapReduce jobs with an in-memory DAG engine and adds SQL, streaming, ML, and graph APIs.
  • Thinking NoSQL replaces RDBMSs everywhere: Polyglot persistence (Slide 57) explicitly advocates using the right store per job—and Slide 56 warns against forcing structured relational data into schema-free documents.
  • Confusing HDFS with HBase: HDFS is the distributed file system storing blocks/files; HBase is a columnar database running on top of HDFS for structured random-access storage.

Retrieval questions with answers

1. In MongoDB, what is the main tradeoff between embedding a contact sub-document inside a user document versus storing a separate contact document with a user_id reference?
Embedding keeps related data together for fast single-document reads and atomic single-document updates. Splitting into separate documents with ObjectId references avoids duplicating data, but MongoDB has no native join—making cross-document queries inefficient and pushing designs toward denormalization.

2. What is Polyglot Persistence, and why would an IoT + search + user-profile application use Cassandra, ElasticSearch, and MongoDB together?
Polyglot persistence is using multiple specialized databases within one application so each workload runs on the store suited to it: Cassandra absorbs massive high-throughput IoT sensor writes, ElasticSearch powers full-text search, and MongoDB stores flexible document profiles.

3. Why can Apache Spark run iterative analytics up to 100× faster than classic Hadoop MapReduce in memory?
Spark uses an in-memory DAG execution engine that chains operators (flatMap, map, reduceByKey, etc.) in RAM instead of writing intermediate outputs back to HDFS disk between every Map and Reduce step.