Spring Batch ItemReader Interview Questions and Answers

Master Spring Batch ItemReader with interview questions covering FlatFileItemReader, JdbcCursorItemReader, JdbcPagingItemReader, JpaPagingItemReader, MultiResourceItemReader, custom readers, restartability, and production best practices.


Introduction

The ItemReader is the first component in the Spring Batch processing pipeline. Its responsibility is to read data from an input source and provide one item at a time to the batch framework.

Spring Batch supports reading data from various sources, including:

  • CSV Files
  • Excel Files
  • Databases
  • XML Files
  • JSON Files
  • REST APIs
  • Kafka
  • Custom Data Sources

A well-designed ItemReader ensures efficient memory usage, restartability, and high-performance processing for millions of records.


ItemReader Architecture

flowchart LR

CSVFile --> ItemReader

Database --> ItemReader

XML --> ItemReader

RESTAPI --> ItemReader

ItemReader --> ItemProcessor

ItemProcessor --> ItemWriter

Q1. What is ItemReader?

Answer

ItemReader<T> is a Spring Batch interface responsible for reading one item at a time.

Interface

public interface ItemReader<T> {

    T read() throws Exception;

}

The read() method

  • Returns one item
  • Returns null when no more data exists

Processing Flow

Read Item

↓

Process Item

↓

Write Item

↓

Read Next Item

Q2. How does ItemReader work?

The ItemReader repeatedly reads records until it returns null.

Execution Flow

flowchart TD

Start --> ReadItem

ReadItem --> Item

Item --> Processor

Processor --> Writer

Writer --> ReadItem

ReadItem --> End

Spring Batch automatically controls the reading loop.


Q3. What is FlatFileItemReader?

FlatFileItemReader reads text-based files such as CSV.

Example

@Bean
FlatFileItemReader<Customer> reader() {

    return new FlatFileItemReaderBuilder<Customer>()
            .name("customerReader")
            .resource(new FileSystemResource("customers.csv"))
            .delimited()
            .names("id","name","email")
            .targetType(Customer.class)
            .build();

}

Supported Formats

  • CSV
  • Pipe Delimited
  • Tab Delimited
  • Fixed Width

Q4. What is JdbcCursorItemReader?

JdbcCursorItemReader streams rows using a database cursor.

Example

@Bean
JdbcCursorItemReader<Customer> reader(
DataSource dataSource){

    JdbcCursorItemReader<Customer> reader =
            new JdbcCursorItemReader<>();

    reader.setDataSource(dataSource);

    reader.setSql(
            "SELECT * FROM CUSTOMER");

    return reader;

}

Advantages

  • Streams records
  • Low memory usage
  • Suitable for sequential processing

Q5. What is JdbcPagingItemReader?

JdbcPagingItemReader retrieves data page by page.

Example

reader.setPageSize(1000);

Paging Flow

flowchart LR

Database --> Page1

Page1 --> Page2

Page2 --> Page3

Page3 --> ItemProcessor

Advantages

  • Better scalability
  • Lower database locking
  • Ideal for millions of records

Q6. What is JpaPagingItemReader?

JpaPagingItemReader retrieves entities using JPA pagination.

Example

JpaPagingItemReader<Customer> reader =
        new JpaPagingItemReader<>();

reader.setPageSize(1000);

Benefits

  • Uses EntityManager
  • Supports JPA entities
  • Efficient pagination

Use when working with Hibernate/JPA applications.


Q7. What is MultiResourceItemReader?

It processes multiple files as a single input source.

Example

customers1.csv

customers2.csv

customers3.csv

Flow

flowchart LR

CSV1 --> MultiReader

CSV2 --> MultiReader

CSV3 --> MultiReader

MultiReader --> Processor

Useful for daily batch imports.


Q8. How does Restartability work with ItemReader?

Spring Batch stores the reader position in the ExecutionContext.

Example

Processed

650,000 Rows

↓

Failure

↓

Restart

↓

Continue

650,001

Restart Flow

