1. What is Spring Cloud and how does it support microservice architecture?
Answer:
Spring Cloud provides tools for building distributed systems with common patterns like service discovery, configuration management, circuit breakers, intelligent routing, distributed tracing, and load balancing. It builds on top of Spring Boot and integrates with Netflix OSS components and other cloud-native tools.
2. What is Spring Cloud Config Server? How does it work?
Answer:
Spring Cloud Config Server centralizes external configuration for distributed systems. It stores configuration in a Git, SVN, or native file system and serves it to clients over REST endpoints.
-
Clients use
spring-cloud-starter-config
. -
Server reads values from Git or filesystem.
-
Clients access config via
/application/profile
endpoints.
3. How do you secure Spring Cloud Config Server?
Answer:
-
Use Spring Security with OAuth2 or Basic Auth.
-
Encrypt sensitive properties using
@EnableConfigServer
+spring-cloud-starter-security
. -
Use the
/encrypt
and/decrypt
endpoints or integrate with HashiCorp Vault.
4. What is Netflix Eureka? How does service discovery work?
Answer:
Netflix Eureka is a service registry where services register themselves and discover other services.
-
Service Registration: Each service registers with Eureka Server at startup.
-
Service Discovery: Services use the Eureka client to find other services by name.
-
Health Check: Eureka monitors availability using heartbeats.
5. What is Spring Cloud Gateway and how does it compare to Zuul?
Answer:
Spring Cloud Gateway is the replacement for Netflix Zuul 1.x and offers non-blocking, reactive routing using Spring WebFlux.
Gateway features:
-
Path-based routing
-
Filters (pre/post)
-
Rate limiting
-
Load balancing
-
Integration with OAuth2, Circuit Breaker, etc.
Zuul 1 is servlet-based and synchronous, while Spring Cloud Gateway is fully asynchronous and performant.
6. What is Hystrix and how is it used for fault tolerance?
Answer:
Netflix Hystrix is a circuit breaker library that prevents cascading failures in microservices.
-
Wrap remote calls with
@HystrixCommand
. -
If the downstream system fails, fallback logic is executed.
-
Circuit opens if failure threshold is crossed.
🔍 Note: Hystrix is now in maintenance mode. Use Resilience4j as the preferred alternative.
7. What is Ribbon and how does load balancing work in Spring Cloud?
Answer:
Netflix Ribbon is a client-side load balancer.
-
Ribbon picks a service instance from Eureka and uses a round-robin or weighted algorithm.
-
It is integrated with RestTemplate and Feign clients.
🎯 Spring Cloud 2020+ replaces Ribbon with Spring Cloud LoadBalancer.
8. How does distributed tracing work with Spring Cloud Sleuth and Zipkin?
Answer:
-
Spring Cloud Sleuth adds trace and span IDs to log entries.
-
Zipkin collects and visualizes traces for debugging latency issues.
Each request propagates trace IDs across services to correlate logs and understand request flow.
9. How do you implement centralized logging in Spring Cloud?
Answer:
-
Use ELK Stack (Elasticsearch, Logstash, Kibana) or EFK (Fluentd).
-
Include correlation IDs from Sleuth in logs.
-
Forward logs via Filebeat/FluentBit to Elasticsearch.
10. What are common Spring Cloud patterns and components for microservices?
Answer:
-
Service Discovery: Eureka, Consul
-
API Gateway: Spring Cloud Gateway
-
Config Management: Spring Cloud Config
-
Load Balancer: Ribbon / Spring Cloud LoadBalancer
-
Circuit Breaker: Hystrix / Resilience4j
-
Tracing: Sleuth + Zipkin
-
Messaging: Kafka, RabbitMQ, Stream
✅ Spring Batch & Scheduling – Interview Questions & Answers
🔁 Spring Batch
1. What is Spring Batch and when should you use it?
Answer:
Spring Batch is a lightweight framework for batch processing large volumes of data in a robust, transactional manner. Use it for:
-
ETL (Extract-Transform-Load) jobs
-
Report generation
-
Data migration
-
Scheduled processing
2. What are the key components of Spring Batch?
Answer:
-
Job
: Encapsulates an entire batch process. -
Step
: Represents a phase in the job (read/process/write). -
ItemReader
: Reads data from a source. -
ItemProcessor
: Applies business logic. -
ItemWriter
: Writes processed data to a destination. -
JobLauncher
: Launches jobs programmatically. -
JobRepository
: Stores metadata and execution history.
3. How do you handle job restartability and fault tolerance?
Answer:
-
Enable
restartable="true"
on steps. -
Use
skip()
andretry()
configurations. -
Configure checkpoints to allow job resumption.
-
Maintain
JobExecution
state inJobRepository
.
4. What is chunk-oriented processing in Spring Batch?
Answer:
Chunk processing reads, processes, and writes a fixed number of records (chunk size) in a transaction.
Example: read 100 records → process → write → commit.
5. What are Job Parameters and why are they important?
Answer:
JobParameters
are used to uniquely identify job executions and pass external data. Jobs with the same parameters won't run again unless marked restartable
.
6. How do you design parallel and partitioned Spring Batch jobs?
Answer:
-
Parallel Steps: Use
Flow
+split()
to run steps concurrently. -
Partitioning: Divide data using a
Partitioner
, and assign to worker steps (e.g., multi-thread or remote).
7. How do you trigger batch jobs manually and via API?
Answer:
-
Manually via
JobLauncher.run(job, jobParameters)
-
Expose a REST endpoint that accepts parameters and launches the job programmatically.
8. How do you monitor and audit Spring Batch jobs?
Answer:
-
Enable Spring Batch metadata tables.
-
Expose job status via REST.
-
Log
JobExecution
,StepExecution
details. -
Integrate with Prometheus + Grafana or use
JobExplorer
.
⏰ Spring Scheduling
9. How does Spring’s @Scheduled
annotation work?
Answer:
Schedules methods to run at fixed intervals or cron expressions. Enable with @EnableScheduling
.
10. What are different scheduling modes supported by Spring?
Answer:
-
fixedRate
: Run at a fixed interval regardless of method duration. -
fixedDelay
: Run after the method completes and waits a fixed delay. -
cron
: CRON expression-based scheduling.
11. How to prevent overlapping scheduled executions?
Answer:
-
Use
@SchedulerLock
from ShedLock library to prevent concurrent runs. -
Persist lock in a DB, Redis, etc.
-
Or synchronize manually using a flag or DB lock.
12. How to handle errors in scheduled tasks?
Answer:
-
Wrap logic in try/catch blocks.
-
Use async error logging or alerting tools.
-
Integrate with monitoring tools (like Prometheus).
13. Can you schedule jobs in a clustered environment?
Answer:
Yes, but you must ensure single execution per cluster:
-
Use Quartz Scheduler with JDBC job store.
-
Or use ShedLock with a distributed lock provider (e.g., PostgreSQL, Redis).
14. What’s the difference between Spring Batch and Quartz Scheduler?
Answer:
Feature | Spring Batch | Quartz Scheduler |
---|---|---|
Purpose | Batch processing (ETL) | Job scheduling |
Persistence | Built-in via JobRepository | JDBC, RAMJobStore |
Flow | Chunked, complex pipelines | Trigger-based jobs |
Integration | Native in Spring | Needs explicit integration |
15. How to integrate Spring Batch with Quartz?
Answer:
-
Schedule a Quartz job.
-
Inside Quartz job’s
execute
method, launch a Spring BatchJobLauncher
.
No comments:
Post a Comment