The debugging round

Part of the free LogicWiz Data Engineering course, Module 18: Interview rounds. The SQL and Python run in the app, on real Postgres and real Python in the browser.

It is the debugging round. The interviewer pastes forty lines into the chat and says: "This ran in 20 minutes last week. Today it is at three hours, and one executor has just died. What is going on?" There is a task table, a stack trace and a plan. Somewhere in there is one number that is wrong. The round is about finding it before you start guessing.

You'll learn to

  • Read evidence in a fixed order, ending with the proof
  • Diagnose Spark from task tables, event logs, plans and error messages
  • Diagnose the rest: consumer lag, streaming progress, Postgres spills, slots

Learn

How the round runs

The interviewer pastes evidence and asks "what is wrong?" It might be a stage's task table, a plan from explain(), a stack trace, the output of kafka-consumer-groups --describe, or a few lines of EXPLAIN ANALYZE. There is no single right tool. There is a right order:

  1. Say what the evidence is, and what normal looks like: "one stage, 12 tasks, a median of 30 s".
  2. Find the number out of line with its peers or its history.
  3. Name the mechanism that produces exactly that number.
  4. Give the fix and its proof: which metric should move, and by how much.

Each problem below hands you the evidence as a file and states the team's runbook rules (thresholds, labels). You write the function an alert or a runbook would run.

Build one answer: the three-hour job

Take the scene. The first thing in the paste is the task table of stage 7: twelve tasks, each with its run time, the shuffle data it read and what it spilled to disk.

Read it in the four moves, and say each one out loud. The bars below are the run times of the twelve tasks.

Stage 7, read in four moves

Bars for the twelve tasks of stage 7: eleven take between 28 and 34 seconds and task 7 takes 415 seconds; the steps name what normal is, mark task 7 as the straggler, add its shuffle read and spill, and show it back at the median after the fix.

  1. Say what it is, and what normal looks like. "This is the task table of one stage: 12 tasks. Eleven of them took between 28 and 34 seconds, so the median is 30.5 s."

  2. Find the number out of line. "Task 7 took 415 s. That is 13.6 times the median, and a stage takes as long as its slowest task."

  3. Name the mechanism. Look at the next column of the table. "Task 7 also read 1,690 MB of shuffle data, 14 times the median, and spilled 2,350 MB. Fourteen times the data is skew: a hot key. With normal data it would be a slow machine."

  4. Give the fix and its proof. "If this is a join, AQE's skew split handles it. If it is an aggregation, I would salt the key. I would confirm by task 7 coming down to about the median, and the stage time with it."

Twelve bars, one per task, all on the same scale. The picture barely changes between the first three steps. What changes is what you are able to say about it.

Evidence, and its tell

EvidenceLook atThe tellUsually means
Stage task tablemax against median duration, and shuffle readone task 10x the median with 10x the dataskew; with normal data, a slow node
Stage task tableSpill (disk), GC timespill on most tasks, GC over 10% of run timepartitions too big for task memory
explain()join nodes, Exchanges, PushedFiltersBroadcastNestedLoopJoin, CartesianProduct, PushedFilters: [] under a filtera non-equi join; a filter Spark could not push, such as a Python UDF
Executor errorthe first exception lineJava heap space; Container killed ... exceeding physical memory limitsheap too small for one task; overhead too small
Driver errorthe first exception lineis bigger than spark.driver.maxResultSizea collect() or a broadcast of big data
Stage retrythe reasonFetchFailedException after a lost executorits shuffle files went with it
Streaming progressinput against processed rows a second; state rowsprocessed below input for many batches; state always risingunder-provisioned; no watermark
Consumer group describeCURRENT-OFFSET in two samplesoffset frozen while LAG growsa stuck consumer
EXPLAIN ANALYZESort Method, Batchesexternal merge Disk:, Batches: 8workmem too small for that node
pgreplicationslotsretained WAL per slotan inactive slot holding gigabytesa stopped CDC connector

