Objective
Understand the microservices architecture, its principles, and implementation using Spring Boot. Learn to fragment business requirements into microservices, apply deployment patterns, use an API gateway for routing, implement service discovery, and manage databases for microservices. Demonstrate a Spring Boot microservices application with two services (Employee and Department) communicating via REST APIs.
Lecture Topics
1. Introduction to Microservices
- Definition: Microservices architecture structures an application as a collection of small, independent services, each responsible for a specific business capability, communicating over a network (e.g., via REST APIs).
- Key Characteristics:
- Modularity: Each service handles a single function (e.g., employee management, department management).
- Independence: Services are developed, deployed, and scaled independently.
- Decentralized: Each service can use its own technology stack and database.
- Interoperability: Services communicate using standard protocols (HTTP/REST, messaging).
- Benefits:
- Scalability: Scale only high-demand services (e.g., scale employee service during hiring season).
- Flexibility: Use different languages or frameworks (e.g., Java for one service, Python for another).
- Resilience: Failure in one service (e.g., payment service) doesn’t crash the entire system.
- Faster Development: Teams work on separate services concurrently.
- Challenges:
- Distributed Systems: Network latency, fault tolerance, and eventual consistency.
- Data Management: Each service has its own database, complicating data consistency.
- Monitoring: Tracking health and performance across multiple services.
- Deployment Complexity: Managing multiple deployments and versions.
- Exam Tip: Be able to define microservices and list at least three benefits and challenges.
2. Microservices Architecture
- Core Principles:
- Single Responsibility: Each microservice focuses on one business capability.
- Loose Coupling: Services interact via well-defined APIs, minimizing dependencies.
- Decentralized Governance: Teams choose the best tools for each service.
- Autonomy: Services are independently deployable and scalable.
- Components:
- Services: Small applications exposing REST APIs or message queues.
- API Gateway: Central entry point for routing client requests to services.
- Service Discovery: Mechanism for services to find each other dynamically.
- Databases: Separate databases per service to ensure independence.
- Comparison with Monolithic Architecture:
- Monolithic: Single codebase, shared database, tight coupling, single deployment.
- Microservices: Multiple services, separate databases, loose coupling, independent deployments.
- Example: A library system as a monolith (single app for books, users, loans) vs. microservices (separate services for books, users, loans).
- Exam Tip: Explain how microservices architecture improves scalability compared to monolithic systems.
3. Fragmentation of Business Requirement
- Definition: Breaking down a large application into smaller services based on business capabilities.
- Process:
- Identify business domains (e.g., in a library system: book management, user management, loan processing).
- Define service boundaries to ensure each service has a single responsibility.
- Design APIs for inter-service communication (e.g., REST endpoints for
/books
, /users
).
- Example:
- Monolithic Library System: One application handles books, users, and loans.
- Microservices:
- Book Service: Manages book catalog (CRUD operations).
- User Service: Manages user accounts.
- Loan Service: Tracks book loans.