6: Troubleshooting, Monitoring, and Optimization

    Identifying Trends in Job Performance Using the Lakeflow Jobs Run-History View

    A production data engineer spends more time investigating "why is this slower than yesterday" than building new pipelines. The run-history view of a Lakeflow Job is the first place that question is answered, because every triggered, scheduled, file-arrival, and table-update run is recorded with its duration, status, parameters, cluster spec, and per-task breakdown.

    Reading that history correctly distinguishes a one-off blip from a sustained regression, separates data growth from code change, and points the next investigation step at the right surface (Spark UI, executor logs, cluster event log, or the upstream table).

    The run history of a job is reached from the Lakeflow Jobs UI by opening the job and switching to the Runs tab, or programmatically by calling jobs/runs/list against the Databricks REST API or databricks jobs list-runs --job-id <id> from the CLI. Each row in the list represents one execution and surfaces the start time, end time, total duration, trigger type (manual, scheduled, file arrival, table update, continuous), the parameter set captured for that run, and the final outcome.

    The view exposes columns for the run ID, the start time in the workspace timezone, the duration in human-readable form (for example 00:42:13), the queue duration, the setup duration, and the execution duration broken out separately.

    Reading the run-history columns

    The four duration fields tell different stories and must be read separately:

    • Queue duration is the time the run spent waiting for any prerequisite to become available, including waiting for an active run to complete when max_concurrent_runs = 1, waiting for a cluster instance pool to allocate a VM, or waiting for a Serverless Jobs compute warm pool slot. A growing queue duration with steady execution duration points at upstream concurrency, not at the pipeline code.
    • Setup duration is the time spent provisioning the job cluster: requesting VMs from the cloud provider, attaching disks, installing libraries declared on the cluster, and pulling the requested Databricks Runtime image. A setup duration that grows from one minute to seven minutes typically means a new init script, a new library install, or a switch from Serverless compute to a classic job cluster.
    • Execution duration is the time the tasks actually ran on the cluster. This is the field that responds to data growth and code change.
    • Cleanup duration is the time spent tearing the cluster down after the last task finished. It is usually small but grows when a final cleanup task issues a long VACUUM or a wide OPTIMIZE.

    The run history also captures the cluster fingerprint for each run: node type, worker count (or autoscale bounds), Databricks Runtime version, Photon on/off, and instance pool ID if any. When run durations diverge, comparing the cluster fingerprints of a fast run and a slow run answers the "did anything about the environment change" question before any Spark-level investigation.

    Establishing a baseline

    A regression has no meaning without a baseline.

    The simplest baseline is the rolling median of the last N successful runs of the same job, which filters out one-off outliers (a noisy neighbor, a single slow Auto Loader file listing) without being distorted by failed runs.

    A typical baseline window is twenty-five to fifty successful runs, large enough to stabilize and short enough to track gradual data-volume growth.

    The baseline is best computed against execution duration, not total duration, because total duration includes queue and setup which respond to operational changes (a busy workspace, a new init script) rather than the workload itself.

    When a pipeline is run hourly, twenty-four runs per day cover the entire diurnal cycle and capture the natural variance between a 03:00 run on near-empty volumes and a 09:00 run on the morning ingest peak.

    -- Compute a rolling baseline for any Lakeflow Job run history exported to Delta
    WITH successful AS (
     SELECT
     job_id,
     run_id,
     start_time,
     execution_duration_ms / 1000.0 AS execution_seconds
     FROM main.observability.job_runs
     WHERE result_state = 'SUCCESS'
    )
    SELECT
     job_id,
     run_id,
     start_time,
     execution_seconds,
     percentile_cont(0.5) WITHIN GROUP (ORDER BY execution_seconds)
     OVER (PARTITION BY job_id ORDER BY start_time
     ROWS BETWEEN 25 PRECEDING AND 1 PRECEDING) AS median_25,
     execution_seconds /
     NULLIF(
     percentile_cont(0.5) WITHIN GROUP (ORDER BY execution_seconds)
     OVER (PARTITION BY job_id ORDER BY start_time
     ROWS BETWEEN 25 PRECEDING AND 1 PRECEDING),
     0
     ) AS slowdown_factor
    FROM successful
    ORDER BY job_id, start_time DESC;
    

    A slowdown_factor of 1.1 means the current run is ten percent slower than the rolling median of the previous twenty-five successful runs; 2.0 means double; 0.7 means thirty percent faster. The threshold for investigation is workload-dependent: tightly bounded SLA jobs warrant action above 1.2, while exploratory backfills tolerate 2.0 routinely.

    Comparing two specific runs

    When the run history flags a regression, the next step is a direct comparison of the slow run against the fastest recent successful run.

    The Lakeflow Jobs UI supports this through the Compare action on the Runs tab:

    • selecting two runs side-by-side displays per-task durations,
    • parameter differences,
    • cluster differences,
    • and per-task result states aligned by task name in the DAG.

    The same comparison is reachable programmatically by joining two jobs/runs/get responses on task_key.

    The fields that explain the largest share of regressions, in roughly descending order of frequency:

    1. Task duration shift on one specific task while peers are unchanged. This is a code or data change scoped to that task's logic.
    2. Parameter difference: a different date parameter, a different mode parameter, or a full-refresh flag toggled on.
    3. Cluster shape change: a node type swap, a worker count reduction, Photon turning off, or a runtime version change.
    4. Upstream data volume: the bronze table the task reads from doubled in row count overnight; the task duration scaled accordingly even though the code did not change.
    5. Library version change: a new whl file in the cluster libraries, a notebook %pip install with an unpinned version, or a base image bump.
    # Programmatic side-by-side comparison of two runs of the same job
    from databricks.sdk import WorkspaceClient
    
    w = WorkspaceClient()
    
    slow_run = w.jobs.get_run(run_id=11223344).as_dict()
    fast_run = w.jobs.get_run(run_id=11223300).as_dict()
    
    slow_tasks = {t["task_key"]: t for t in slow_run["tasks"]}
    fast_tasks = {t["task_key"]: t for t in fast_run["tasks"]}
    
    for key in sorted(slow_tasks):
     s = slow_tasks[key].get("execution_duration", 0) / 1000.0
     f = fast_tasks.get(key, {}).get("execution_duration", 0) / 1000.0
     delta = s - f
     if f > 0 and abs(delta) > 30:
     print(f"{key:40s} slow={s:8.1f}s fast={f:8.1f}s delta={delta:+8.1f}s")
    

    A printout that shows a single task with a large positive delta while every other task is within a few seconds is a strong signal to inspect that task's Spark UI. A printout where every task is uniformly slower points at a cluster-level cause: smaller workers, fewer workers, or Photon disabled.

    Reading trend shapes

    Three trend shapes dominate the run-history view, each with its own diagnosis:

    • Step change: the duration was stable around X for weeks, then on a specific date moved permanently to Y. This is almost always a code deployment (Declarative Automation Bundle release), a cluster spec change, or a one-time data backfill that grew the source table.
    • Gradual drift: the duration creeps up by a small percentage per week. This is data volume growth, sometimes combined with table fragmentation (many small files accumulating in the absence of OPTIMIZE). The remediation is Liquid Clustering plus predictive optimization, covered later in this chapter.
    • Periodic spikes: every Monday morning, every month-end, or every quarter-end the run is much slower than the rest. This is a calendar-driven data-volume peak (reconciliation, settlement, audit close). The remediation is either schedule the heavy run on its own larger cluster, or pre-partition the work into a For each task that fans out.

    💡 Exam Trap: A job that took thirty minutes for the last twenty successful runs and suddenly takes ninety minutes is showing a step change, not data growth. Always look at the cluster spec and parameters of the slow run before blaming the data; the question that asks "what is the first diagnostic step" expects you to compare the slow run's environment to a recent fast run, not to open the Spark UI.

    💡 Exam Trap: Total run duration on the Runs tab includes queue, setup, execution, and cleanup. A run that shows fifteen minutes total but only forty seconds of execution did not "run for fifteen minutes"; it spent fourteen minutes provisioning. Knowing which duration to read is a frequent multiple-choice trap.

    📊 Limit: The Lakeflow Jobs API retains run history for sixty days by default. Beyond that window, only runs that were exported to a Delta sink (via system tables or a custom runs/list exporter) remain queryable. For long-term trend analysis, configure the Unity Catalog system tables for jobs (system.lakeflow.jobs and system.lakeflow.job_run_timeline), which retain run-level data for the full system-table retention period.

    ⚠️ Anti-Pattern: Diagnosing a slow run by opening the Spark UI of that one run in isolation. Without a baseline, "the join took twelve minutes" is meaningless; the same join took eleven minutes yesterday on the same data. Always pull the previous fast run into view first.

    🎯 Scenario: A nightly silver build that has been running for forty minutes for six months now takes ninety minutes. The Runs tab shows queue duration steady at ten seconds, setup duration up from one minute to four minutes, execution duration up from thirty-nine minutes to eighty-six minutes. The cluster fingerprint shows the worker node type changed from Standard_D8s_v5 to Standard_D4s_v5. The diagnosis is a Declarative Automation Bundle deployment that overrode the production cluster spec with the dev cluster spec; the remediation is to revert the bundle target override.

    Decision Anchor

    Choose the run-history view when the question is "is this run abnormal" or "when did the regression start". Choose the Spark UI of a single run when the question is "what part of this run is slow". The run-history view ranks and locates; the Spark UI dissects. Skipping the ranking step and jumping straight to Spark UI of an arbitrary run wastes time investigating noise.

    System tables for run history

    For workloads that need history beyond sixty days or want to build dashboards on top of run trends, Unity Catalog exposes job execution data through system tables: system.lakeflow.jobs (job metadata), system.lakeflow.job_tasks (task definitions), system.lakeflow.job_run_timeline (one row per run), and system.lakeflow.job_task_run_timeline (one row per task run). These tables are read-only Delta tables maintained by Databricks and queryable from any SQL warehouse or notebook.

    -- Average execution time per job over the last 90 days, with run count
    SELECT
     jr.job_id,
     j.name,
     COUNT(*) AS run_count,
     AVG(jr.execution_duration_ms) / 1000.0 / 60.0 AS avg_minutes,
     MAX(jr.execution_duration_ms) / 1000.0 / 60.0 AS max_minutes
    FROM system.lakeflow.job_run_timeline jr
    JOIN system.lakeflow.jobs j
     ON jr.job_id = j.job_id
    WHERE jr.period_start_time >= date_sub(current_date(), 90)
     AND jr.result_state = 'SUCCESS'
    GROUP BY jr.job_id, j.name
    ORDER BY avg_minutes DESC;
    

    System-table dashboards built once and refreshed by a downstream Lakeflow Job survive workspace UI changes, work across multiple workspaces in the same account, and are the recommended foundation for long-term capacity planning. ---

    Continue with the interactive course

    Track your progress, jump into practice questions and use the Shark AI tutor inside the Databricks - Data Engineer Associate learning hub.