SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
Part 7: Advanced Data Topics
  • Batch vs Stream Processing
  • Change Data Capture (CDC)
  • MapReduce
  • ETL Pipelines
  • Data Lakes
  • Data Warehousing
  • Data Lakehouse
  • Lambda Architecture
  • Kappa Architecture
  • Streaming Engines
  • Spark and Hadoop
  • Workflow Orchestration (Airflow)
  • Parquet and Columnar File Formats
  • Data Governance and Feature Stores
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/Advanced Data Topics/Big Data Processing

Lambda Architecture

Run a batch path and a speed path side by side, then merge their answers.

⏱ 13 min readAdvancedInterview FavouriteHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

Lambda architecture answers one question with two paths at the same time: a slow, exact batch path and a fast, rough speed path. A third part, the serving layer, merges both into one answer.

  • The batch layer re-reads all the data now and then, and computes exact results. It is accurate but always a little old.
  • The speed layer handles only the newest events and gives quick, approximate results.
  • The serving layer answers a query by combining the batch result (old data) with the speed result (recent data).

02Concept: What is it?

Imagine you want to show "total sales today" on a dashboard. A big batch job can count every sale perfectly, but it runs every few hours, so its answer is old. A stream job can count each sale the moment it happens, but it is harder to make exact. Lambda architecture says: use both.

All new data goes into an append-only master dataset (raw events that are never changed) and, at the same time, to the speed layer. The batch layer periodically recomputes batch views from the whole master dataset. The speed layer keeps small real-time views for the data the last batch run did not see yet.

The three layers
📥New eventsclicks, orders, logs
→↓all data
Batch layer
🗃️Master datasetappend only
⚙️Batch jobexact, slow
→↓recent data
Speed layer
⚡Stream jobfast, rough
→↓
Serving layer
🔀Mergebatch view + real-time view
→↓
📊Queryone answer
A query reads both views and merges them. When a new batch view is ready, the speed layer can forget the data that the batch run now covers.
🧠Why two paths?
The idea comes from a time when stream tools were not trusted for exact answers, but batch tools were slow. The batch layer is the source of truth. The speed layer only covers the gap until the next batch run catches up.

03Real-Life Example

🍵The Newspaper and the Live Blog
A news site prints a careful, checked newspaper once a day. It is exact, but it is already old when you read it. During the day the same site runs a live blog with quick updates that may contain small mistakes. To know what is going on right now, you read the newspaper for everything up to this morning, and the live blog for what happened since. Tomorrow's newspaper then replaces the live blog notes with checked facts.
Newspaper and live blog
Lambda architecture
The daily newspaper
Batch layer: exact, but old
The live blog
Speed layer: quick, maybe rough
Reading both together
Serving layer: merge two views
The archive of every story ever filed
Master dataset (append only raw data)
Tomorrow's paper replaces the blog notes
A new batch view replaces the speed layer's data
A mistake fixed in the next print
Errors in the speed layer are corrected by the batch layer
One query, two sources
🗓️Midnight to 12:00batch view (exact)
→↓+
🕛12:00 to nowspeed view (recent)
→↓=
✅Total todaymerged answer

04Why Do We Need It?

🎯Exact results
The batch layer reads all the data with full logic, so the final numbers are correct.
⚡Fresh results
The speed layer closes the gap, so users do not wait hours for new data to show up.
🛠️Fix mistakes by recomputing
A bug in the logic is fixed by running the batch job again over the raw data.
🧾Raw data is kept
The master dataset is never changed, so you can always go back to the original events.
🛡️Safe against human error
Bad code can corrupt a view, but not the raw data. The views can be rebuilt.
📈Scales out
Both layers run on many machines, so huge amounts of data are fine.
Batch gives you the right answer late. Streaming gives you a quick answer now. Lambda gives you both, and lets the right answer replace the quick one.

05Where Do We Use It?

📊Real-time dashboards
Today's numbers must be fresh, but last month's numbers must be exact.
🛒Recommendations
A model trained on all history in batch, plus quick updates from recent clicks.
🕵️Fraud and abuse checks
Fast rules catch bad activity now, and a deeper batch analysis confirms it later.
📣Ad and event counting
Quick counts for campaigns, with exact billing numbers computed later in batch.
📡Metrics and logs
Recent metrics from a stream, long-term reports from a batch over stored logs.
🧮Counting big sets
Approximate counts now (for example with sketches), exact counts after the batch run.
📝Note
Many teams today use a single stream engine instead, because modern engines can give exact results. See Kappa Architecture and Streaming Engines.

