Parallel stages execution in Jenkins - Time & Space Complexity
When Jenkins runs stages in parallel, it can do many tasks at the same time.
We want to see how the total time changes as we add more parallel stages.
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent any
stages {
stage('Parallel Work') {
parallel {
stage('Task 1') {
steps { echo 'Running Task 1' }
}
stage('Task 2') {
steps { echo 'Running Task 2' }
}
}
}
}
}
This code runs two tasks at the same time inside a parallel block.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Running each parallel stage's steps.
- How many times: Once per stage, but all run simultaneously.
Adding more parallel stages means more tasks start at the same time.
| Input Size (n) | Approx. Operations |
|---|---|
| 2 | 2 tasks run simultaneously |
| 10 | 10 tasks run simultaneously |
| 100 | 100 tasks run simultaneously |
Pattern observation: Total time depends on the longest single task, not the number of tasks.
Time Complexity: O(1)
This means the total time stays about the same no matter how many parallel stages run, assuming enough resources.
[X] Wrong: "More parallel stages always take more total time because they add up."
[OK] Correct: Parallel stages run at the same time, so total time depends on the slowest task, not the count.
Understanding parallel execution helps you explain how to speed up builds by running tasks together, a useful skill in real projects.
What if the parallel stages share limited resources and cannot truly run at the same time? How would the time complexity change?
Practice
parallel block in a Jenkins pipeline?Solution
Step 1: Understand the role of parallel block
Theparallelblock allows multiple tasks or stages to run simultaneously in Jenkins.Step 2: Compare with other options
Options A, B, and C describe restart, sequential execution, and manual pause, which are unrelated to parallel execution.Final Answer:
To run multiple stages at the same time to save time -> Option AQuick Check:
Parallel stages = run together [OK]
- Thinking parallel means sequential execution
- Confusing parallel with manual approval
- Assuming parallel restarts the pipeline
Solution
Step 1: Recall correct parallel syntax
Theparallelblock should use a map of branches, each containing steps.Step 2: Analyze options
parallel([ "Build": { steps { echo 'Building' } }, "Test": { steps { echo 'Testing' } } ]) correctly defines parallel branches. Options B, C, and D misuse the placement ofparallelor stages.Final Answer:
parallel([ "Build": { steps { echo 'Building' } }, "Test": { steps { echo 'Testing' } } ]) -> Option DQuick Check:
parallel wraps stages directly [OK]
- Putting parallel inside steps block
- Nesting stages inside other stages incorrectly
- Using parallel with single stage only
pipeline {
agent any
stages {
stage('Parallel Stage') {
steps {
parallel (
"A": {
echo 'Running A'
},
"B": {
echo 'Running B'
}
)
}
}
}
}Solution
Step 1: Understand parallel execution output
Stages insideparallelrun at the same time, so their output order is not guaranteed.Step 2: Check syntax validity
The syntax is valid for parallel branches inside a stage's steps block, so no syntax error occurs.Final Answer:
Running A and Running B in any order or simultaneously -> Option CQuick Check:
Parallel stages output order is unpredictable [OK]
- Assuming output order is always A then B
- Thinking nested stages cause syntax error here
- Expecting sequential output in parallel block
pipeline {
agent any
stages {
stage('Test') {
steps {
parallel {
stage('Unit') {
steps { echo 'Unit tests' }
}
stage('Integration') {
steps { echo 'Integration tests' }
}
}
}
}
}
}
What is the error and how to fix it?Solution
Step 1: Identify parallel block syntax rules
Theparalleldirective expects a map of branch names to closures containing steps, not loose stage blocks inside {}.Step 2: Fix the pipeline structure
Change to parallel( "Unit": { echo 'Unit tests' }, "Integration": { echo 'Integration tests' } ) to fix the syntax error.Final Answer:
parallel block cannot contain nested stage blocks directly; use map syntax with branches -> Option BQuick Check:
parallel uses branch map syntax [OK]
- Placing parallel inside steps block
- Ignoring pipeline syntax hierarchy
- Assuming echo commands cause error
Solution
Step 1: Understand dependency requirements
'Deploy' must wait for 'Build' and 'Test' to finish successfully, so it cannot run in parallel with them.Step 2: Structure pipeline accordingly
Run 'Build' and 'Test' inside aparallelblock, then add a separate 'Deploy' stage after that block to run only if previous succeed.Final Answer:
Use parallel for 'Build' and 'Test' stages, then a separate 'Deploy' stage after parallel block -> Option AQuick Check:
Dependent stage runs after parallel block [OK]
- Putting dependent stage inside parallel block
- Running deploy before build and test
- Trying to conditionally skip stages inside parallel
Ready to prove it?
Pass the test and this pattern moves unknown
