Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

🏠 Back to Blog

DAG and Parallel Execution

  • dag template: tasks with explicit dependencies
  • Tasks with no dependencies start immediately (in parallel)
  • A task starts once all its dependencies finish successfully
- name: main-dag
  dag:
    tasks:
    - name: task-a
      template: worker
    - name: task-b
      template: worker
      dependencies: [task-a]
    - name: task-c
      template: worker
      dependencies: [task-a]
    - name: task-d
      template: worker
      dependencies: [task-b, task-c]
  • a runs first, b and c in parallel, d waits for both

DAG vs steps

  • steps: list of lists, order implied by position, simpler for linear flows
  • dag: explicit dependencies, better for complex graphs with many parallel paths, nicer visualization
  • Task outputs: {{tasks.<name>.outputs.parameters.<p>}} (steps uses steps.)

Dependencies

  • dependencies: [a, b] - needs all of them to succeed
  • If a dependency fails, dependent tasks do not run
    • continueOn on the task proceeds anyway:
- name: cleanup
  dependencies: [test]
  continueOn:
    failed: true
  • depends - boolean expression on task results, replaces dependencies (can’t use both on one task)
    • results: .Succeeded, .Failed, .Errored, .Skipped, .Completed, .Daemoned
    • operators: &&, ||, !
    • plain depends: "task" is same as task.Succeeded
- name: deploy-prod
  depends: "test.Succeeded"
  template: deploy
- name: notify-failure
  depends: "test.Failed"
  template: send-alert
  • when is still available for conditions on values, e.g. when: "{{tasks.quality-gate.outputs.parameters.passed}} == true"
    • it only evaluates after dependencies succeed, so it can’t be used to catch failures. Use depends for that

Parallelism

  • All independent tasks run at once
  • Limit with parallelism
    • workflow level: spec.parallelism: 2
    • template level: parallelism on the dag/steps template
spec:
  entrypoint: main
  parallelism: 2     # max 2 pods at once
  • Controller-wide defaults can be set in workflow-controller-configmap

Dynamic parallelism

  • withItems - static list
  • withParam - JSON list, usually from another task’s output
  • withSequence - number range
- name: generate-list
  template: generate-items      # script printing json.dumps([...])
- name: process-items
  template: processor
  dependencies: [generate-list]
  arguments:
    parameters:
    - name: item
      value: "{{item}}"
  withParam: "{{tasks.generate-list.outputs.result}}"
  • List of objects: {{item.id}}, {{item.data}}

Fan-out / fan-in

  • fan-out: one task creates work, many tasks process it in parallel
  • fan-in: a task depending on all of them aggregates
  • Static: list each process-N task with dependencies: [prepare-work], and aggregate with dependencies: [process-1, process-2, process-3]
  • Dynamic: withParam on the processing task, then
- name: aggregate-results
  template: aggregator
  dependencies: [process-tasks]     # waits for every expanded task
  • To collect outputs of all expanded tasks use {{tasks.process-tasks.outputs.parameters}} (JSON list of all outputs)

CI/CD style DAG

  • checkout -> (build-backend, build-frontend, security-scan) in parallel -> tests -> quality-gate -> deploy -> smoke-tests -> notify
- name: quality-gate
  template: check-quality
  dependencies: [test-backend, test-frontend, integration-tests, security-scan]
- name: deploy-to-staging
  template: deploy
  dependencies: [quality-gate]
  when: "{{tasks.quality-gate.outputs.parameters.passed}} == true"

Remember

  • dependencies / depends for ordering, when for conditions

  • Failed dependency blocks dependents unless continueOn or depends with .Failed/.Completed

  • parallelism caps concurrency

  • withItems static, withParam dynamic

  • Lab 03: Building DAG Workflows

  • DAG walk-through

  • Loops