06Types / Parts / How It Works

The three layers in detail

LayerJobData it seesSpeed and accuracy
Batch layerStore the raw master dataset and recompute batch views from all of itEverything up to the last batch runSlow (minutes to hours) and exact
Speed layerProcess new events right away and keep small real-time viewsOnly data the batch views do not cover yetFast (seconds) and often approximate
Serving layerStore the batch views for quick reads and merge them with real-time viewsBoth viewsFast reads, as exact as its inputs

How the hand-over works

The batch view grows, the speed view shrinks
Before the batch run finishes
🗃️Batch viewhours 0 to 6
⚡Speed viewhours 6 to now
→↓new batch view lands
After the batch run finishes
🗃️Batch viewhours 0 to 12
⚡Speed viewhours 12 to now
Exact Quick and rough
When a batch run finishes, the part of the day it covers moves from the speed view to the batch view.

Common tools (examples, not a rule)

LayerTypical choices
Master datasetObject storage or a distributed file system (S3, HDFS)
Batch layerMapReduce, Apache Spark
Speed layerKafka Streams, Apache Flink, Spark Streaming
Serving layerA fast database such as Cassandra, HBase, Druid or Elasticsearch
VisualizerBatch view, speed view and the merged answerMove the clock through the day. Turn the speed layer off and see how stale the batch-only answer is. Make the batch job less frequent or slower, and try the rough speed layer.
Batch job starts every
Speed layer
Events per hour today. Each bar is one hour. The colour says who knows about it.
0
3
6
▲
9
12
▲
15
18
21
Batch view (exact)Speed layer (recent)Missing (nobody knows yet)▲A batch run starts here
Batch view
820
hours 0 to 12
Speed view
330
hours 12 to 14
Answer served
1150
batch + speed
True total
1150
what really happened
Error
0
answer minus truth
The batch view holds hours 0 to 12 exactly (820). The speed layer fills the last 2 hours (330). Merged: 1150, which is the true total.

A simplified day with fixed event counts. A batch run that starts at hour H reads all data before H and is ready 2 h later. Real jobs and delays vary.

07How to Use It / Steps / Implementation

Building the idea step by step

  1. 1
    Keep an append-only master dataset
    Write every raw event to cheap, durable storage and never change it. This is the source of truth.
  2. 2
    Write the batch job
    A job that reads the whole master dataset (or a big part of it) and produces batch views. Schedule it, for example every few hours.
  3. 3
    Write the speed job
    A stream job that reads the same events and updates small real-time views for data the batch has not covered.
  4. 4
    Define the cut-off
    Decide how the serving layer knows where the batch view ends and the speed view begins, for example a timestamp or an offset.
  5. 5
    Merge at query time
    A query reads the batch view and the real-time view and combines them (add counts, take the latest value, and so on).
  6. 6
    Expire the speed data
    After a new batch view lands, delete the real-time data it now covers, so the speed layer stays small.
  7. 7
    Test that both paths agree
    Run the same sample data through both paths and compare. The two code paths must produce the same numbers.
⚠️The big cost: two code bases
The same business logic must be written twice, once for batch and once for streaming, often in different tools. They can drift apart, and a difference between them becomes a hard-to-find bug. Some teams reduce this by sharing code or using one framework for both.
💡Make merging easy
Choose results that merge simply: counts and sums add up, and "latest value wins" works with timestamps. Results like exact distinct counts or medians are hard to merge from two parts.

08Key Terms to Know

TermMeaning in easy words
Batch layerRecomputes exact views from all the data, on a schedule
Speed layerProcesses recent events fast to fill the gap before the next batch run
Serving layerStores the views and answers queries by merging them
Master datasetThe append-only raw data that is the source of truth
Batch viewA precomputed result built by the batch layer
Real-time viewA small, quick result built by the speed layer for recent data
Immutable dataData that is only added, never changed or deleted in place
RecomputationRunning the batch job again over raw data, for example to fix a bug
Eventual accuracyQuick answers may be off, but become exact when the batch result arrives
LatencyHow old the data in an answer is, or how long it takes to get it
Cut-off pointThe time or offset where the batch view ends and the speed view begins