One row per kind of evidence: where to look, what gives it away, what it usually means. The tell is always relative: a 400-second task is fine in a stage of 400-second tasks. Compare with the peers in the same stage, or with last week's run.

A plan with two problems

A plan tree: a broadcast nested loop join of orders with tiers on a range condition, where the orders side is filtered by a Python UDF evaluated in BatchEvalPython above a Parquet scan whose pushed filters are empty.

The next clue in the paste: the plan. Read the two red nodes. A range condition has no equality key, so Spark cannot hash-join and compares every pair. Tiers is small, so it is a broadcast nested loop, slow but survivable; with two big tables it would be a CartesianProduct. And the UDF hides the filter from the optimiser: rewrite it as a join to a VIP table, or as a built-in expression, and Spark can push it into the scan.

Narrate the reading: "This is stage 7's task table. The median is 30 seconds; task 7 is 415 and read 14 times the median data, so it is a hot key, not a slow node. AQE's skew join would split it if this is a join; if it is an aggregation I would salt the key. I would confirm by the stage time dropping to about the median." Evidence, mechanism, fix, proof.

The trap: reading the wrong number. "Spill (memory)" is the size the spilled data had in memory; "Spill (disk)" is what was written, often far smaller. A FetchFailedException is a symptom: look for the executor that died just before it. An OutOfMemoryError on the driver is not an executor problem. And consumer lag is a total: one frozen partition can hide inside a healthy-looking sum.

Remember: what is it, what is normal, what is out of line, why, and how you would prove the fix.

That was the last round. Go back to the one that felt slowest, and do its ten problems again against the clock.

In one line

Debug from evidence in a fixed order: say what it is and what normal looks like, find the number out of line, name the mechanism, then give the fix and the metric that proves it.

Interview corner

One task of a stage runs 40 minutes while 199 finish in 30 seconds. What do you check, and what do you do?

I compare that task's shuffle read and records with the median. If it read far more, it is skew on a hot key: for a join, AQE's skew join splits it (on by default, partitions over 5 times the median and 256 MB); for an aggregation I salt the key or pre-aggregate the hot value apart. If it read the same as the others, it is the machine: I look at that executor's GC time and host, and turn on speculation so a copy runs elsewhere. Either way I confirm by the stage time dropping towards the median.

Executors die with "Container killed by YARN for exceeding physical memory limits". What do you change?

That is the cluster manager killing the container, not a Java heap error, so the memory outside the heap grew past the overhead: Python workers in PySpark, Arrow batches in pandas UDFs, netty buffers, or off-heap memory. I raise spark.executor.memoryOverhead (by default 10% of executor memory, at least 384 MB), or reduce what lives there, such as smaller Arrow batches. Making the heap bigger without more container memory makes it worse. The same kill on Kubernetes shows as OOMKilled with exit code 137.

Consumer lag on a topic keeps rising. How do you find out why?

I take two kafka-consumer-groups --describe samples a minute apart and look per partition, not at the total. If a few partitions have a frozen current offset while the rest move, a consumer is stuck: a poison message, a blocked call or a long GC, and I check its logs and restart it. If every partition moves but lag grows everywhere, the group is under-provisioned: add consumers up to the partition count, or make each one faster. If a partition has no consumer id, the group is rebalancing or a member died, and I check for max.poll.interval.ms (5 minutes by default) being exceeded.

A Postgres report got slow after the data doubled, and EXPLAIN ANALYZE shows "Sort Method: external merge Disk: 900000kB". What now?

The sort no longer fits in work_mem, 4 MB by default, so it writes temporary files and merges them. Each sort or hash node can use work_mem, so I raise it for that report's session or role rather than globally, where many connections times many nodes could exhaust memory. Better still, I check whether an index can deliver the rows already ordered, or whether a filter or a pre-aggregated table can shrink what is sorted. I confirm with a second EXPLAIN ANALYZE showing an in-memory quicksort and the new run time.