1. What are Microservices?
Microservices are an architectural style where a large application is divided into multiple small, independent services.
Each service:
- Handles one specific business function
- Runs independently
- Has its own database (often)
- Communicates through APIs
Example
In an e-commerce application:
| Service | Responsibility |
|---|---|
| User Service | Authentication & profiles |
| Product Service | Product listing |
| Cart Service | Shopping cart |
| Order Service | Orders |
| Payment Service | Payments |
| Notification Service | Emails/SMS |
Each service can be developed and deployed separately.
2. What is the main idea behind Microservices?
The main goal is:
Break a large monolithic application into small independently deployable services.
Why?
Because large applications become:
- Hard to maintain
- Difficult to scale
- Slow to deploy
- Risky to update
Microservices solve this by allowing teams to work independently.
Real Example
Netflix uses microservices:
- Recommendation service
- Streaming service
- Billing service
- User profile service
If recommendation service fails, streaming still works.
3. Difference Between Monolithic and Microservices Architecture
| Feature | Monolithic | Microservices |
|---|---|---|
| Codebase | Single codebase | Multiple small services |
| Deployment | One deployment | Independent deployment |
| Scaling | Entire app scaled | Individual service scaled |
| Fault Isolation | One bug may crash app | Failures isolated |
| Technology Stack | Usually single stack | Multiple technologies possible |
| Development Speed | Slower in large apps | Faster with teams |
Example
Monolithic
Amazon-like app:
- User
- Product
- Payment
- Orders
All inside one application.
If payment service changes → redeploy whole app.
Microservices
Each module becomes separate service:
- User Service
- Payment Service
- Inventory Service
Only modified service gets deployed.
4. Why are Microservices Popular?
Microservices became popular because modern applications need:
- High scalability
- Faster releases
- Cloud deployment
- Independent teams
Advantages
1. Independent Deployment
Deploy one service without affecting others.
Example
Fix payment bug without redeploying product service.
2. Better Scalability
Scale only heavily used services.
Example
During sale:
- Product service traffic ↑
- Payment traffic ↑
Scale only those services.
3. Fault Isolation
Failure of one service doesn't crash entire system.
Example
Notification service fails → Orders still work.
4. Technology Flexibility
Different services can use different languages.
Example:
- Payment → Java
- Recommendation → Python
- Chat → Node.js
5. What is an API in Microservices?
API allows communication between services.
Most microservices communicate using:
- REST APIs
- gRPC
- GraphQL
- Messaging systems
Example
Order Service calls Payment Service:
POST /pay
{
"amount": 500
}
Payment Service responds:
{
"status": "SUCCESS"
}
6. What is an API Gateway?
API Gateway is the single entry point for all client requests.
Instead of clients calling services directly:
Client → API Gateway → Services
Responsibilities
1. Routing
Routes requests to correct service.
2. Authentication
Checks JWT tokens/users.
3. Rate Limiting
Prevents API abuse.
4. Logging & Monitoring
Tracks requests.
5. Load Balancing
Distributes traffic.
Example
User requests:
/api/products
Gateway forwards to:
- Product Service
Popular Tools
7. What is Service Discovery?
In microservices, service IPs change dynamically.
Service Discovery helps services locate each other automatically.
Problem Without Service Discovery
Payment service may run on:
10.1.2.3 today
10.1.5.8 tomorrow
Hardcoding IPs becomes impossible.
Solution
Services register themselves:
Payment Service → Discovery Server
Order service asks:
"Where is Payment Service?"
Discovery returns active instance.
Tools
8. What is REST?
REST (Representational State Transfer) is a standard way to build APIs over HTTP.
Uses:
- GET
- POST
- PUT
- DELETE
Example
Get product
GET /products/101
Create order
POST /orders
REST Principles
- Stateless
- Client-server architecture
- Resource-based URLs
- Uses HTTP standards
9. What is Statelessness?
In stateless architecture:
- Server stores no client session
Each request contains all necessary data.
Example
JWT Token request:
Authorization: Bearer xyz123
Server validates token each time.
Benefits
- Easy scaling
- Better reliability
- Simpler architecture
10. What is a Microservice? Give Example.
A microservice is:
A small independently deployable service performing one business capability.
Example
Payment Service
Responsibilities:
- Process payments
- Refunds
- Payment history
Independent from:
- Product Service
- User Service
11. What is Containerization?
Packaging application + dependencies together.
Ensures app runs same everywhere.
Example
Without containers:
Works on my machine
With Docker:
Works everywhere
12. What is Docker?
Docker is a platform for building and running containers.
Docker Workflow
Step 1: Create Dockerfile
FROM node:18
COPY . .
RUN npm install
CMD ["npm", "start"]
Step 2: Build Image
docker build -t product-service .
Step 3: Run Container
docker run -p 3000:3000 product-service
13. What is Kubernetes?
Kubernetes is a container orchestration platform.
It manages:
- Deployment
- Scaling
- Networking
- Self-healing
Example
Suppose:
- Product Service needs 10 instances
- Payment Service needs 5 instances
Kubernetes automatically manages them.
14. Advantages of Kubernetes in Microservices
1. Auto Scaling
Automatically adds containers under load.
2. Self Healing
If container crashes:
Kubernetes restarts it automatically
3. Rolling Updates
Deploy new versions without downtime.
4. Service Discovery
Services discover each other internally.
5. Load Balancing
Traffic distributed automatically.
15. What is CI/CD?
CI/CD automates:
- Build
- Test
- Deployment
CI = Continuous Integration
Developers merge code frequently.
Automated tests run automatically.
CD = Continuous Deployment
Automatically deploys services.
Example Pipeline
Code Push
↓
Build
↓
Test
↓
Docker Build
↓
Deploy to Kubernetes
Tools
16. Role of DevOps in Microservices
DevOps automates:
- Deployment
- Monitoring
- Infrastructure
- Logging
- Scaling
Without DevOps, managing hundreds of services becomes difficult.
17. What is a Message Broker?
A message broker enables asynchronous communication.
Instead of direct calls:
Service A → Queue → Service B
Example
Order Service places message:
{
"orderId": 101
}
Notification Service consumes it later.
Benefits
- Loose coupling
- Reliability
- Retry support
- Better scalability
Tools
18. What is Asynchronous Communication?
Sender doesn't wait for response.
Example
Order placed:
- Order Service sends event
- Email sent later
User doesn't wait for email service.
Advantages
- Faster systems
- Better reliability
- Reduced latency
19. What is Synchronous Communication?
Service waits for immediate response.
Example
Order Service → Payment Service
Order waits until payment completes.
Drawback
If Payment Service is slow:
- Entire request becomes slow
20. What is Database-per-Service Pattern?
Each microservice owns its own database.
Example
| Service | Database |
|---|---|
| User Service | MySQL |
| Product Service | MongoDB |
| Analytics Service | Cassandra |
Benefits
- Independent scaling
- Loose coupling
- Independent schema changes
21. What is Loose Coupling?
Services are independent.
Changes in one service minimally affect others.
Example
Payment Service upgraded:
- Product Service unaffected
22. What is Tight Coupling?
Services heavily depend on each other.
One change can break multiple services.
Example
Shared database among all services:
- Schema change breaks everything
23. What is Scalability?
Ability to handle increasing traffic/load.
Types
Vertical Scaling
Increase server power:
More RAM/CPU
Horizontal Scaling
Add more servers/instances.
Microservices prefer horizontal scaling.
24. How Do You Scale Microservices?
Methods
1. Add More Instances
Product Service:
1 → 10 instances
2. Use Load Balancer
Traffic distributed evenly.
3. Use Kubernetes Autoscaling
Automatically scale under heavy traffic.
4. Cache Frequently Used Data
Use:
25. What is a Health Check?
Endpoint used to verify service health.
Example
GET /health
Response:
{
"status": "UP"
}
Types
Liveness Probe
Checks if service is alive.
Readiness Probe
Checks if service can accept traffic.
26. What is Logging in Microservices?
Capturing logs/events from services.
Problem
Hundreds of services generate logs.
Need centralized logging.
Solution
Use logging platforms.
Tools
27. What is Monitoring in Microservices?
Tracking:
- CPU
- Memory
- Response time
- Errors
- Traffic
Example Metrics
| Metric | Meaning |
|---|---|
| Latency | API response time |
| Error Rate | Failed requests |
| Throughput | Requests/sec |
Tools
28. What is Distributed Tracing?
Tracks a request across multiple services.
Example
User places order:
API Gateway
↓
Order Service
↓
Payment Service
↓
Inventory Service
↓
Notification Service
Tracing shows:
- Which service was slow
- Failure points
- End-to-end latency
Tools
29. What is a Load Balancer?
Distributes incoming traffic across multiple instances.
Example
Without Load Balancer:
All traffic → One server
With Load Balancer:
Traffic → Multiple servers
Benefits
- High availability
- Better performance
- Prevents overload
Tools
30. Complete Microservices Architecture Example
E-Commerce Application
Client
│
API Gateway
│
┌─────────┬──────────┬──────────┬──────────┐
│ │ │ │ │
User Product Order Payment Inventory
Service Service Service Service Service
│ │ │ │ │
DB DB DB DB DB
Flow Example
Step 1
User places order.
Step 2
Order Service:
- Validates cart
- Calls Payment Service
Step 3
Payment successful.
Step 4
Order Service publishes event:
ORDER_CREATED
Step 5
Inventory Service:
- Reduces stock
Notification Service:
- Sends email/SMS
Analytics Service:
- Updates dashboards