Skip to content

Java Backend Production Scenario Interview Guide

1. API Response Time Suddenly Increases (200 ms → 5 seconds)

Section titled “1. API Response Time Suddenly Increases (200 ms → 5 seconds)”

Never guess. Start with metrics.

flowchart TD
    C[Client] --> LB[Load Balancer]
    LB --> G[API Gateway]
    G --> S[Spring Boot Service]
    S --> DB[(Database)]
    S --> R[(Redis)]
    S --> M[Other Microservices]
  1. Check dashboards (Prometheus, Grafana, Datadog, New Relic)

    • CPU
    • Memory
    • Thread count
    • GC time
    • Database connections
    • Request rate
  2. Use distributed tracing (Zipkin, Jaeger, OpenTelemetry)

flowchart TD
    API --> UserService
    UserService --> OrderService
    OrderService --> PaymentService
  1. Check database:

    • Slow query log
    • EXPLAIN
    • Missing indexes
    • Lock waits
  2. Check downstream service latency.

  3. Capture thread dumps.

Terminal window
jstack <pid>

Look for blocked threads, deadlocks, and waiting threads.

  1. Review GC logs for Full GC pauses.
  • Database bottleneck
  • External API latency
  • Network issues
  • JVM/GC pauses
  • Thread pool exhaustion
  • Cache failures

Prevent cascading failures using:

flowchart LR
    Timeout --> Retry --> CircuitBreaker --> Fallback

Use Resilience4j:

@CircuitBreaker
@Retry
@TimeLimiter
@Bulkhead

Example usage with a fallback method:

@CircuitBreaker(name = "paymentService", fallbackMethod = "fallback")
public Payment getPayment() {
return paymentClient.getPayment();
}

3. Circuit Breaker vs Retry vs Timeout vs Bulkhead

Section titled “3. Circuit Breaker vs Retry vs Timeout vs Bulkhead”
Pattern Use When
Timeout Prevent waiting forever
Retry Temporary/transient failures
Circuit Breaker Service repeatedly fails
Bulkhead Prevent one dependency from exhausting all threads

Example:

  • Timeout: 2 seconds
  • Retry: 3 attempts
  • Circuit opens after 50% failures
  • Bulkhead: limit to 20 threads

  • Long-running transactions
  • Connection leaks
  • Slow SQL
  • Small connection pool
  • Deadlocks
  • Lock contention

Example:

@Transactional
public void process() {
// Avoid long-running transactions
}

Check HikariCP metrics:

  • Active connections
  • Idle connections
  • Waiting threads

Implement idempotency.

Request:

POST /pay
Idempotency-Key: abc123

If the same request arrives again, return the previously stored response.

flowchart TD
A[Receive Request] --> B{Already Processed?}
B -->|Yes| C[Return Existing Response]
B -->|Processing| D[Reject Duplicate]
B -->|No| E[Process Payment]
E --> F[Store Result]

Store the key in Redis or the database.

Structure:

Key: abc123
Status: SUCCESS
Response: PaymentId

Use SETNX (Redis) or a unique database constraint so only the first request succeeds.


The application should continue functioning.

Fallback:

Redis unavailable
Database

Performance may degrade, but availability should remain.


8. Cache Invalidation Across Multiple Instances

Section titled “8. Cache Invalidation Across Multiple Instances”
flowchart LR
    U[User Update] --> P[Redis Pub/Sub or Kafka Event]
    P --> A1[App Instance 1]
    P --> A2[App Instance 2]
    P --> A3[App Instance 3]

Possible solutions:

  • Redis Pub/Sub
  • Kafka events
  • Spring Cache
@CacheEvict

9. Works for 100 Users but Fails at 10,000

Section titled “9. Works for 100 Users but Fails at 10,000”

Check:

  • Connection pools
  • Thread pools
  • Database bottlenecks
  • CPU
  • Garbage collection
  • Rate limiting
  • Network
  • Load balancer

Most common bottlenecks:

  • Database
  • Connection pool
  • External APIs

10. Processing a 5 GB CSV Without OutOfMemoryError

Section titled “10. Processing a 5 GB CSV Without OutOfMemoryError”

Never load the entire file.

Avoid:

Files.readAllLines(path);

Instead:

BufferedReader reader = Files.newBufferedReader(path);
// or Spring Batch

Process in chunks:

  • Read 1000 rows
  • Process
  • Write
  • Clear memory

11. Synchronous vs Asynchronous Communication

Section titled “11. Synchronous vs Asynchronous Communication”

Use for:

  • Login
  • Payment validation
  • OTP verification

Use for:

  • Email
  • Notifications
  • Auditing
  • Analytics
  • Invoice generation

12. Kafka Consumer Crashes Before Offset Commit

Section titled “12. Kafka Consumer Crashes Before Offset Commit”

Scenario:

sequenceDiagram
    Kafka->>Consumer: Deliver message
    Consumer->>Consumer: Process successfully
    Consumer--XKafka: Crashes before commit
    Kafka->>Consumer: Redelivers message

Solution:

  • Design consumers to be idempotent.

Store processed event IDs in:

  • Redis
  • Database

If an event ID already exists, skip processing.


