When do I have to use TensorFlow's FileWriter.flush method?
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
FileWriter.flush() exists because summary data is buffered before it reaches the event file on disk. You need it when you want TensorBoard or another reader to see the latest summaries immediately, or when you want to reduce the risk of losing recently written events before the program exits.
What flush() Actually Does
Writing summary data on every single call would be slow, so the writer batches work in memory. flush() tells the writer to push buffered events to the filesystem now instead of waiting for the normal internal schedule.
That means flush() is about visibility and durability, not about changing the summary values themselves.
Typical Use Cases
You usually call flush() in one of these situations:
- during long-running training when you want TensorBoard to show recent metrics sooner
- before a program exits or a process may terminate unexpectedly
- after important milestones such as evaluation checkpoints
- in notebook or debugging workflows where immediate feedback matters more than I/O efficiency
If none of those apply, the writer can often manage its own buffering without manual flushing on every step.
TensorFlow 1 Style Example
The legacy tf.summary.FileWriter API often appears in TensorFlow 1 style code.
Here, flush() ensures the summary becomes visible in the event file immediately instead of waiting for a later implicit flush.
TensorFlow 2 Style Writer
In TensorFlow 2, the API changed, but the same idea still exists.
The modern API is different, but flushing still means "write buffered summaries now."
When You Usually Do Not Need It
Calling flush() after every summary step is rarely necessary. Excessive flushing can slow training because it forces more frequent disk writes.
A common middle ground is:
- write summaries every
nsteps - flush every
nsteps or at the end of each epoch - always close the writer cleanly when training finishes
This gives near-real-time monitoring without turning the event file into a performance bottleneck.
Difference Between flush() And close()
close() usually performs a final flush and then releases the writer resources. flush() writes buffered data but keeps the writer usable.
That means:
- use
flush()when training continues - use
close()when you are done with the writer
If the program exits abruptly without either, the most recent buffered summaries may never reach disk.
Practical Training Loop Pattern
This is a reasonable compromise for many training jobs: summaries are written often enough for monitoring, and flushing happens periodically instead of constantly.
Common Pitfalls
The most common mistake is flushing on every single batch by habit. That usually adds I/O overhead without meaningful benefit.
Another mistake is never flushing or closing the writer during long jobs and then wondering why TensorBoard looks stale. The summaries may still be buffered in memory.
A third issue is mixing old and new summary APIs without checking which writer object is actually in use. The concept is the same, but the TensorFlow 1 and TensorFlow 2 interfaces are different.
Summary
- '
flush()forces buffered summary events to be written to disk immediately.' - Use it when you need up-to-date TensorBoard output or safer persistence during long runs.
- Do not call it after every single summary unless you truly need immediate visibility.
- '
flush()keeps the writer open, whileclose()finishes and releases it.' - Periodic flushing during training is usually the practical balance.
Related reading
- When global_variables_initializer is actually required
- When I try to train tensorflow's object detection api I get CUDA_ERROR_ILLEGAL_INSTRUCTION
- When importing tensorflow, I get the following error No module named ''numpy.core._multiarray_umath''
- When to use tensorflow datasets api versus pandas or numpy
- When does dataloader shuffle happen for Pytorch?
- When should I use Azure ML Notebooks VS Azure Databricks? Both are competitor products in my opinion
- When do you exactly use consensus algorithm in distributed system?
- When does Docker image cache invalidation occur?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the 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.