0
0
Microservicessystem_design~25 mins

First microservice architecture diagram in Microservices - System Design Exercise

Choose your learning style9 modes available
Design: Simple E-commerce Microservice System
Design microservices for product catalog, order management, and user management. Include API gateway and database. Exclude payment gateway and shipping services.
Functional Requirements
FR1: Users can browse products
FR2: Users can place orders
FR3: Orders are processed asynchronously
FR4: System should handle 1000 concurrent users
FR5: API response time p99 under 300ms
Non-Functional Requirements
NFR1: Availability 99.9% uptime
NFR2: Scale to 1000 concurrent users
NFR3: Latency p99 < 300ms for API calls
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
API Gateway
Product Service
Order Service
User Service
Database per service
Message Queue for async processing
Design Patterns
API Gateway pattern
Database per service pattern
Event-driven architecture
Circuit breaker for fault tolerance
Reference Architecture
          +-------------+          
          |   Clients   |          
          +------+-----+          
                 |                
          +------+-----+          
          | API Gateway |          
          +------+-----+          
           /     |     \          
          /      |      \         
+---------+  +---+----+  +-------+
| Product |  | Order  |  | User  |
| Service |  | Service|  | Service|
+----+----+  +---+----+  +---+---+
     |           |          |     
+----v----+  +---v----+  +--v----+
|ProductDB|  |OrderDB |  |UserDB |
+---------+  +--------+  +-------+
           \          /          
            +--------+           
            | Message |           
            | Queue   |           
            +--------+           
Components
API Gateway
Nginx or Kong
Single entry point for clients, routes requests to appropriate microservices, handles authentication
Product Service
Node.js or Spring Boot
Manages product catalog and product data
Order Service
Node.js or Spring Boot
Handles order placement and processing asynchronously
User Service
Node.js or Spring Boot
Manages user profiles and authentication
Databases
PostgreSQL or MongoDB
Each service has its own database to ensure loose coupling
Message Queue
RabbitMQ or Kafka
Enables asynchronous communication between services, especially for order processing
Request Flow
1. Client sends request to API Gateway
2. API Gateway authenticates and routes request to appropriate microservice
3. Product Service fetches product data from its database and returns to client
4. When user places an order, API Gateway routes request to Order Service
5. Order Service saves order in its database and publishes order event to Message Queue
6. Order processing workers consume events from Message Queue to process orders asynchronously
7. User Service manages user data and authentication independently
Database Schema
Entities: - Product: id (PK), name, description, price, stock - Order: id (PK), user_id (FK), product_id (FK), quantity, status, created_at - User: id (PK), username, email, password_hash Relationships: - Order references User and Product by foreign keys - Each service owns its own database schema to avoid tight coupling
Scaling Discussion
Bottlenecks
API Gateway can become a single point of failure
Order Service database can become a bottleneck under high write load
Message Queue can be overwhelmed with too many events
Product Service database read load can increase with many users browsing
Solutions
Deploy multiple API Gateway instances behind a load balancer for high availability
Use database sharding or read replicas for Order Service database
Scale Message Queue cluster horizontally and partition topics
Implement caching (e.g., Redis) for Product Service to reduce database reads
Interview Tips
Time: 10 minutes for requirements and clarifications, 20 minutes for architecture design and data flow, 10 minutes for scaling discussion, 5 minutes for questions
Explain why microservices are chosen for modularity and scalability
Describe how API Gateway simplifies client interaction
Discuss asynchronous processing with message queue for order handling
Highlight database per service to reduce coupling
Address scaling challenges and solutions clearly