Commands:

Terminal window
kubectl describe pod <pod-name>
kubectl logs <pod-name> --previous

Check for:

  • OOMKilled
  • CrashLoopBackOff
  • Probe failures
  • Restart count
  • Kubernetes events

Possible causes:

  • Database locks
  • Network latency
  • Slow downstream service
  • Thread starvation
  • GC pauses
  • Blocking I/O

Use distributed tracing.

flowchart LR
    API --> Service --> Database
    Service --> ExternalAPI

Measure:

  • API latency
  • Database execution time
  • Network RTT
  • External service latency

Tools:

  • Zipkin
  • Jaeger
  • OpenTelemetry

Investigate:

  • Execution plans
  • Indexes
  • Statistics
  • Data volume
  • Locks
  • Parameter sniffing
  • Database version
  • Hardware differences

Avoid XA transactions.

Prefer:

  • Saga Pattern
  • Outbox Pattern

Each service commits locally and compensates on failure.


19. Saga Pattern and Compensation Failures

Section titled “19. Saga Pattern and Compensation Failures”
flowchart LR
    Order --> Inventory --> Payment --> Shipping

If payment fails:

  • Undo inventory reservation
  • Undo order creation

If compensation also fails:

  • Retry
  • Dead Letter Queue
  • Manual intervention

Two ways to implement a Saga:

  • Choreography — each service publishes events; the next service reacts, with no central coordinator.
  • Orchestration — a central orchestrator explicitly tells each service what to do next.
flowchart LR
    Order --> Payment --> Shipping
    Payment -->|Failure| Cancel["Compensating Transaction: Cancel Order"]

Popular Saga orchestration tools: Temporal, Camunda, Axon, Eventuate Tram.

Why Not Use One Transaction Across Multiple Microservices?

Section titled “Why Not Use One Transaction Across Multiple Microservices?”

Reasons:

  • Independent databases per service
  • Two-Phase Commit (2PC) introduces latency and tight coupling
  • Reduced scalability and availability

Preferred approach: Saga pattern + Kafka/RabbitMQ + eventual consistency, instead of distributed ACID transactions.


20. Duplicate Requests Due to Network Retries

Section titled “20. Duplicate Requests Due to Network Retries”

Use:

  • Idempotency keys
  • Unique business keys
  • Database unique constraints

Return the same response for duplicate requests.


Strategies:

  • Rolling update
  • Blue-Green deployment
  • Canary deployment

Kubernetes:

maxUnavailable: 0
maxSurge: 1

Use readiness probes before routing traffic.


During pod termination:

SIGTERM

Configure:

  • Finish in-flight requests
  • Reject new requests
  • terminationGracePeriodSeconds
  • Readiness probe removal before shutdown

Algorithms:

  • Token Bucket
  • Leaky Bucket
  • Sliding Window

Store counters in Redis.

Reject excess requests with:

HTTP/1.1 429 Too Many Requests

Concrete implementation with Bucket4j:

<dependency>
<groupId>com.bucket4j</groupId>
<artifactId>bucket4j-core</artifactId>
</dependency>
Bucket bucket = Bucket4j.builder()
.addLimit(Bandwidth.simple(10, Duration.ofMinutes(1)))
.build();

Or at the gateway layer with Spring Cloud Gateway:

filters:
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 10
redis-rate-limiter.burstCapacity: 20

For a full HLD/LLD treatment (algorithm comparison, distributed design, custom Token Bucket implementation, edge cases), see the Rate Limiter System Design case study.


Monitor:

  • Heap usage
  • Heap dumps
  • GC logs

Tools:

  • Eclipse MAT
  • VisualVM
  • YourKit

Look for:

  • Large collections
  • ThreadLocal leaks
  • Static references
  • Cache growth
  • Listener leaks

25. Production Issue Only Under Heavy Traffic

Section titled “25. Production Issue Only Under Heavy Traffic”

Approach:

  1. Metrics
  2. Logs
  3. Distributed tracing
  4. Thread dump
  5. Heap dump
  6. GC logs
  7. Database metrics
  8. Load testing

Tools:

  • JMeter
  • Gatling
  • Prometheus
  • Grafana
  • OpenTelemetry
  • Zipkin
  • Jaeger

Reproduce using production-like traffic rather than local environments.


A senior engineer should:

  1. Observe first (metrics, logs, traces)
  2. Isolate the bottleneck
  3. Mitigate quickly (timeouts, circuit breakers, scaling)
  4. Fix the root cause
  5. Prevent recurrence (monitoring, alerts, automation)
  • Spring Boot production tuning
  • Kafka partitioning, ordering, retries, DLQs
  • Redis caching patterns
  • Kubernetes probes and autoscaling
  • Observability
  • Resilience4j
  • Concurrency control
  • Database optimization
  • Always diagnose production issues using metrics, logs, and tracing before making changes.
  • Build resilient microservices with timeouts, retries, circuit breakers, bulkheads, and idempotency.
  • Design for scalability through efficient resource management, streaming, caching, and rate limiting.
  • Prefer observability and automation to quickly identify and resolve production issues.
  • Demonstrate a structured troubleshooting approach in interviews to reflect real-world production experience.