Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

A WatchService-based importer works on Linux but misses or delays files on macOS and on a network-mounted volume. Why, and what would you use instead?

WatchService uses the operating system's native change notifications where the JDK has an implementation, such as inotify on Linux, but on other platforms, macOS among them in many JDK versions, it falls back to polling the directory every few seconds, which delays events. On network-mounted volumes the notifications are generated by the local kernel, which never sees changes made by other machines, so events are missed entirely. For a reliable importer we poll the directory on a schedule or, better, have the producer send a message, and if we keep the watcher we pair it with a periodic full scan to catch anything it misses.

Why this question is asked

An importer that works perfectly on a developer's Linux box and then misses files in another environment is a typical portability trap. WatchService looks like a platform-independent API, but its behaviour depends heavily on the operating system and the file system underneath. Interviewers ask this scenario to see whether we know where notifications come from and whether we design for reliability rather than for the happy path. In this answer, we will recap how WatchService gets its events, explain the macOS delay and the network-mount misses, and then design an importer that does not depend on notifications to be correct.

1A Quick Recap: Where Change Events Come From

WatchService is an interface, and the JDK picks an implementation per platform. On Linux it uses inotify, a kernel feature that reports changes to watched directories almost immediately. On Windows it uses ReadDirectoryChangesW, the native change notification API. Where the JDK has no native implementation for a platform, it uses a polling implementation instead: a background thread lists each registered directory periodically, compares the result with the previous listing, and creates events for the differences.

In both cases, the events reflect what the local machine can observe. Native notifications are produced by the local kernel when a local process changes the file system through it. That detail explains both of our problems.

How WatchService gets events
EnvironmentMechanismTypical behaviour
Linux, local diskinotify (native)events within milliseconds
Windows, local diskReadDirectoryChangesW (native)events within milliseconds
macOS (many JDK versions)polling fallbackevents after seconds; check your JDK
Network mount (NFS, SMB)local notifications onlychanges from other machines often missed

2Why Files Are Delayed on macOS

For many JDK versions, there has been no native WatchService on macOS, so the JDK uses the polling implementation. It scans each registered directory every few seconds, so events arrive with a delay of up to the polling interval, and changes that happen and reverse between two scans, such as a temporary file created and deleted, are never seen at all. In a large directory each scan also costs a full listing. JDK internals let the sensitivity be raised with a non-standard modifier, but that only shortens the delay and increases the cost. Native support for macOS has been worked on in recent JDK releases, so check the version you run rather than assume either way.

For the importer, a delay of a few seconds may be harmless. The real lesson is that the code's latency and completeness depend on the platform, so tests on Linux say little about behaviour elsewhere.

3Why Files Are Missed on a Network Volume

On an NFS or SMB mount, the files are stored on a file server and changed by many clients. When another machine writes a file into the shared directory, the change happens on the server; our machine's kernel is not involved, so inotify has nothing to report. Some file systems and clients pass a few notifications through, and SMB has its own change notify mechanism that may or may not be wired up, but in general Linux inotify does not report changes made by other NFS clients. Our watcher only sees changes made from our own machine, which in production is usually none of them.

Client caching makes it worse: even a directory listing on our side may show slightly stale content for a while, depending on the mount's attribute cache settings. So on network storage, WatchService is not a reliable signal at all.

4What to Use Instead: Polling or a Message From the Producer

The most reliable design does not watch the file system. When the producer finishes writing a file, it sends a message to a queue or calls an endpoint, saying which file is ready. The message is an explicit, durable signal, it arrives only after the file is complete, it can carry metadata such as a checksum, and it works the same whatever the storage. If the files live in object storage, the storage service can publish these events itself.

When we cannot change the producer, scheduled polling is the honest choice. Every 30 seconds, say, we list the directory, pick up files that match the naming convention and are complete, process them, and move each to a processed folder so it is not picked up twice. Polling is simple, it works on every file system, and its delay is bounded and known.

Java
@Scheduled(fixedDelay = 30_000)
void pollInbox() throws IOException {
try (Stream<Path> files = Files.list(inbox)) {
for (Path f : files.filter(p -> p.toString().endsWith(".csv")).toList()) {
if (isComplete(f)) { // marker file, or stable size and mtime
process(f);
Files.move(f, done.resolve(f.getFileName()), StandardCopyOption.ATOMIC_MOVE);
}
}
}
}

5Combining a Watcher With a Full Scan

If low latency matters on platforms where native events work, we can keep the WatchService as a fast path and add a periodic full scan as a safety net. The watcher handles most files within milliseconds; the scan, every minute or so, catches anything the watcher missed, whether from an OVERFLOW, a polling gap, or a network mount. Because the same file can be found by both, processing must be idempotent, which the move to a processed folder already gives us, or we can record handled file names with a checksum.

Step throughReliable import with two paths
  1. WatchService eventfast path, may miss files
  2. Scheduled full scansafety net, every minute
  3. File complete and not yet handled?
    Yesprocess it, then move it to processed/Noskip it

3 steps in order. Play them, or step through them one at a time.

Notice that correctness now comes from the scan, and the watcher only improves latency. That is the right split: notifications are a hint, the directory contents are the truth.

6Follow-up questions

Would increasing the inotify limits help on the network mount?

No. The inotify limits, such as max_user_watches and the event queue size, matter when we watch many directories or receive bursts of events on local storage. On a network mount the problem is that changes from other machines never generate local events at all, so no limit change helps.

Is there a library that watches files more reliably?

Some libraries wrap native APIs such as macOS FSEvents to get better local behaviour on macOS. They help with the delay on local disks, but none of them can see changes on a network share that the operating system itself does not report. For shared storage, polling or producer messages remain the reliable options.

7Conclusion

In this answer, we saw that WatchService relies on native notifications like inotify on Linux, falls back to polling on platforms without a native implementation, which delays events on macOS in many JDK versions, and misses changes on network volumes because they never pass through the local kernel. A reliable importer uses a message from the producer or scheduled polling, and if it keeps a watcher for speed, it adds a periodic full scan and idempotent processing so that correctness never depends on notifications.

What to say in the interviewAbout 100 seconds aloud

Open with

WatchService is only as good as the platform's notifications. On Linux the JDK uses inotify, which reports changes almost instantly, and on Windows it uses the native change API.

Then, if they want more

Where the JDK has no native implementation, which has been the case on macOS for many versions, it falls back to polling, listing the directory every few seconds, so events are delayed and short-lived changes can be missed entirely; I would check the JDK version since that has been worked on. The network volume is a different and worse problem.

The notifications come from the local kernel, and when another machine writes a file to an NFS or SMB share the change happens on the server, so our kernel never sees it and no event is produced. Caching can also make listings stale. So for a reliable importer I would not depend on WatchService.

The best option is a message from the producer when a file is complete, through a queue or an API call, or object storage events if the files live there. If I cannot change the producer, I would poll the directory on a schedule, process complete files, and move them to a processed folder.

If we need low latency where native events work, I would keep the watcher as a fast path but add a periodic full scan as the safety net, and make processing idempotent, because then correctness comes from the scan and the watcher only makes it faster.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A WatchService-based importer works on Linux but misses or delays files on macOS and on...": https://scrivio-interview-library.netlify.app/java/io-and-nio/0d2c2bc796/