Quick revision
The short answer
I would poll SFTP every minute with an inbound adapter whose filter is a persistent accept-once filter backed by a shared metadata store, such as JDBC or Redis, so two instances never take the same file; alternatively the poller runs only on a leader elected through a lock registry. Each file is split into records, validated, and routed: valid records to a Kafka outbound adapter, invalid ones to a JDBC dead-letter table. A per-flow error channel records failures with file name and line number, Kafka keys built from file name and line make writes idempotent, processed files are moved, and Micrometer metrics show what happens.
What to remember
In this answer, we designed a Spring Integration flow that polls SFTP every minute with a persistent accept-once filter on a shared metadata store, optionally on an elected leader, so two instances do not process the same file. The flow splits files into records, validates them and routes valid ones to Kafka and invalid ones to a dead-letter table, sends failures to a per-flow error channel with file and line, keeps writes idempotent with file and line keys, moves processed files and publishes Micrometer metrics.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would poll SFTP every minute with an inbound adapter whose filter is a persistent accept-once filter backed by a shared metadata store, such as JDBC or Redis, so two instances never take the same file; alternatively the poller runs only on a leader elected through a lock registry. Each file is split into records, validated, and routed: valid records to a Kafka outbound adapter, invalid ones to a JDBC dead-letter table. A per-flow error channel records failures with file name and line number, Kafka keys built from file name and line make writes idempotent, processed files are moved, and Micrometer metrics show what happens.
Why this question is asked
Partner file feeds look simple until they run in production with more than one instance. Two pods both see the same new file and both process it, or a crash in the middle of a file leaves half the orders sent and half missing. Interviewers ask this design question to see whether we know the Spring Integration building blocks that solve these problems and how to reason about duplicates and failures. In this answer, we will lay out the flow, solve the two-instance problem in two ways, look at splitting, validation and routing, design error handling, and finish with idempotency, file housekeeping and metrics.
1The Shape of the Flow
Spring Integration lets us describe this pipeline as one IntegrationFlow: an inbound SFTP adapter that polls every minute, a splitter that turns each file into one message per record, a parsing and validation step, and a router with two outbound paths. Messages carry headers along the way, including the file name set by the adapter and a sequence number set by the splitter, which is the line number when markers are turned off. Those two headers are what we will use for error reports and idempotency.
- Poll SFTP every minutepersistent accept-once filter
- Split file into recordsheaders: file name and line number
- Parse and validate each recordresult: valid or invalid with reason
- Record valid?YesKafka outbound adapter, key file:lineNoJDBC dead-letter table
- Mark file done and move itprocessed directory on SFTP
- Errors to the flow's error channelfile name, line, exception
6 steps in order. Play them, or step through them one at a time.
2Not Processing a File Twice With Two Instances
The usual inbound SFTP adapter synchronises remote files to a local directory and then emits each local file as a message. Its remote filter decides which remote files to take. The default filters remember files in memory, which is per instance, so two instances would both take every file. The fix is a persistent accept-once filter, SftpPersistentAcceptOnceFileListFilter, backed by a shared ConcurrentMetadataStore such as JdbcMetadataStore or RedisMetadataStore.
The filter records each file name and modification time with an atomic putIfAbsent. When two instances list the same new file at the same moment, only one insert succeeds, and only that instance downloads the file. A similar persistent filter on the local directory protects against reprocessing local copies after a restart.
The second option is leadership. A LockRegistryLeaderInitiator with a JdbcLockRegistry or RedisLockRegistry elects one instance as leader for a role, and the poller endpoint is marked with that role and not started automatically. Only the leader polls; if it dies, its lock expires and another instance takes over. This is simpler to reason about, but it puts all file work on one instance. I would combine both: a leader for polling, and the persistent filter as a safety net.
3Splitting, Validating and Routing Records
Files.splitter() turns the file into one message per line and copies the file headers onto each one. A transformer parses the line into a small result object that holds either the order or the reason it is invalid, for example a missing partner reference or a negative quantity. Putting the validation result in the payload, rather than throwing an exception, keeps bad data in the normal flow, because a bad record is an expected case, not a failure.
The router then reads the valid flag and sends each record down one of two sub-flows. The valid path uses a Kafka outbound adapter on a KafkaTemplate. The invalid path uses a JDBC outbound handler that inserts the raw line, the reason, the file name and the line number into a dead-letter table that the partner support team can query.
4Error Handling and the Bad File Decision
Invalid records are business data, but real failures still happen: Kafka is down, the database rejects an insert, or the file is not CSV at all. Because the flow starts from a poller, these exceptions go to the error channel set on the poller, here ordersErrors. A small flow on that channel logs and stores the failure with the file name and line number from the failed message's headers, and raises an alert if the rate is high.
We must also decide, with the business, what one bad record means. If each record is independent, a bad record goes to the dead-letter table and the rest of the file continues, which is the design above. If the file is a unit, for example a batch that must be accepted or rejected as a whole, we validate the whole file first and only then send records, so a bad file sends nothing and is moved to a rejected directory.
5Idempotency, File Housekeeping and Metrics
Even with the filter, records can be sent twice, for example if the instance crashes after sending half a file and the file is reprocessed. So the writes must be idempotent, meaning repeating them causes no harm. The Kafka record key, file name plus line number, identifies each record uniquely, so consumers can ignore keys they have already processed, and producer idempotence prevents duplicates from the producer's own retries. The dead-letter table has a unique constraint on file name and line number, and inserts ignore duplicates.
After the last record, the flow moves or renames the remote file into a processed directory, or the partner's agreed archive location, so the inbound directory only holds new work. Finally, Spring Integration publishes Micrometer metrics for channels and handlers when a MeterRegistry is present, which Boot provides. I would add counters for valid and invalid records per partner.
6Follow-up questions
How would you resume a half-processed file after a crash?
First, make sure it is retried at all: a persistent accept-once filter already stored the file as seen, so after a crash it would be skipped. I would keep a small file status table, written when processing starts and when it ends, and on startup reprocess files still marked in progress, removing their entries from the metadata store or reading the local copy again. Then either reprocess the whole file, which is safe because Kafka keys and the dead-letter unique constraint make repeated writes harmless, or keep a checkpoint of the last line committed and skip lines up to it. For typical file sizes, reprocessing the whole file with idempotent writes is simpler and less error-prone.
Why not let each instance poll and rely only on idempotent writes?
It works for correctness, but it doubles the SFTP traffic and the processing, and the dead-letter table and Kafka consumers see twice the duplicates. The shared filter is cheap, so I would use it and keep idempotency as the safety net.
7Conclusion
In this answer, we designed a Spring Integration flow that polls SFTP every minute with a persistent accept-once filter on a shared metadata store, optionally on an elected leader, so two instances do not process the same file. The flow splits files into records, validates them and routes valid ones to Kafka and invalid ones to a dead-letter table, sends failures to a per-flow error channel with file and line, keeps writes idempotent with file and line keys, moves processed files and publishes Micrometer metrics.
Open with
I would build one IntegrationFlow. It starts with an SFTP inbound adapter polling every minute, and the important part is the filter: an SftpPersistentAcceptOnceFileListFilter backed by a shared ConcurrentMetadataStore, JDBC or Redis, which uses an atomic putIfAbsent, so when two instances see the same file only one takes it.
Then, if they want more
I would usually also run the poller only on a leader, using a LockRegistryLeaderInitiator with a JDBC or Redis lock registry, and keep the filter as a safety net. Then Files.splitter turns the file into one message per line, with the file name and line number in the headers.
A transformer parses and validates each line into a result object, and a router sends valid records to a Kafka outbound adapter and invalid ones to a JDBC handler that writes a dead-letter table with the raw line and the reason. Real failures go to an error channel set on the poller, where I record file name, line and exception.
I agree with the business whether one bad record rejects the whole file. Writes are idempotent: the Kafka key is file name plus line, and the dead-letter table has a unique constraint. Processed files are moved to an archive directory, and Micrometer metrics count files and records per partner.
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.
- Official documentation
- Spring Boot reference
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 "Design a Spring Integration flow that picks up partner order files from SFTP every...": https://scrivio-interview-library.netlify.app/spring-boot/spring-projects/2922d4233e/