Design EC2 Service
Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2024-10-04
Problem Statement
Design a cloud compute service (like AWS EC2) that allows customers to provision, manage, and run virtual machines (VMs) on-demand. The service should provide scalability, elasticity, isolation, and fault tolerance, while exposing APIs for developers to interact programmatically.
✅ Functional Requirements
1.<span></span>Instance Lifecycle Management
2.<span></span>Launch/start/stop/reboot/terminate instances.
3.<span></span>Support multiple instance types (CPU, memory, storage optimized).
4.<span></span>Networking
5.<span></span>Each instance should get an IP (private & public).
6.<span></span>Support VPC, subnets, firewalls (security groups).
7.<span></span>Storage
8.<span></span>Support attaching/detaching persistent volumes (like EBS).
9.<span></span>Support ephemeral storage.
10.<span></span>Scaling
11.<span></span>Allow customers to scale up/down number of instances.
12.<span></span>Support auto-scaling based on metrics (CPU, memory).
13.<span></span>Monitoring & Metrics
14.<span></span>Collect logs/metrics (CPU, disk, network).
15.<span></span>Provide dashboards & alerting.
16.<span></span>APIs & Console
17.<span></span>REST/gRPC APIs for programmatic access.
18.<span></span>Management console for UI-based provisioning.
⚙️ Non-Functional Requirements
1.<span></span>High Availability → No single point of failure.
2.<span></span>Elasticity → Rapid scale-in/scale-out.
3.<span></span>Isolation → VMs of different tenants must be isolated (security).
4.<span></span>Performance → Provisioning within seconds, low network latency.
5.<span></span>Cost Efficiency → Optimize for resource utilization across tenants.
6.<span></span>Security & Compliance → Data encryption, IAM integration, audit logging.
🔐 Constraints
1.<span></span>Must support multi-tenancy safely (tenant isolation).
2.<span></span>Resource pools (compute, storage, networking) must be dynamically allocated.
3.<span></span>VM lifecycle operations (start/stop/terminate) must be idempotent.
4.<span></span>Must integrate with identity & access management (IAM) for authentication/authorization.
🧱 Key Components to Design
1.<span></span>API Gateway – Expose EC2 APIs for users (launch/start/stop).
2.<span></span>Compute Orchestrator – Manages VM lifecycle, scheduling on physical hosts.
3.<span></span>Hypervisor/Container Layer – Provides virtualization (KVM, Xen, Firecracker).
4.<span></span>Resource Manager – Allocates CPU, memory, disk, network from resource pools.
5.<span></span>Storage Service – Manages persistent volumes (block storage like EBS).
6.<span></span>Networking Service – Assigns IPs, configures VPC/subnets/security groups.
7.<span></span>Scheduler – Determines placement of instances on available hosts.
8.<span></span>Monitoring & Logging – Collects metrics, health checks, auto-scaling triggers.
9.<span></span>Billing & Metering Service – Tracks usage per tenant for cost calculation.
🧪 Example Flow: Launching an Instance
<div><div><br /></div><div>1. User calls EC2 API → Launch Instance request. </div><div>2. API Gateway forwards to Compute Orchestrator. </div><div>3. Scheduler selects a physical host (based on availability & resource needs). </div><div>4. Hypervisor boots a VM from chosen AMI (image). </div><div>5. Networking Service assigns IP, configures firewall/security groups. </div><div>6. Storage Service attaches required volumes. </div><div>7. Instance moves to RUNNING state. </div><div>8. Monitoring Service starts collecting metrics. </div><div>9. Billing Service tracks usage for pricing. </div></div>👉 This problem tests:
1.<span></span>Designing multi-tenant distributed systems.
2.<span></span>Resource scheduling & virtualization.
3.<span></span>Handling elasticity, high availability, and fault tolerance.
4.<span></span>Cloud-scale monitoring, networking, and billing challenges.