Conceptual Β· Decision frameworks Β· No hands-on lab
| Time | Segment | Activity |
|---|---|---|
| 0β10 min | Recap | L12 Spark β what is the micro-batch model in one sentence? |
| 10β25 min | The Core Difference | What true streaming actually means |
| 25β40 min | Exactly-Once Semantics | Why it exists, why banks care, why most don't need it |
| 40β55 min | Latency in Business Context | When does 200ms vs 10ms change the outcome? |
| 55β70 min | Full Decision Framework | Spark vs Flink vs ksqlDB vs Python β the complete map |
| 70β82 min | Case Study: UPI Fraud | Walk through the real-time fraud architecture decision |
| 82β88 min | Group Debate | "Spark is good enough for everything" β argue for or against |
| 88β90 min | Module 3 Wrap-Up | The streaming landscape + preview of Module 4 (Grafana) |
Spark (micro-batch): Imagine a factory where a bell rings every 200 milliseconds. Workers grab everything in front of them, sort it, and send results forward β then wait for the next bell. The minimum time from arrival to processing: anywhere from 1ms (just after the bell) to 199ms (just before the next one).
Flink (true streaming): The same factory, but each worker processes an item the instant it arrives β no bell, no waiting. Processing time: 1β10 milliseconds from arrival.
Spark: βββ [--200ms--] [--200ms--] [--200ms--]
β β β
run batch run batch run batch
Flink: βββ β β β β β β β β
(each event processed individually, within ~10ms)
Spark was designed first as a batch processing system β streaming was added later as "fast batching." Flink was designed from day one as a streaming system β batch processing was added as a special case. Neither is "better" β they made different primary trade-offs.
| Latency | Roughly Equivalent To | Business Example |
|---|---|---|
| 1ms | Blink of an eye Γ· 300 | High-frequency trading signal |
| 10ms | Blink of an eye Γ· 30 | Payment fraud check (Flink typical) |
| 100ms | Just perceptible as a pause | User feels slight lag |
| 200ms | Noticeable delay | Spark micro-batch typical |
| 500ms | Clearly slow | Google reports this hurts search usage |
| 2,000ms | Very slow | User considers abandoning the page |
The key business question is not "what latency does Flink provide?" β it is "what latency does my use case require?" For 90% of business applications, anything under 2 seconds is fine. That is Spark's territory. Flink is needed for the remaining 10%.
| Guarantee | Risk | Acceptable For | Not Acceptable For |
|---|---|---|---|
| At-most-once | Events may be lost on crash | Social media likes, page views | Financial transactions, medical records |
| At-least-once | Events may be double-counted on crash/replay | Analytics approximations, event logs | Billing, banking, inventory allocation |
| Exactly-once | No loss, no duplication β guaranteed | All financial and compliance cases | β |
When a system crashes mid-processing, it must answer on recovery: "Did I already write this result to the database before I crashed?" Achieving exactly-once requires coordinating the database write AND the Kafka offset confirmation as a single atomic operation.
| Sector | Use Case | Need Exactly-Once? | Why |
|---|---|---|---|
| Banking | Transaction processing | Yes | Duplicate = double debit |
| E-commerce | Order revenue totals | Yes | Billing error = legal liability |
| Telecom | Call data records | Yes | Over/under billing customers |
| Marketing | Ad impression counting | No | ~1% error is acceptable |
| Social media | Like/view counts | No | Approximate is fine |
| Logistics | Delivery ETA | No | Small error has no consequences |
Practical reality: Most companies use at-least-once processing and manage the "duplicate" problem through idempotent writes β designing database operations so that processing the same event twice produces the same result as processing it once. This is simpler than exactly-once and works for most cases.
With Spark (~200ms fraud check): user taps "Pay" and experiences essentially zero perceptible delay. With Flink (~10ms): same user experience. Business verdict: Spark is fine.
At 50,000 TPS, if the fraud pipeline becomes a bottleneck (batch takes longer than its trigger interval), transactions start queuing. Flink's consistent per-event processing avoids this queuing effect under load. Business verdict: At very high volume, Flink's consistency matters more than raw latency.
A 200ms delay means a competitor has already acted on the same signal. Flink at 10ms is competitive. For high-frequency trading, even Flink is often "too slow" β those systems use custom hardware. Business verdict: Spark is not appropriate here.
| Dimension | Python Consumer | Apache Spark | ksqlDB | Apache Flink |
|---|---|---|---|---|
| Latency | < 100ms | ~200msβ2s | ~50ms | < 10ms |
| Scale limit | ~1K events/sec | Millions/sec | Hundreds K/sec | Millions/sec |
| Exactly-once | No | Yes (batch) | Yes | Yes (per-event) |
| Infrastructure cost | None | MediumβHigh | Low | High |
| Talent cost | Low | Medium | Low | Very High |
| Best for | Learning, prototypes | Batch + streaming teams | SQL-native teams | Payments, trading |
| Factor | Apache Spark | Apache Flink |
|---|---|---|
| Monthly infra cost | βΉ30KββΉ3L (Databricks/EMR) | βΉ50KββΉ5L (self-managed or Confluent) |
| Time to first production job | 2β4 weeks | 6β12 weeks |
| Hiring difficulty | Moderate | Very Hard |
| Salary premium | 40β60% over general engineer | 80β120% over general engineer |
NPCI processes ~5 billion UPI transactions/month (~1,900 TPS average, much higher at peak). Fraud must be detected and flagged before clearing: <300ms SLA.
Each transaction must be checked for:
| Option | Assessment |
|---|---|
| Manual Python Consumer | Single-threaded, ~1K events/sec. β Cannot handle 1,900 TPS peak. |
| Apache Spark Streaming | Handles 1,900 TPS across distributed cluster. Micro-batch 200ms β within 300ms SLA. β Scale sufficient, latency acceptable. |
| Apache Flink | ~10ms per-transaction latency. Stateful per-user history in-memory (no database roundtrip for checks 1β4). β Better latency margin, better for stateful checks. Higher cost. |
"Spark's micro-batch is sufficient for 95% of real business use cases. The additional complexity and cost of Flink is rarely justified."
"As data volumes and latency expectations grow, Flink's true streaming model will become the standard. Companies that invest now will have a competitive advantage."
Time: 5 minutes to prepare arguments, 5 minutes of structured debate, 2 minutes for faculty summary.
Faculty debrief: Both positions have merit β context determines correctness. The question is always "what does the business need?" not "what's the best technology?"
| L11: Windowing | L12: Spark | L13: Flink | |
|---|---|---|---|
| Core concept | Slicing infinite streams into measurable chunks | Distributed micro-batch processing | True per-event stream processing |
| What you built | consumer_window.py (tumbling + sliding) | Observed spark_consumer.py (demo) | Conceptual understanding |
| Best for | Any consumer needing time-based aggregations | Large-scale batch+streaming, SQL teams | Sub-100ms latency, financial exactly-once |
| Business skill | Choose the right window type | Know when to invest in distributed processing | Know when latency justifies the cost |
Module 2: We made Kafka work β producers, consumers, MongoDB, alert routing
Module 3: We learned to aggregate intelligently (windows), and understand the
frameworks that handle this at scale (Spark, Flink)
Module 4: We stop building backends and focus on the output β dashboards.
All of this data you've been streaming? Now we make it visible.
Starting Lecture 14, we shift from building pipelines to making them visible. You will use the same data you have been streaming in Module 2 (transactions, INFY/TCS prices) and turn it into dashboards a business manager can actually use.