Search This Blog

Saturday, February 1, 2025

Q6-Q10

Q6: What could be your folder structure for MS project in C#?
Q7: What is circuit breaker design pattern? When to use.
Q8: What are the various states of Circuit breaker design pattern?
Q9: How Circuit monitors the failure and get activated. How it changes it states to normal again?
Q10: List various Microservices Architecture design pattern?

Circuit Design Pattern
-----------------------------------------------------------------------------------------------------------------------------
Q6: What could be your folder structure for MS project in C#?

Answer:

MyService/
├── MyService.Api/               # API layer (Controllers, Startup, Program)
│   ├── Controllers/
│   ├── Models/
│   ├── DTOs/
│   ├── Startup.cs
│   └── Program.cs
├── MyService.Application/       # Business logic layer
│   ├── Interfaces/
│   ├── Services/
│   └── DTOs/
├── MyService.Domain/            # Core domain logic
│   ├── Entities/
│   ├── ValueObjects/
│   ├── Exceptions/
│   └── Interfaces/
├── MyService.Infrastructure/    # Data access, external integrations
│   ├── Persistence/             # EF Core DbContext, Repositories
│   ├── Migrations/
│   └── ExternalServices/
├── MyService.Tests/              # Unit and integration tests
└── dockerfile                    # For containerization

Key points for microservices:

API layer → Handles HTTP endpoints (Controllers, minimal APIs).
Application layer → Contains business logic, services, and use cases.
Domain layer → Pure business models and rules, independent of frameworks.
Infrastructure layer → Database, external APIs, message queues.
Tests → Unit tests for each layer, integration tests for endpoints.

-----------------------------------------------------------------------------------------------------------------------------
Q7: What is circuit breaker design pattern? When to use. 

Answer:
It acts like an electrical circuit breaker: it monitors for failures and stops the flow of requests to a failing service to prevent cascading failures and allow time for recovery.

When to Use:
  • When calling remote services, APIs, or databases that might fail or become unresponsive.
  • In microservices where one service depends on another.
  • When you want to fail fast instead of waiting for timeouts.
In this pattern, a circuit breaker acts as a safety net between the client and the service, protecting the client from failures in the service. The circuit breaker monitors the status of the service and, if it detects that the service is failing, it can open the circuit and prevent further requests from being sent to the service until the service has recovered.

-----------------------------------------------------------------------------------------------------------------------------
Q8: What are the various states of Circuit breaker design pattern. How Circuit monitors the failure and get activated. How it changes it states to normal again?

Answer:

Different states in the circuit break pattern
Closed - When everything works well according to the normal way, the circuit breaker remains in this closed state.
Open -  When the number of failures in the system exceeds the maximum threshold, this will lead to open up the open state. This will give the error for calls without executing the function.
Open -Half (Half Open Half Close) - After having run the system several times, the circuit breaker will go on to the half-open state in order to check the underlying problems are still exist. 

-----------------------------------------------------------------------------------------------------------------------------
Q9: How Circuit monitors the failure and get activated. How it changes it states to normal again?

Answer:
Here’s how it works in simple terms:

1. Normal Operation (Closed State)
  • Requests flow normally to the downstream service.
  • The Circuit Breaker monitors failures (e.g., timeouts, exceptions).
  • If the failure rate crosses a configured threshold (say, 5 failures in 10 requests), it opens the circuit.
2. Failure Mode (Open State)
  • No new requests are sent to the failing service — they fail fast (avoiding long waits).
  • Instead, the system may: Return a fallback response Queue the request for later retry
3. Recovery Check (Half-Open State)
  • After a “cool-down period” (timeout), the Circuit Breaker lets a small number of requests through to test the service.
  • If they succeed → circuit goes back to Closed (normal).
  • If they fail → it returns to Open for another cooldown.

-----------------------------------------------------------------------------------------------------------------------------
Q10: List various Microservices Architecture design pattern?

Answer:
1. Database per Microservice
2. Event Sourcing
3. CQRS
4. Saga
5. BFF (Backend for FrontEnds)
6. API Gateway
7. Strangler
8. Circuit Breaker
9. Externalized Configuration
10. Consumer-Driven Contract Tracing
11. Aggregator Design pattern. 


My Other Blogs

 

Q1-Q5

Q1 What are microservices?
Q2: What are the various ways of communication between microservices?
Q3: How to implement Load balancing in MS. What are the different tpes of load balancing. 
Q4: How we can do Monitoring, health check, log aggregation in MS. 
Q5: How hosting of microservice in general is planned?
----------------------------------------------------------------------------------------------------------------
Q1 What are microservices?

Answer:
Microservices are an architectural approach where an application is built as a collection of small, independent services, each responsible for a specific business function, communicating through APIs. They allow faster development, scalability, and easier maintenance compared to a single large monolithic application.
----------------------------------------------------------------------------------------------------------------

Q2: What are the various ways of communication between microservices?

Answer:

1. RPC (Remote procedure Invocation) - This include communication b/w two MS sync/ asyc using REST, SOAP or gPRC protocols. 

2. Messaging services - Communication b/w MS with the help of messsaing service in b/w eg - Kafka, RabbitMQ, Azure Storage Queue, Azure Service Bus etc

3. Service Registry: a Service Registery middleware between multiple MS. eg Zookeper, Azure API management can also be used. 

4. API Gateway: Single point of contact for multiple MS. similar to Service Registry but more modern approach. We can implement Authentication at G/W as well. eg Azure API GW, Ocelot. 
----------------------------------------------------------------------------------------------------------------
Q3: How to implement Load balancing in MS. What are the different tpes of load balancing. 

Answer:
LB can be of 
1) Round Robin
2) Capacity - based on capacity of hosted env, eg ms is hosted on higher ram than we can plan more incoming request to that node, 
3) Weight: We can plan more incoming request to MS which is hosted near to our region.
----------------------------------------------------------------------------------------------------------------
Q4: How we can do Monitoring, health check, log aggregation in MS. 

Answer:
famous tools are Grafana for monitoring
Splunk and DataDog for log aggregation. 
----------------------------------------------------------------------------------------------------------------
Q5: How hosting of microservice in general is planned?

Answer:
Usually, MS are packaged with dependencies into container images. Docker is one of the famous tools used to create container images called docker images. Then these images are hosted and orchestrated using Kubernetes. 

Other than Docker, we can use Azure service like ACI, Azure Container instance to create images (non-docker) from the ms. 

Other than Kubernetes, we can use Azure Kubernetes Services AKS for orchestration. 
----------------------------------------------------------------------------------------------------------------



Q31-Q35

Q31: What is pods and how it is related to Docker images and Docker containers? Q32: What is the use of Compose Docker file? Q33. What are t...