TensorFlow
FileWriter
flush method
data logging
machine learning

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.

Practice ML system design

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.

python
1import tensorflow as tf
2
3writer = tf.summary.FileWriter("./logs")
4summary = tf.Summary(value=[tf.Summary.Value(tag="loss", simple_value=0.25)])
5
6writer.add_summary(summary, global_step=1)
7writer.flush()
8writer.close()

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.

python
1import tensorflow as tf
2
3writer = tf.summary.create_file_writer("./logs")
4
5with writer.as_default():
6    tf.summary.scalar("accuracy", 0.92, step=1)
7    writer.flush()

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 n steps
  • flush every n steps 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

python
1import tensorflow as tf
2
3writer = tf.summary.create_file_writer("./logs")
4
5for step in range(1, 101):
6    loss = 1.0 / step
7    with writer.as_default():
8        tf.summary.scalar("loss", loss, step=step)
9
10    if step % 10 == 0:
11        writer.flush()
12
13writer.close()

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, while close() finishes and releases it.'
  • Periodic flushing during training is usually the practical balance.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice ML system design

All Rights Reserved.