Guide to AI and Data Courses · Lecture Notes

Chapter 27: Big Data Systems

A video site records billions of viewing events every day; they do not fit on one computer's disk, and even if they did, one computer could not read and summarize them overnight. The answer is to split both the work and the data across hundreds of cheap machines, but machines will fail and the network between them will break. Big data systems teaches how to build systems that give correct results despite those failures. These notes introduce the course and work through its three core ideas with small examples you can compute by hand.

Learning outcomes

Partitioning and replication
Compute how many blocks a file splits into and how much storage a replication factor needs; distribute keys over partitions.
MapReduce
Run a word count by hand through the map, shuffle and reduce steps; show how a combiner cuts network traffic.
Scaling limits
Compute how a serial fraction limits the speedup from adding machines.
Consistency
Explain why the quorum rule (W + R > N) prevents stale reads; describe the CAP trade-off with an example.
Place of the course
State what separates big data systems from data mining, data science, GPU computing and MLOps.

27.1What question does the course ask?

How is data that does not fit on one machine stored and processed?

Big data systems study how to store and process data that does not fit on one machine, or arrives faster than one machine can keep up, by spreading it over many machines. The course deals with the trade-offs between partitioning, replication, fault tolerance and consistency.

For a program on a single machine, memory, disk and processor are ready-made resources; if the machine fails, the program stops, and that is acceptable. On hundreds of machines the situation changes: even if each machine has a small chance of failing in a year, out of a thousand machines one is expected to fail every day. So in big data systems failure is not an exception but part of normal operation, and the system is designed for it from the start.

We can split the question of the course into two parts. The first is where does the data live? The data is cut into blocks and partitions, and every piece is replicated on several machines. The second is where does the computation run? Because pulling data across the network is expensive, the computation is moved to the machine where the data sits. Every decision between the two parts is a trade-off: more replicas are safer but cost more, stricter consistency is slower, and more machines mean more coordination.

27.1.1Scaling up and scaling out

A growing load can be met in two ways. Scaling up means making the machine stronger (more memory, a faster disk); it is simple but has a ceiling, and the price does not grow linearly. Scaling out means adding machines; it has no ceiling, but it pushes the work of splitting the data, putting the pieces back together and handling failures onto software. The course studies the software of the second route.

27.1.2At a glance

CriterionBig Data Systems
Approachdistributed storage and computation
Inputhuge, fast-flowing data
Outputprocessed data, query results
Success measurescalability, latency, cost
Math loadlow
Data needsvery large

27.2A short history

YearMilestone
2001Laney describes the data management challenge as volume, velocity and variety (the 3 Vs).
2003The Google File System paper: a fault-tolerant distributed file system built from cheap machines.
2004Dean and Ghemawat publish MapReduce: a simple programming model across thousands of machines.
2006Hadoop, an open-source counterpart of GFS and MapReduce, becomes its own Apache project.
2010Spark is released as open source: keeping intermediate results in memory speeds up iterative jobs.
2011LinkedIn open-sources Kafka: a distributed log system for event streams.

27.3Core concepts

The rest of the course is built on the concepts below. The following sections work through the most important ones with examples.

ConceptMeaning
Volume, velocity, varietyThe size of data, how fast it arrives, how many formats.
Distributed file systemSplitting files into blocks and replicating them across machines.
MapReduceMap, shuffle, reduce: moving computation to the data.
In-memory processingSpark: chaining steps without writing to disk.
Stream processingProcessing an unbounded event stream as it arrives.
NoSQLKey-value, document, wide-column and graph stores.
CAP theoremUnder a network partition, choosing consistency or availability.
Partitioning, replicationSpreading load and not losing data when a machine fails.

27.4Partitioning and replication

If the data does not fit on one machine we must cut it into pieces. This comes in two forms. In distributed file systems (GFS, HDFS) a large file is cut into fixed-size blocks. In data stores, records are spread over partitions by a key. In both cases every piece is replicated on several machines so that it is not lost when a machine fails.

Definition 27.1 (Replication factor)

The number of machines that store each block. With factor r, a block stays readable even if up to r − 1 machines fail at the same time; the raw storage requirement is r times the data size.

