Video in progress

Several servers exchange files through a shared NFS directory. Where does Java IO code break in such an environment, and what do you propose?

NFS doesn't fully provide local disk semantics: attributes are cached, rename and locks work with delays and errors, WatchService doesn't see changes made on another machine, and IO hangs on an outage. Instead of file-based integration, I propose a queue or object storage.

📖 Answer

Assumptions that held on a local disk break one by one on NFS:

  • Caching. The client caches file attributes (size, mtime) for a few seconds. Logic like "the file size didn't change, so the write is done" gives wrong results. You may also not see data written by another server right away (close-to-open consistency: the guarantee holds only when you open after the writer has closed).
  • Locks. On NFSv3 locking goes through a separate protocol (NLM); if a client or server restarts, locks can be lost or get "stuck". "Only one server processes this" logic built on FileLock can end up running two servers at once.
  • Events. inotify only sees local changes.
  • Hangs. With a "hard" mount, if the server doesn't respond, read waits forever. The Java thread hangs in native code in the RUNNABLE state and can't be interrupted. If all the threads in a pool hang like that, the service is dead.
  • Uniqueness. CREATE_NEW may not be atomic on old NFS versions.

If you can't give up NFS, the minimal discipline: the writer writes under a .tmp name and does a rename at the end; the reader takes only the final names; each file is "claimed": the reader renames it to its own name, and whoever succeeds processes it; and the NFS IO runs in a separate bounded pool, off the main request path.

But as an architect I usually propose a different path: files go to object storage, and the "file is ready" event goes to a queue (Kafka, RabbitMQ, SQS) or the database. Each file has an explicit state (RECEIVED → PROCESSING → DONE) stored in the database. Then who is processing what, what failed and what needs to be resent are all visible with a query, instead of being guessed from file names in a directory.