📖 Scenario: You work for a retail company that collects sales data from stores. The data comes in two ways: large files uploaded daily (batch data) and live sales events streaming in real-time (streaming data). You want to combine both to get a complete view of sales.
🎯 Goal: Build a simple Lambda architecture example that processes batch sales data and streaming sales data separately, then combines their results to get total sales per product.
📋 What You'll Learn
Create a batch dataset of sales with product names and quantities
Create a streaming dataset of sales events with product names and quantities
Write batch processing logic to sum quantities per product
Write streaming processing logic to sum quantities per product
Combine batch and streaming results to get total sales per product
Print the combined total sales
💡 Why This Matters
🌍 Real World
Retail companies often collect sales data in batches (daily reports) and streams (live transactions). Combining both helps get up-to-date sales insights.
💼 Career
Data engineers and data scientists use Lambda architecture to handle large-scale data processing combining batch and real-time data for analytics and reporting.
Progress0 / 4 steps
1
Create batch sales data
Create a dictionary called batch_sales with these exact entries: 'apple': 100, 'banana': 150, 'orange': 120 representing quantities sold in batch data.
Hadoop
Hint
Use curly braces to create a dictionary with keys as product names and values as quantities.
2
Create streaming sales data
Create a list called streaming_sales with these exact tuples representing live sales events: ('apple', 20), ('banana', 30), ('orange', 25).
Hadoop
Hint
Use square brackets to create a list of tuples with product names and quantities.
3
Process batch and streaming data
Create a dictionary called streaming_totals that sums quantities per product from streaming_sales using a for loop with variables product and qty. Then create a dictionary called total_sales that adds batch and streaming quantities per product.
Hadoop
Hint
Use a for loop to sum streaming sales. Use dict.get(key, 0) to handle missing keys.
4
Print total sales per product
Write a for loop with variables product and qty to iterate over total_sales.items() and print the product and total quantity in the format: Product: apple, Total Sold: 120.
Hadoop
Hint
Use a for loop over total_sales.items() and an f-string to format the print output.
Practice
(1/5)
1. What is the main purpose of the batch layer in Lambda architecture?
easy
A. To process all data for accurate and complete results
B. To handle real-time data streams quickly
C. To store raw data without processing
D. To visualize data for end users
Solution
Step 1: Understand batch layer role
The batch layer processes all accumulated data to provide accurate and comprehensive views.
Step 2: Compare with other layers
The speed layer handles real-time data, not the batch layer.
Final Answer:
To process all data for accurate and complete results -> Option A
Quick Check:
Batch layer = accurate full data processing [OK]
Hint: Batch layer = full data, accurate results [OK]
Common Mistakes:
Confusing batch layer with speed layer
Thinking batch layer processes data in real-time
Assuming batch layer only stores raw data
2. Which of the following is the correct component of Lambda architecture for handling real-time data?
easy
A. Batch layer
B. Speed layer
C. Serving layer
D. Storage layer
Solution
Step 1: Identify real-time processing component
The speed layer is designed to process data streams in real-time for quick insights.
Step 2: Eliminate other options
Batch layer processes full data, serving layer provides query results, storage layer is not a standard Lambda component.
Final Answer:
Speed layer -> Option B
Quick Check:
Speed layer = real-time data processing [OK]
Hint: Speed layer = real-time data [OK]
Common Mistakes:
Choosing batch layer for real-time data
Confusing serving layer with speed layer
Thinking storage layer is a Lambda component
3. Consider this simplified pseudocode for Lambda architecture layers:
If batch_layer returns {'count': 100} and speed_layer returns {'count': 5}, what will final_result be after merging by summing counts?
medium
A. {'count': 105}
B. {'count': 100}
C. {'count': 5}
D. Error: Cannot merge dictionaries
Solution
Step 1: Understand merging logic
Merging sums the 'count' values from batch and speed results: 100 + 5 = 105.
Step 2: Confirm final result
The final dictionary after merging is {'count': 105}.
Final Answer:
{'count': 105} -> Option A
Quick Check:
100 + 5 = 105 [OK]
Hint: Sum batch and speed counts for final result [OK]
Common Mistakes:
Ignoring speed layer result
Assuming merge replaces batch with speed
Thinking merge causes error
4. This code snippet tries to implement the speed layer but has an error:
def speed_layer(stream):
for record in stream:
process(record)
return processed_data
stream = get_stream()
result = speed_layer(stream)
print(result)
What is the likely error here?
medium
A. process() function is missing
B. stream is not iterable
C. processed_data is not defined before return
D. print() syntax error
Solution
Step 1: Check variable initialization
The variable processed_data is returned but never defined or updated inside the function.
Step 2: Verify other parts
Assuming stream is iterable and process() exists, the main error is missing processed_data definition.
Final Answer:
processed_data is not defined before return -> Option C
Quick Check:
Undefined variable causes error [OK]
Hint: Check all variables are defined before return [OK]
Common Mistakes:
Assuming process() is missing without checking
Thinking stream is not iterable
Ignoring variable initialization
5. In a Lambda architecture, if the batch layer updates data every 24 hours and the speed layer processes data every second, what is the best way to ensure the serving layer shows the most accurate and up-to-date results?
hard
A. Use the speed layer results immediately and replace them with batch results after 24 hours
B. Only show batch layer results to avoid inconsistency
C. Ignore speed layer and rely on batch layer for all queries
D. Merge speed and batch results continuously, updating serving layer with combined data
Solution
Step 1: Understand serving layer role
The serving layer combines batch and speed layer outputs to provide accurate and timely results.
Step 2: Evaluate options
Merge speed and batch results continuously, updating serving layer with combined data merges both results continuously, ensuring the serving layer is both up-to-date and accurate.
Final Answer:
Merge speed and batch results continuously, updating serving layer with combined data -> Option D
Quick Check:
Serving layer = combined batch + speed data [OK]
Hint: Serving layer merges batch and speed data continuously [OK]
Common Mistakes:
Using only speed or batch layer results alone
Replacing speed results abruptly without merging
Ignoring serving layer's combining role
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.