Example 27.1: Cutting a file into blocks

A 1000 MB file is stored with 128 MB blocks and replication factor r = 3.

  1. Number of blocks. ⌈1000 / 128⌉ = 8: seven blocks are full (7 × 128 = 896 MB) and the eighth holds 1000 − 896 = 104 MB.
  2. Number of replicas. 8 × 3 = 24 block replicas, spread over the machines of the cluster.
  3. Raw storage. 3 × 1000 = 3000 MB: three times the space is paid for each byte of data.
  4. Durability. The three replicas of a block sit on different machines (preferably different racks); even if two machines fail at once, the block can be read.

That the eighth block holds only 104 MB is not a problem: no space is reserved for the unused part of a block.

The simplest partitioning rule in a data store is to take the hash of the key modulo the number of partitions: partition = key mod P. The rule is easy and usually spreads keys evenly, but it runs into trouble when the number of partitions changes.

Example 27.2: Adding a node moves keys

The user ids are 17, 22, 35, 40, 51, 64, 78, 90; at first there are P = 4 partitions.

  1. With 4 partitions. The remainders are 1, 2, 3, 0, 3, 0, 2, 2. Partition 0 holds 2 records, partition 1 holds 1, partition 2 holds 3, partition 3 holds 2 (a slightly uneven spread).
  2. We add a fifth partition. The new remainders are 2, 2, 0, 0, 1, 4, 3, 0.
  3. Comparison. Only 22 and 40 stay where they were; 17, 35, 51, 64, 78, 90 move, that is 6 of the 8 records go to a different partition.

Adding a single node moved most of the records. That is why real systems usually use consistent hashing or a fixed, large number of small partitions, so that adding a node moves only a small share of the data.

Common mistake

Assuming that "we spread the keys evenly, so the load is even too". If one key is very popular (the id of a celebrity user, say), all requests for it land on one partition and that machine becomes the bottleneck; this is called a hot spot. The spread must be judged by the number of requests, not the number of records.

27.5MapReduce and the limit of scaling

MapReduce offers a simple two-function model for processing data spread over thousands of machines. The programmer writes only the map and reduce functions; splitting the data, distributing the tasks and rerunning a failed task are the framework's job. Map produces (key, value) pairs from each input record. The shuffle step in between gathers the pairs with the same key at the same reducer. Reduce collapses the values of each key into one result.

Example 27.3: Word count

Three input lines go to three mappers: "to be or not to be", "to do or not to do", "be or do".

  1. Map. For every word, (word, 1) is produced. The lines give 6, 6 and 3 pairs: 15 pairs in all.
  2. Combiner (optional). Each mapper adds up its own output locally before sending it over the network. The first mapper sends (to,2) (be,2) (or,1) (not,1) = 4 pairs, the second (to,2) (do,2) (or,1) (not,1) = 4 pairs, the third (be,1) (or,1) (do,1) = 3 pairs. The number of pairs on the network goes 15 → 11.
  3. Shuffle. The same words go to the same reducer; for example reducer 1 gets to and be, reducer 2 gets or, not and do.
  4. Reduce. The values are added: to = 2 + 2 = 4, be = 2 + 1 = 3, or = 1 + 1 + 1 = 3, not = 1 + 1 = 2, do = 2 + 1 = 3. The total is 4 + 3 + 3 + 2 + 3 = 15, the number of words in the input.

That the counts add up to the number of input words is a good sanity check.

In MapReduce the intermediate result of every step is written to disk; this makes fault tolerance easy but iterative jobs slow. Spark keeps the same map-and-reduce idea, holds intermediate results in memory and recomputes a lost piece from its source. As a result iterative jobs are often markedly faster.

27.5.1How much do more machines help?

If a fraction f of a job can be parallelized and the remaining 1 − f stays serial (reading the input, shuffling, waiting for the slowest task), the speedup on n machines is 1 / ((1 − f) + f / n); this relation is called Amdahl's law.

Example 27.4: The ceiling on speedup

