Skip to Content
Course content

209: Data Quality Monitoring in ETL Pipelines

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I've seen this happen more times than I care to admit: a pipeline runs perfectly. The logs are green, the job finishes in record time, and the dashboard shows everything is "healthy." Then, a business analyst pings you because the monthly revenue report is off by 15%. You dive in and realize that three weeks ago, an upstream team changed a field from a required double to an optional string, and your code has been silently swallowing the nulls ever since.

case class Transaction(id: String, amount: Option[Double], currency: String)

def processTransactions(txs: List[Transaction]): Double = {
  // The "Silent Killer" pattern
  txs.map(_.amount.getOrElse(0.0)).sum
}

The Danger of Silent Defaults

The code above is "safe" in the sense that it won't throw a NullPointerException. But in a production ETL pipeline, safety isn't just about avoiding crashes; it's about ensuring data integrity. By using .getOrElse(0.0), you've effectively hidden a data quality crisis. You aren't distinguishing between a transaction that actually cost zero dollars and a transaction where the data is missing. Over a million rows, a 1% failure rate in your source data becomes a massive financial discrepancy that is nearly impossible to trace backward.

Tracking Corruption with Validated Types

To fix this, we need to stop treating data quality as a binary (it works or it crashes) and start treating it as a metric. I prefer using a pattern that separates "Clean" records from "Quarantined" records. Instead of forcing a default value, we wrap the result in a type that tells us exactly what went wrong.

sealed trait ValidationResult[+A]
case class Valid[A](data: A) extends ValidationResult[A]
case class Invalid(reason: String, rawRecord: String) extends ValidationResult[Nothing]

case class TransactionRecord(id: String, amountStr: String)

def validateTransaction(rec: TransactionRecord): ValidationResult[Transaction] = {
  val amountOpt = scala.util.Try(rec.amountStr.toDouble).toOption
  
  amountOpt match {
    case Some(amt) if amt >= 0 => Valid(Transaction(rec.id, Some(amt), "USD"))
    case Some(amt) => Invalid(s"Negative amount: $amt", rec.toString)
    case None => Invalid(s"Invalid numeric format: ${rec.amountStr}", rec.toString)
  }
}

Implementing a Quality Threshold Guardrail

Now that we can identify bad records, we don't just log them—we monitor the ratio of bad to good data. In a real-world scenario, you might be okay with 0.1% of records being malformed, but 5% suggests a breaking schema change upstream that requires an immediate stop to the pipeline.

def runPipeline(rawRecords: List[TransactionRecord]): Unit = {
  val results = rawRecords.map(validateTransaction)
  
  val clean = results.collect { case Valid(tx) => tx }
  val dirty = results.collect { case Invalid(reason, raw) => (reason, raw) }
  
  val corruptionRate = dirty.size.toDouble / rawRecords.size
  
  // This is your "Circuit Breaker"
  if (corruptionRate > 0.05) { 
    throw new Exception(s"Data Quality Threshold Exceeded! Corruption rate: ${corruptionRate * 100}%")
  }
  
  println(s"Processed ${clean.size} records. Corruption rate: ${corruptionRate * 100}%")
  // Proceed to save 'clean' to the database...
}

By shifting the logic this way, the pipeline fails loudly and specifically when the data degrades. You get a clear error message telling you the corruption rate, and you have a list of Invalid records (the "Dead Letter Queue") that you can hand back to the upstream team to fix. You've moved from "guessing" why your numbers are wrong to "knowing" exactly where the data broke.




📋 Practical Task

Building a Transaction Quality Guardrail

You are tasked with implementing a data quality monitor for a stream of sensor readings. Each reading arrives as a SensorReading(id: String, value: String). A reading is considered "Valid" if the value is a number between -50.0 and 150.0. Any other value is considered "Corrupt".

Write a Scala program that does the following:

  • Defines a ValidationResult algebraic data type (ADT) to handle Valid and Invalid states.
  • Implements a validateReading function that parses the string value and checks the range.
  • Processes a provided list of readings and calculates the percentage of corrupt records.
  • Throws a RuntimeException if the corruption rate exceeds 10%, otherwise prints the total number of valid records processed.

Test Data to use:

val readings = List(
  SensorReading("S1", "23.5"),
  SensorReading("S2", "160.0"), // Invalid: Too high
  SensorReading("S3", "abc"),   // Invalid: Not a number
  SensorReading("S4", "-10.2"),
  SensorReading("S5", "45.0")
)
Rating
0 0

There are no comments for now.

to be the first to leave a comment.