Sunday, 24 August 2025

Mock Interview Preparation - Microservices in Spring Boot

I: Can you explain how you implemented microservices in your last project?

C: Sure. In my last project, I worked on a Digital Locker / Document Vault System where we followed a microservices architecture. 
We had separate services like User Service, Document Service, and Notification Service. 
- Each service had its own database to ensure loose coupling. 
- Services communicated using REST APIs for synchronous calls and Kafka for asynchronous, event-driven scenarios. 
- We used Spring Boot + Spring Cloud for building microservices, Eureka for service discovery, and Spring Cloud Gateway as the API Gateway. 
- Security was handled via OAuth2 with JWT tokens. 
- For deployment, we containerised each service with Docker and deployed them on Kubernetes (OpenShift). 
- To handle resilience, we used Resilience4j (circuit breakers, retries, fallbacks). 
This architecture gave us scalability, independent deployments, and fault isolation. 


🔁 Follow-ups

I: Why did you choose microservices instead of a monolith? 
C: In a monolith, even a small change requires redeploying the whole application. Scaling was also harder since everything scaled together. With microservices, we gained independent deployments, better scalability, fault isolation, and tech flexibility. 

I: Why Kafka when REST APIs were already there? 
C: REST works fine for synchronous calls, but for event-driven features like sending notifications after a document upload, Kafka allows asynchronous communication. If the notification Service was down, messages stayed in Kafka and were processed later. 

I: How did you manage databases across services? 
C: Each service had its own database. This ensured loose coupling and autonomy. For example, User Service used PostgreSQL and Document Service used MongoDB. If we used a single DB, services would get tightly coupled. 

I: How did you secure inter-service communication? 
C: We used OAuth2 with JWT tokens. Auth Service generated JWTs after login. Each request carried the JWT, and microservices validated it. This ensured stateless, centralised authentication. 

I: What challenges did you face? 
C: 
- Distributed logging → solved with ELK stack. 
- Service failures → handled with Resilience4j circuit breakers & retries. 
- Database consistency → used the saga pattern for long transactions. 
- Network latency from multiple calls → optimised with caching + async messaging. 

I: Why Eureka and API Gateway? 

C: 
- Eureka (Service Discovery): avoided hardcoding URLs, services registered dynamically. 
- API Gateway: acted as a single entry point, managed routing, authentication, and rate limiting. 

I: How did you deploy and manage microservices? 
C: Each service had its own Jenkins pipeline. We used Docker for containerization and deployed on Kubernetes (OpenShift). This gave us scalability, rolling updates, and independent deployments. 


No comments:

Post a Comment