Tensorboard
writer.flush()
data visualization
machine learning
logging

When to use writer.flush in Tensorboard

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

TensorBoard summary writers buffer data before they are written to disk. writer.flush() forces those buffered events out immediately, which is useful when you need TensorBoard to show the latest metrics right now instead of waiting for the writer's normal flush cycle.

You usually do not need to flush after every single scalar write. The practical use cases are debugging, important checkpoints such as epoch boundaries, and short-lived scripts that may end before automatic flushing happens.

Why Writers Buffer

Summary writers batch disk I/O for performance. If every metric write forced a filesystem sync, training loops would spend unnecessary time writing logs instead of doing model work.

A minimal TensorFlow example looks like this:

python
1import tensorflow as tf
2
3writer = tf.summary.create_file_writer("logs/train")
4
5for step in range(5):
6    with writer.as_default():
7        tf.summary.scalar("loss", 1.0 / (step + 1), step=step)

Those summaries have been created, but TensorBoard may not display them instantly because the writer may still be buffering the event file.

When to Flush Explicitly

Flush when immediate visibility matters. A common pattern is to flush at the end of an epoch or after a meaningful evaluation step:

python
1import tensorflow as tf
2
3writer = tf.summary.create_file_writer("logs/train")
4
5for epoch in range(3):
6    for step in range(100):
7        with writer.as_default():
8            tf.summary.scalar("accuracy", 0.8, step=epoch * 100 + step)
9
10    writer.flush()

That is usually a good tradeoff. The writer stays efficient during the inner loop, but TensorBoard still receives updates at useful checkpoints.

Another good time to flush is right after validation metrics are written, especially if you monitor training from another machine or terminal.

Flush Before Program Exit

If the script is short-lived, flushing before exit is a good habit:

python
1with writer.as_default():
2    tf.summary.scalar("final_loss", 0.12, step=999)
3
4writer.flush()
5writer.close()

Closing often implies a flush, but an explicit call makes the intent clear and reduces the chance of losing the last few events when debugging quick runs.

Do Not Overuse flush()

This pattern is usually too aggressive:

python
1for step in range(10000):
2    with writer.as_default():
3        tf.summary.scalar("loss", 0.1, step=step)
4    writer.flush()

It works, but it turns buffered logging into constant disk I/O. Unless every step must become visible immediately, interval flushing is the better default.

Common Situations Where flush() Helps

flush() is especially useful when:

  • you are checking whether TensorBoard is pointed at the correct log directory
  • you want to confirm summaries are being produced at all
  • you just finished an epoch or evaluation pass
  • the process may shut down unexpectedly or shortly after logging

In those situations, forcing the writer to push data to disk removes one layer of uncertainty.

flush() Is Not a Replacement for Good Logging Strategy

If a run feels invisible in TensorBoard, the first thing to check is usually how often you write summaries at all. Flushing helps already-written events appear faster, but it does not create missing metrics. Good logging frequency and deliberate flush timing work together.

Common Pitfalls

  • Calling flush() after every metric update and slowing training unnecessarily.
  • Assuming TensorBoard is broken when the writer simply has not flushed yet.
  • Forgetting to flush or close the writer in short scripts.
  • Treating flush() as a fix for wrong log directories or missing TensorBoard refreshes.
  • Logging very rarely and then blaming flush timing for missing charts.

Summary

  • 'writer.flush() forces buffered TensorBoard events to be written immediately.'
  • Use it at epoch boundaries, before exit, and during debugging.
  • Do not usually call it after every single summary write.
  • Writers buffer on purpose for performance.
  • Flush deliberately when visibility matters, not automatically after everything.

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.