flowchart TD

ItemReader --> ReadRows

ReadRows --> ExecutionContext

ExecutionContext --> Failure

Failure --> Restart

Restart --> ResumeReading

No already-processed rows are reread.


Q9. How do you create a Custom ItemReader?

Implement the ItemReader interface.

Example

public class ApiItemReader
implements ItemReader<Customer>{

    @Override
    public Customer read(){

        // Read from REST API

        return customer;

    }

}

Custom readers are useful for

  • REST APIs
  • Kafka
  • FTP Servers
  • Cloud Storage
  • Proprietary systems

Q10. ItemReader Best Practices

Use Paging Readers

Avoid loading millions of records into memory.


Keep Readers Stateless

Store restart state in the ExecutionContext.


Read Only Data

Business logic belongs in ItemProcessor.


Tune Fetch Size

Improve database performance.

Example

reader.setFetchSize(1000);

Banking Example

flowchart TD

TransactionCSV --> FlatFileItemReader

FlatFileItemReader --> ItemProcessor

ItemProcessor --> ItemWriter

ItemWriter --> BankingDatabase

ExecutionContext --> Restart

Common Interview Questions

  • What is ItemReader?
  • How does ItemReader work?
  • What is FlatFileItemReader?
  • JdbcCursorItemReader vs JdbcPagingItemReader?
  • What is JpaPagingItemReader?
  • What is MultiResourceItemReader?
  • How does restartability work?
  • How do you build a custom ItemReader?
  • Which ItemReader is best for large datasets?
  • ItemReader best practices?

Quick Revision

Topic Summary
ItemReader Reads one item at a time
FlatFileItemReader Reads CSV/Text files
JdbcCursorItemReader Streams database rows
JdbcPagingItemReader Reads paginated data
JpaPagingItemReader Reads JPA entities
MultiResourceItemReader Reads multiple files
Custom Reader Reads custom sources
ExecutionContext Stores checkpoint
Fetch Size Database optimization
Restartability Resume after failure

ItemReader Execution Lifecycle

sequenceDiagram
Step->>ItemReader: read()
ItemReader->>CSV/Database: Fetch Record
CSV/Database-->>ItemReader: Item
ItemReader-->>Step: Return Item
Step->>ItemProcessor: Process
ItemProcessor-->>Step: Processed Item
Step->>ItemReader: Read Next Item
ItemReader-->>Step: null (End of Data)

Production Example – Processing a 1 Million Row CSV

A banking application receives a 1 million-row transaction CSV every night.

Configuration:

  • FlatFileItemReader reads one record at a time.
  • Chunk size = 1000
  • Every 1000 records are committed.
  • The current file position is stored in the ExecutionContext.
  • If processing fails after 720,000 records, the reader resumes from record 720,001 after restart.
flowchart LR

CSVFile --> FlatFileItemReader

FlatFileItemReader --> Chunk1000

Chunk1000 --> ItemProcessor

ItemProcessor --> ItemWriter

ItemWriter --> PostgreSQL

ItemWriter --> ExecutionContext

This checkpoint mechanism enables Spring Batch to process extremely large files efficiently without reprocessing completed records.


Key Takeaways

  • ItemReader is responsible for reading one item at a time from the input source.
  • The read() method returns one item per call and returns null when processing is complete.
  • Spring Batch provides specialized readers such as FlatFileItemReader, JdbcCursorItemReader, JdbcPagingItemReader, JpaPagingItemReader, and MultiResourceItemReader.
  • JdbcPagingItemReader is generally preferred over JdbcCursorItemReader for very large datasets because it reduces long-running cursor usage and improves scalability.
  • ExecutionContext stores the reader's progress, enabling job restart without rereading completed records.
  • Custom ItemReaders can be implemented for REST APIs, Kafka, FTP, cloud storage, or proprietary systems.
  • Keep ItemReaders focused solely on reading data; transformation and validation belong in the ItemProcessor.
  • Proper reader selection and tuning are critical for high-performance enterprise batch processing.