A job takes 100 minutes on one machine and 90% of it can be parallelized (f = 0.9).

  • n = 2: time 10 + 90/2 = 55 minutes, speedup 1.82 (not 2).
  • n = 10: time 10 + 9 = 19 minutes, speedup 5.26.
  • n = 100: time 10 + 0.9 = 10.9 minutes, speedup 9.17.

No matter how many machines are added, the speedup cannot pass 1 / 0.1 = 10; the serial 10 minutes set the ceiling.

Common mistake

Saying "if I double the machines the time halves". In the example above two machines cut the time from 100 to 55 minutes, not to 50. And in real clusters the shuffle and the slowest task (the machine that finishes last) make the serial fraction even larger.

27.6Replicas and consistency: quorums

If a value is replicated on three machines and a write ends before reaching all three, the replicas differ for a while. Which replica will a reader see? The answer is the system's consistency model. The CAP theorem sets a limit: when the network splits in two, the system must choose either consistency (every read sees the latest write) or availability (every request gets an answer); it cannot give both.

Definition 27.2 (Quorum)

Let there be N replicas. A write counts as successful when at least W replicas acknowledge it, and a read counts as complete when R replicas have answered. If W + R > N, every read set overlaps every write set in at least one replica, so a read sees the latest written value.

Example 27.5: Choosing W and R for N = 3

There are three replicas A, B and C, all starting with x = 1 (version 1). The value x = 5 (version 2) is written, but C is unreachable at that moment.

  1. W = 2. The write reaches A and B, two acknowledgments arrive, the write succeeds. State: A = version 2, B = version 2, C = version 1 (stale).
  2. R = 2. A reader asks two replicas; there are three possible pairs: (A, B) → 2, 2; (A, C) → 2, 1; (B, C) → 2, 1. All three pairs include version 2, and the highest version is taken. Because W + R = 4 > 3, a stale read is impossible.
  3. If W = 1, R = 2. Let the write reach only A. W + R = 3, the condition fails. If the reader asks the pair (B, C), both return version 1: one of the three pairs (1/3) reads stale data.
  4. The price. With W = 2 writes continue even if one machine is down; if two are down, the write is refused (availability is given up for consistency).

The choice depends on the application. A system that wants fast reads can take R = 1, W = N; a system that wants writes to always succeed can take W = 1 and accept stale reads. In a shopping cart a brief inconsistency is usually harmless; in a bank balance it is not. In systems such as Cassandra these numbers can be set per query.

Common mistake

Reading the quorum rule as "stale reads never happen". W + R > N only says that after a successful write the value read is current. If a write stops halfway without collecting W acknowledgments, a partial write remains among the replicas and a read may see it; concurrent writes and clock differences also need extra mechanisms (version numbers, a consensus protocol).

27.7How is the course taught?

27.7.1A typical workflow

CollectKafka,event streamsStoreHDFS, objectstorageProcessSpark, FlinkQuerydistributed SQLServedashboard,model, report
Figure 27.1: A typical workflow in Big Data Systems

A video site collects billions of view events a day with Kafka, partitions them by date in object storage, computes nightly per-user viewing summaries with Spark and writes the results to tables that analysts can query with SQL.

27.7.2Topics over the semester

  1. The scaling problem: scaling up and scaling out
  2. Distributed file systems: GFS and HDFS
  3. The MapReduce programming model
  4. Spark: RDDs, DataFrames and Spark SQL
  5. Data models: relational, document, wide-column
  6. Storage engines: LSM trees, B-trees, columnar files
  7. Partitioning and load balancing
  8. Replication and consistency models
  9. CAP, distributed transactions, consensus: Paxos, Raft
  10. Event streams: Kafka and publish-subscribe
  11. Stream processing: windows, event time, Flink
  12. Data lakes, warehouses and workflow orchestration

This is a typical semester; the order and coverage vary between universities.

27.7.3Skills you will gain

  • writing distributed jobs in PySpark
  • writing Kafka producers and consumers
  • columnar storage with Parquet
  • data modeling in Cassandra or MongoDB
  • diagnosing a slow Spark job

27.7.4Try it yourself

