0
0
Microservicessystem_design~25 mins

High cohesion in Microservices - System Design Exercise

Choose your learning style9 modes available
Design: Microservices System with High Cohesion
Focus on designing microservices with high cohesion and their interactions. Out of scope are detailed UI design and deployment infrastructure specifics.
Functional Requirements
FR1: Design a microservices-based system where each service has a focused responsibility.
FR2: Each microservice should handle a specific business capability end-to-end.
FR3: Services must communicate efficiently with minimal dependencies.
FR4: The system should support scaling individual services independently.
FR5: Ensure services are easy to maintain and update without affecting others.
Non-Functional Requirements
NFR1: The system should handle 10,000 concurrent users.
NFR2: API response time p99 should be under 300ms.
NFR3: Availability target is 99.9% uptime.
NFR4: Services must be loosely coupled but highly cohesive internally.
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
API Gateway for routing requests
Individual microservices each focused on one business domain
Service registry and discovery
Message broker or event bus for asynchronous communication
Centralized logging and monitoring tools
Design Patterns
Single Responsibility Principle applied to services
Domain-Driven Design for service boundaries
API Gateway pattern
Event-driven architecture
Circuit breaker for fault tolerance
Reference Architecture
Client
  |
  v
API Gateway
  |
  +----------------+----------------+----------------+
  |                |                |
User Service   Order Service    Inventory Service
  |                |                |
Database       Database         Database

Services communicate asynchronously via Event Bus
Components
API Gateway
Nginx or Kong
Routes client requests to appropriate microservices and handles authentication.
User Service
Spring Boot / Node.js
Manages user profiles and authentication with focused logic.
Order Service
Spring Boot / Node.js
Handles order creation, updates, and status tracking.
Inventory Service
Spring Boot / Node.js
Manages product stock levels and availability.
Event Bus
Apache Kafka or RabbitMQ
Enables asynchronous communication between services to decouple dependencies.
Databases
PostgreSQL or MongoDB
Each service owns its own database to maintain data encapsulation.
Request Flow
1. Client sends request to API Gateway.
2. API Gateway routes request to the appropriate microservice based on the endpoint.
3. Microservice processes the request using its own database and business logic.
4. If needed, microservice publishes events to the Event Bus for other services to consume.
5. Other services subscribe to relevant events and update their state accordingly.
6. Microservice returns response to API Gateway, which forwards it to the client.
Database Schema
User Service: User(id PK, name, email, password_hash) Order Service: Order(id PK, user_id FK, status, total_amount) Inventory Service: Product(id PK, name, stock_quantity) Each service owns its schema to ensure high cohesion and data encapsulation.
Scaling Discussion
Bottlenecks
API Gateway can become a single point of failure or bottleneck.
Database contention within a single service under heavy load.
Event Bus overload if too many events are published.
Tight coupling if services share databases or logic.
Solutions
Use load balancing and multiple instances for API Gateway with health checks.
Scale databases vertically or use read replicas; consider sharding if needed.
Partition Event Bus topics and scale brokers horizontally.
Enforce strict service boundaries and use asynchronous communication to reduce coupling.
Interview Tips
Time: Spend 10 minutes understanding requirements and clarifying scope, 20 minutes designing the system focusing on high cohesion in microservices, 10 minutes discussing scaling and trade-offs, 5 minutes summarizing.
Explain the importance of high cohesion for maintainability and scalability.
Describe how each microservice owns a single business capability and its own data.
Discuss communication patterns that reduce coupling, like event-driven messaging.
Highlight how independent scaling of services improves performance.
Mention monitoring and fault tolerance to maintain availability.