Manager and worker nodes in Docker - Time & Space Complexity
When managing Docker Swarm, understanding how tasks are assigned and managed helps us see how the system scales.
We want to know how the time to assign and run tasks changes as we add more worker nodes.
Analyze the time complexity of this Docker Swarm manager assigning tasks to worker nodes.
# Initialize swarm manager
docker swarm init
# Add worker nodes
for i in $(seq 1 $NUM_WORKERS); do
docker swarm join --token SWMTKN-... worker-node-$i
docker node update --availability active worker-node-$i
done
# Manager schedules tasks to workers
This snippet shows a manager initializing the swarm and adding multiple workers, then scheduling tasks to them.
Look for repeated actions that affect time.
- Primary operation: Loop adding each worker node to the swarm.
- How many times: Once per worker node, so as many times as the number of workers.
As the number of worker nodes increases, the manager must perform more join and update commands.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 join and update commands |
| 100 | About 100 join and update commands |
| 1000 | About 1000 join and update commands |
Pattern observation: The number of operations grows directly with the number of worker nodes.
Time Complexity: O(n)
This means the time to add and manage workers grows linearly as you add more worker nodes.
[X] Wrong: "Adding more workers happens instantly and does not affect time."
[OK] Correct: Each worker requires a join and update step, so more workers mean more steps and more time.
Understanding how task assignment scales with workers shows you can think about system growth and resource management clearly.
"What if the manager assigned tasks in batches instead of one by one? How would the time complexity change?"