Scala
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Scala
-
Section 4: Functional Scala
-
Section 5: Collections in Depth
-
Section 6: Type System
-
Section 7: Concurrency and Ecosystem
-
Section 8: Practical Projects
-
Section 9: Interview Practice
-
Section 10: Data Structures and Algorithms in Scala
-
Section 11: More Practice Exercises
-
Section 12: Advanced Functional Patterns
-
Section 13: More Ecosystem
-
Section 14: Scala Collections Library Deep Dive
-
Section 15: Scala Standard Library Deep Dive
-
Section 16: Akka Ecosystem Deep Dive
-
Section 17: Cats and Cats Effect Deep Dive
-
Section 18: Play Framework Deep Dive
-
Section 19: Apache Spark with Scala Deep Dive
-
Section 20: Scala Build Tools Deep Dive
-
Section 21: Scala 3 Specific Features
-
90: Union and Intersection Types
-
Section 22: Scala Testing Deep Dive
-
Section 23: Functional Domain Modeling
-
Section 24: More Data Structures and Algorithms in Scala
-
Section 25: Scala for Data Engineering
-
Section 26: More Practical Projects
-
Section 27: More Interview and Review
-
Section 28: ZIO Ecosystem Deep Dive
-
Section 29: Scala for Machine Learning
-
Section 30: Scala Microservices Architecture
-
Section 31: Scala Type System Deep Dive
-
Section 32: More Practice and Drills
-
Section 33: Scala Performance Deep Dive
-
Section 34: Scala Ecosystem Tooling
-
Section 35: Scala for Reactive Systems
-
Section 36: More Real-World Case Studies
-
Section 37: Scala for Financial Systems
-
Section 38: Scala GraphQL and gRPC
-
Section 39: More Final Projects
-
Section 40: More Interview and Final Review
-
Section 41: Scala for Streaming Data
-
Section 42: Scala Security Practices
-
Section 43: More Language Deep Dive
-
Section 44: Scala Command-Line Tools
-
Section 45: Scala Documentation and Style
-
Section 46: Scala Dependency Management
-
Section 47: More Practical Backend Patterns
-
Section 48: Scala for Event-Driven Architecture
-
Section 49: More Practice Drills Round 2
-
Section 50: Scala Compiler Deep Dive
-
Section 51: Scala for Web Frontends
-
Section 52: More Data Engineering Practice
-
Section 53: Scala Observability
-
Section 54: More Advanced Practice Projects
-
Section 55: Scala for Legacy Java Integration
-
Section 56: More Testing Practice
-
Section 57: Final Mastery Review
-
Section 58: Scala History and Ecosystem Context
-
Section 59: More Concurrency Patterns
-
Section 60: Scala for Configuration Management
-
Section 61: More Domain Modeling Practice
218: Building a Fraud Detection Pipeline with Spark
Imagine you're a security guard at a high-end jewelry store. You aren't just watching the front door; you're watching a continuous stream of people walking in and out. Now, a single person entering the store is normal. But if you see the same person enter the store, leave, and come back five times in ten minutes—each time wearing a different disguise—you're going to step in. You aren't judging them based on a single snapshot in time, but on a pattern of behavior over a specific window of time.
That is exactly how we build a fraud detection pipeline in Spark. We aren't looking for a single "bad" transaction; we're looking for "bad" sequences. To do this, we map our security guard's logic directly into Spark Structured Streaming:
- The Flow of People: This is our
DataStreamReader, pulling transactions from a source like Kafka. - The Disguises/IDs: These are our grouping keys (like
userIdorcreditCardNumber). - The Ten-Minute Window: This is the
window()function, which tells Spark to bucket events into time-slices. - The Red Flag: This is our
filter()condition—for example, "more than 3 transactions in 10 minutes from different IP addresses."
Defining the Red Flags in Code
When I first started writing these pipelines, I tried to handle the state manually using a database. It was a nightmare of race conditions. With Spark, we use groupBy combined with a window. Let's look at how we catch a "velocity attack"—where a card is swiped too many times in a short burst.
import org.apache.spark.sql.functions._
import org.apache.spark.sql.streaming.Trigger
case class Transaction(userId: String, amount: Double, timestamp: java.sql.Timestamp, location: String)
val transactions = spark.readStream
.schema(transactionSchema)
.json("/data/transactions")
val fraudAlerts = transactions
.withWatermark("timestamp", "10 minutes") // Handle late-arriving data
.groupBy(
window($"timestamp", "10 minutes", "5 minutes"),
$"userId"
)
.agg(count("*").as("tx_count"), collect_set($"location").as("distinct_locations"))
.filter($"tx_count" > 3 && size($"distinct_locations") > 1)
fraudAlerts.writeStream
.outputMode("update")
.format("console")
.start()
Dealing with the "Late Arrival" Problem
In the real world, data is messy. A mobile app might lose signal and upload a transaction five minutes after it actually happened. If your window closes at 10:00, and a 09:58 transaction arrives at 10:02, you don't want to ignore it, but you also can't keep the window open forever or you'll run out of memory.
That's why I included withWatermark in the code above. Think of the watermark as a "grace period." By setting it to 10 minutes, you're telling Spark: "Wait for late data, but once the system clock hits 10:15, I no longer care about anything that happened before 10:05." It's a trade-off between perfect accuracy and system stability. I've seen clusters crash because engineers forgot watermarks and the state grew until the JVM exploded.
Tuning the Sliding Window
You'll notice I used window($"timestamp", "10 minutes", "5 minutes"). The first duration is the window length, and the second is the slide duration.
If the slide is the same as the length, you have "tumbling windows" (they don't overlap). But for fraud, we usually want "sliding windows." By sliding every 5 minutes, we ensure that a burst of fraud spanning the boundary of two tumbling windows doesn't slip through the cracks. It's like the security guard checking the logs every 5 minutes rather than once every 10.
📋 Practical Task
Build a High-Velocity Geographic Jump Detector
Your task is to extend the fraud pipeline to detect "Impossible Travel." This occurs when a user makes transactions in two different cities within a time window that would be physically impossible to travel between.
Requirements:
- Create a Spark Structured Streaming pipeline that processes a stream of
Transactionobjects (userId, amount, timestamp, city). - Implement a sliding window of 30 minutes, sliding every 10 minutes.
- Use
collect_set("city")to track all unique cities a user visited within that window. - Filter for users who have appeared in more than 2 different cities within that 30-minute window.
- Apply a watermark of 15 minutes to handle delayed events.
- Output the results to the console in
updatemode, showing theuserIdand the list of cities they "visited."
There are no comments for now.