The Try It: Word Count over Search Logs slide in the presentation lets you solve a typical problem of this course yourself: A site's search logs do not fit on one machine; worker machines must count together how often each word was searched. Go step by step: Map → Shuffle → Reduce. Change the number of workers and the combiner, and watch the time.

27.8Are you ready for this course?

27.8.1Prerequisites

CourseWhy it is needed
Operating Systemsprocesses, threads, file systems and the cost of I/O
Object Oriented Programming with JavaHadoop, Spark and Kafka run on the JVM
Databases and SQLthe relational model, indexes, transactions

27.8.2Ask yourself

For every "no", the right-hand column shows where to start. If you can say "yes" to most of them, you are ready.

QuestionIf not, start with
Can you explain the difference between a process and a thread?An operating systems course: processes and concurrency
Can you write an SQL query with JOIN and GROUP BY?SQL and the relational model from a database course
Can you write a program with classes and collections in Java or Python?An object-oriented programming course in Java
Can you move files and run a script from the Linux command line?A short introduction to the Linux command line

27.9Neighboring courses

Data MiningGPU & Parallel ComputingMLOpsBig Data
subfieldbuilds onshares methodsapplication
Figure 27.2: Neighbors of Big Data Systems and the kind of relation

27.9.1Where it overlaps

RelationCourseShared ground
shares methodsShares methods with Data Miningscalable mining
shares methodsShares methods with Machine Learning Systems and MLOpsdata pipelines
shares methodsShares methods with GPU Programming and Parallel Computingparallel and distributed computing

27.9.2Where it differs

  • Data Mining: Data mining asks which patterns to look for; big data systems ask how that search runs on many machines.
  • Data Science: Data science draws questions and conclusions from data; big data systems make that possible when data outgrows one machine.
  • GPU Programming and Parallel Computing: A GPU speeds up computation with thousands of cores in one machine; big data systems spread data over machines, where network and disk are the bottleneck.
  • Machine Learning Systems and MLOps: MLOps manages the model life cycle; big data systems build the data pipelines and storage that feed it.

27.10In practice

27.10.1Applications

AreaWhat is done
Log analysisErrors and traffic from server logs.
ClickstreamTracking user behavior in real time.
Recommendation backendsThe histories of millions of users.
Internet of ThingsContinuous measurements from sensors.
Financial transactionsReal-time fraud alerts.
Search indexCrawling and indexing web pages.

27.10.2Risks and ethics

RiskExplanation
Silent data lossMisconfigured replication or Kafka records deleted when retention expires can destroy data without anyone noticing.
Keep-everything habitsCollecting personal data because storage is cheap widens the breach surface; GDPR and similar laws require purpose limits.
Honoring the right to erasureA person's data spreads into backups, replicas and derived tables; fully honoring a deletion request is hard.
Cloud bills and energyA cluster left running or an unnecessary full table scan means a large bill and wasted energy.

These issues are discussed in depth in Explainable AI and AI Ethics.

27.10.3Tools and datasets

Tools: Apache Hadoop, Apache Spark, Apache Kafka, Apache Flink, Apache Cassandra, MongoDB, Apache Hive.

Well-known datasets: Common Crawl, Wikipedia dumps, NYC TLC taxi records, GDELT, Google Books Ngram.

27.11Common misconceptions

Common mistake

Big data just means a lot of data.

Velocity and variety matter as much as size; the real question is whether the data fits on one machine.

Common mistake

A distributed system is always faster.

On small data a single machine (pandas, SQL) is often faster; distribution has overhead.

Common mistake

Doubling the machines halves the time.

Shuffles, network traffic and the slowest task limit the speedup.

Common mistake

NoSQL has replaced SQL.

NoSQL offers different consistency trade-offs; SQL engines such as Spark SQL and Hive are widespread.

27.12Summary

  • Big data systems cut data that does not fit on one machine into blocks and partitions and store and process it on many machines; failure is treated as normal in this setting.
  • Every block is replicated on r machines: storage cost grows r times, and up to r − 1 machine losses are survived. With "key mod P" partitioning, adding a node moves many keys; consistent hashing reduces this.
  • MapReduce works through map, shuffle and reduce; a combiner reduces network traffic, and Spark speeds up iterative jobs by keeping intermediate results in memory.
  • A serial fraction limits the speedup (Amdahl): a job with 10% serial work can be sped up at most 10 times.
  • The condition W + R > N prevents stale reads; the CAP theorem forces a choice between consistency and availability when the network splits.