09Pros, Cons and Trade-offs

✅ Pros
  • Exact results from batch and fresh results from streaming
  • Bugs are fixed by recomputing from raw data
  • The raw data is never lost or changed
  • Fits very large data sets
  • Each layer can be tuned on its own
⚠️ Cons
  • Two code paths for the same logic
  • More systems to run and to watch
  • Hard to keep both paths equal
  • Merging results is tricky for some calculations
  • Often more expensive than a single stream path

Trade-offs

ChoiceWe gainWe lose
Run the batch job more oftenA smaller gap for the speed layer to coverMore compute cost
Run the batch job less oftenLower costThe speed layer covers more, so more answers are approximate
Rough speed layerSimple and quick to buildVisible differences when the batch result replaces it
Exact speed layerSmooth results with fewer correctionsHarder logic, and it is nearly a full second code base
LambdaBoth exact and fresh resultsTwo systems and two code bases to keep in line
A single stream path (Kappa)One code base and one pathNeeds a log that keeps history and a stream engine you can trust

10Common Problems and Mistakes

Common mistakes

  1. The two paths disagree. A change is made in the batch code but not the stream code, and numbers jump when the batch result replaces the speed result.
  2. A wrong cut-off. If the batch view and speed view overlap, events are counted twice. If there is a gap, events are missed.
  3. Hard-to-merge results. Distinct counts or percentiles from two parts cannot just be added.
  4. Forgetting to expire speed data. The real-time views keep growing and slow everything down.
  5. Treating the speed layer as exact. Users see a number and trust it, then see it change later.
  6. Changing the master dataset. If raw data is edited, recomputation is no longer a safe way to fix mistakes.

Things to watch

🔀Code drift
Share logic between layers where you can, and compare their outputs on sample data.
⏱️Batch delay
A late or failed batch run makes the speed layer cover more and more. Alert on it.
🔁Duplicate counting
Check the cut-off. Use event ids or offsets to avoid counting the same event twice.
💸Cost
Running both layers is not cheap. Make sure the freshness is worth it.

11Real-World Examples

🐦Social media analytics
Lambda architecture was described in the early 2010s from work on large social data systems, with Hadoop for batch and Storm for speed.
📊Analytics dashboards
A dashboard shows exact history from batch results plus the last few minutes from a stream.
🎬Recommendations
Models trained on a long history, and updated with recent viewing activity.
💳Payments monitoring
Fast rules block risky payments now, and deeper batch checks review them later.
📡Telemetry
Recent device metrics come from a stream, while monthly reports come from batch jobs.
🔄Moving away
Many teams have replaced Lambda with a single stream path, or with a lakehouse that handles both.
📝Note
Real systems differ a lot from the textbook picture. Names and tools change, so treat the three layers as an idea, not a fixed recipe.
Mini case: page views per product
🌐Websitepage view events
→↓log
🪵Event lograw, append only
→↓hourly + nightly
⚙️Batch jobexact counts
→↓
🔀Servingbatch + last hour
🪵Event log
→↓live
⚡Stream joblast hour counts
→↓
🔀Serving
The product page shows batch views for history and the speed layer for the last hour.

12Related Topics to Read Next

Kappa Architecture→

The simpler alternative with only a stream path.

Batch vs Stream Processing→

The two ways of processing that Lambda combines.

Streaming Engines→

The tools that power the speed layer.

MapReduce→

The classic batch engine used for the batch layer.

Data Lakes→

A common home for the raw master dataset.

Data Lakehouse→

A newer way to serve batch and fresh data from one place.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • ✔What: a batch path (exact, late) plus a speed path (fresh, rough), merged by a serving layer.
  • ✔Master dataset: raw, append-only events. Recompute views from it to fix bugs.
  • ✔Batch layer: reruns over all data on a schedule. The source of truth.
  • ✔Speed layer: covers only data newer than the last batch run.
  • ✔Serving layer: answer = batch view + real-time view, split at a cut-off.
  • ✔Cost: two code bases and two systems that must agree.
  • ✔Easy to merge: sums, counts, latest value. Hard: distinct counts, percentiles.
  • ✔One line: “Give the quick answer now, and replace it with the exact one when it is ready.”
← Previous
Data Lakehouse
Next →
Kappa Architecture