Parallel processes in distributed tensorflow
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Distributed TensorFlow lets several processes work on the same training job at once. Instead of one Python process owning the whole loop, multiple workers cooperate across machines or devices so larger datasets and models can train faster.
The important idea is that "parallel processes" in TensorFlow are not random background jobs. They usually follow a coordinated distribution strategy that defines how data is split, how gradients are synchronized, and which process is responsible for checkpointing.
Choose the Right Kind of Parallelism
In practice, most TensorFlow training uses data parallelism. Every worker keeps a copy of the model, reads a different shard of the input data, computes gradients locally, and then participates in a synchronization step so all replicas stay consistent.
The most common modern strategy for multi-process training is tf.distribute.MultiWorkerMirroredStrategy. It is a good fit when:
- each worker can hold the full model
- you want synchronous training
- you can tolerate the slowest worker setting the step time
TensorFlow also supports parameter-server style training for other workloads, but the simpler mental model for most teams is still synchronous data parallelism.
How the Workers Coordinate
Each process needs to know the cluster layout. TensorFlow typically gets that from the TF_CONFIG environment variable, which tells a process its role and the addresses of the other workers.
A minimal worker configuration might look like this on one machine:
The second worker runs the same training script with the same cluster block but a different task index. Once both processes are up, TensorFlow can build the distributed runtime and coordinate collective operations between them.
A Minimal Multi-Worker Training Script
The example below uses MultiWorkerMirroredStrategy with Keras. It creates a small model, distributes the dataset automatically, and runs fit in a way that can scale from one worker to several workers with minimal code changes.
The key detail is strategy.scope(). Variables created inside that scope become distributed variables, and the optimizer applies synchronized updates across workers.
TensorFlow handles the collective communication under the hood, but the performance still depends on your input pipeline, network quality, and how balanced the workers are.
What Really Runs in Parallel
There are two layers of parallelism here.
First, each worker is a separate process with its own Python runtime. That process drives local input reading, forward passes, and backward passes. Second, inside each worker, TensorFlow can still use device-level parallelism on CPUs, GPUs, or TPUs.
The distributed step usually looks like this:
- Each worker reads a different batch shard.
- Each worker computes gradients on its local replica.
- A collective operation combines gradients across workers.
- Every worker applies the same update.
That means the whole system behaves as one synchronized trainer, not as independent models racing ahead separately.
Common Pitfalls
The most common failure is mismatched TF_CONFIG values. If one worker has the wrong host, port, or index, the cluster may hang waiting for a process that never joins.
Another mistake is forgetting about input sharding. If every worker reads the same data in the same order, you waste compute and distort the effective batch behavior. Use tf.data carefully and verify that data is being partitioned the way you expect.
Side effects also need special handling. Checkpoint writing, file output, and metric publishing should usually happen only on the chief worker. If every worker writes to the same path, collisions and corrupted outputs are common.
Synchronous training is also sensitive to stragglers. One slow worker, overloaded machine, or unstable network link can slow the entire job because the faster workers wait at synchronization points.
Finally, do not assume distributed training automatically improves convergence. A larger effective batch size changes optimization behavior, so learning rate and scheduling often need retuning.
Summary
- Distributed TensorFlow uses multiple coordinated processes, not just multiple threads.
- '
tf.distribute.MultiWorkerMirroredStrategyis the standard choice for synchronous multi-worker data parallelism.' - Each worker runs the same model and synchronizes gradients with the others.
- '
TF_CONFIGtells every process its role in the cluster.' - Chief-only side effects and correct data sharding are essential for stable training.
- Faster training often requires tuning the input pipeline and optimizer, not only adding more workers.
Related reading
- Parallelization strategies for deep learning
- Passing trainingtrue when using Tensorflow 2's Keras Functional API
- Per pixel softmax for fully convolutional network
- Perform the validation loss from .caffemodel?
- Parallel threads with TensorFlow Dataset API and flat_map
- parallelising tf.data.Dataset.from_generator
- Parameter Tuning for Perceptron Learning Algorithm
- partitioning an float array into similar segments clustering
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.