27.13Test Yourself

  1. A 500 MB file is stored with 64 MB blocks and replication factor 3. How many blocks, how many block replicas and how much raw storage are needed?

    Answer

    Number of blocks ⌈500 / 64⌉ = 8 (seven full blocks make 448 MB, the eighth holds 52 MB). Replicas 8 × 3 = 24. Raw storage 3 × 500 = 1500 MB.

  2. The keys are 5, 8, 11, 14, 17, 20 and the partitioning is "key mod 3". How many records does each partition get? What is wrong with this?

    Answer

    All remainders are 2: partition 2 gets 6 records, partitions 0 and 1 get none. A common pattern in the keys (here they rise by 3) makes the partitioning completely uneven; one machine takes the whole load. The number of partitions or the hash function must be chosen so that such patterns are broken up.

  3. Run a word count on the two input lines "a b a" and "b a c". How many pairs go over the network without and with a combiner? What is the result?

    Answer

    Without a combiner 3 + 3 = 6 pairs go. With a combiner the first mapper sends (a,2) (b,1) = 2 pairs, the second (b,1) (a,1) (c,1) = 3 pairs; total 5. Result: a = 3, b = 2, c = 1 (total 6, the number of words in the input).

  4. 80% of a job can be parallelized. What is the speedup on 4 machines? What is the most it can reach as the number of machines goes to infinity?

    Answer

    The speedup is 1 / (0.2 + 0.8/4) = 1 / 0.4 = 2.5. With infinitely many machines the parallel part shrinks to zero: the limit is 1 / 0.2 = 5 times.

  5. In a system with N = 5 replicas, which of (W, R) = (3, 3) and (2, 3) prevents stale reads? Why?

    Answer

    (3, 3): 3 + 3 = 6 > 5, the read and write sets overlap in at least one replica, so there are no stale reads. (2, 3): 2 + 3 = 5, the condition fails; the write may go to {A, B} and the read to {C, D, E}, and no up-to-date replica is seen.

  6. Events with event times (in seconds) 1, 2, 6, 7, 8, 11 are grouped into 5-second tumbling windows: [0,5), [5,10), [10,15). How many events are in each window? If a record with event time 6 is delayed by 3 seconds in processing and arrives at second 9, does the result change?

    Answer

    Counts: [0,5) → 2 (1 and 2), [5,10) → 3 (6, 7, 8), [10,15) → 1 (11). With windows by event time the late record still falls in [5,10); only that window's result looks incomplete until the record arrives. With windows by processing time the record arrived at second 9 and may be written to a different window. This is why stream systems use mechanisms such as watermarks to deal with event time and late data.

27.14After this course

Big data systems are the infrastructure of the data courses. From here you can move to MLOps, which takes models to production, to GPU and parallel computing, which speeds up computation, or to the data mining and data science that run on top of this infrastructure.

Related careers: Data engineer, Big data architect, Data platform engineer, Cloud engineer.

27.15Further reading

  • M. Kleppmann, Designing Data-Intensive Applications (2017): covers partitioning, replication, consistency and stream processing together with their principles; the best single source for the course.
  • T. White, Hadoop: The Definitive Guide (4th ed., 2015): treats HDFS and MapReduce in detail, with example programs.
  • B. Chambers and M. Zaharia, Spark: The Definitive Guide (2018): shows DataFrames, Spark SQL and stream processing in hands-on form.

27.16References

  1. Kleppmann, Designing Data-Intensive Applications, O'Reilly, 2017.
  2. White, Hadoop: The Definitive Guide, 4th ed., O'Reilly, 2015.
  3. Chambers and Zaharia, Spark: The Definitive Guide, O'Reilly, 2018.
  4. Dean and Ghemawat, "MapReduce: Simplified Data Processing on Large Clusters", OSDI, 2004.

Related pages: Slides · Slides (PDF) · Course page