BookStore Microservices Architecture
Catalog
Service
BookStore API
WebApp Gateway
Order
Service
OAuth Notification
Authorization Service
Server
What is Microservices Architecture?
Microservices is an architectural style where large applications are composed of small services
that provide specific business capabilities that can be deployed and managed independently.
Learning Objectives
Building Spring Boot REST APIs
Database Persistence using Spring Data JPA, Postgres, Flyway
Event Driven Async Communication using RabbitMQ
Implementing OAuth2-based Security using Spring Security and Keycloak
Implementing API Gateway using Spring Cloud Gateway
Implementing Resiliency using Resilience4j
Job Scheduling with ShedLock-based distributed Locking
Using RestClient, Declarative HTTP Interfaces to invoke other APIs
Creating Aggregated Swagger Documentation at API Gateway
Local Development Setup using Docker and Testcontainers
Testing using JUnit 5, RestAssured, Testcontainers, Awaitility, WireMock
Additional Topics(Membership)
Monitoring & Observability using Grafana, Prometheus, Loki, Tempo
Kubernetes 101 course
Deployment to Kubernetes
BookStore Monolithic Architecture
Catalog
Module
WebApp Orders
Module Module
Notification
Module
OAuth
Authorization
Server
Monolithic Architecture
Pros Cons
1. Simpler Development 1. Difficult to scale sub-systems(modules)
2. Easier Testing & Debugging 2. Difficult to adopt new technologies
3. Simpler Deployment 3. Higher chance to become big ball of mud
BookStore Microservices Architecture
Catalog
Service
BookStore
API Gateway
WebApp
Order
Service
OAuth
Notification
Authorization
Service
Server
Microservices Architecture
Pros Cons
1. Can scale individual services 1. Difficult to build & manage distributed systems
2. Smaller codebases easy to reason about 2. Difficult to test & debug
3. Easy to adopt newer technologies if needed 3. Complex deployment process
4. Less dependency on other team deliverables 4. Performance Issues
Spring Cloud vs Kubernetes
Spring Cloud Config Server
catalog-service
Config Repo
(git, DB, Consul, etc)
order-service
notification-service
Service Registry (Eureka, Consul)
102.119.12.10
catalog-service
instance 1
102.119.12.20
Client catalog-service catalog-service
instance 2
102.119.12.30
catalog-service
instance 3
Kubernetes
102.119.12.10
catalog-service
pod 1
service
102.119.12.20
Client catalog-service catalog-service
pod 2
102.119.12.30
catalog-service
pod 3
Package Structure
Controller Service Repository
DB
Package By Layer Package By Feature
root
root
- controllers
- products
- ProductController
- ProductController
- CustomerController
- ProductService
- services
- ProductRepository
- ProductService
- CustomerService - customers
- CustomerController
- repositories
- ProductRepository
- CustomerService
- CustomerRepository - CustomerRepository
Package By Component
root
- web
- products
- ProductController ProductController ProductService ProductRepository
- customers
- CustomerController
- domain
CustomerController CustomerService CustomerRepository
- products
- ProductService
- ProductRepository
- customers
- CustomerService
- CustomerRepository
BookStore Microservices Architecture
Catalog
Service
BookStore API
WebApp Gateway
Order
Service
OAuth Notification
Authorization Service
Server
What is Microservices Architecture?
Microservices is an architectural style where large applications are composed of small services
that provide specific business capabilities that can be deployed and managed independently.
Learning Objectives
Building Spring Boot REST APIs
Database Persistence using Spring Data JPA, Postgres, Flyway
Event Driven Async Communication using RabbitMQ
Implementing OAuth2-based Security using Spring Security and Keycloak
Implementing API Gateway using Spring Cloud Gateway
Implementing Resiliency using Resilience4j
Job Scheduling with ShedLock-based distributed Locking
Using RestClient, Declarative HTTP Interfaces to invoke other APIs
Creating Aggregated Swagger Documentation at API Gateway
Local Development Setup using Docker and Testcontainers
Testing using JUnit 5, RestAssured, Testcontainers, Awaitility, WireMock
Additional Topics(Membership)
Monitoring & Observability using Grafana, Prometheus, Loki, Tempo
Kubernetes 101 course
Deployment to Kubernetes
BookStore Monolithic Architecture
Catalog
Module
WebApp Orders
Module Module
Notification
Module
OAuth
Authorization
Server
Monolithic Architecture
Pros Cons
1. Simpler Development 1. Difficult to scale sub-systems(modules)
2. Easier Testing & Debugging 2. Difficult to adopt new technologies
3. Simpler Deployment 3. Higher chance to become big ball of mud
BookStore Microservices Architecture
Catalog
Service
BookStore
API Gateway
WebApp
Order
Service
OAuth
Notification
Authorization
Service
Server
Microservices Architecture
Pros Cons
1. Can scale individual services 1. Difficult to build & manage distributed systems
2. Smaller codebases easy to reason about 2. Difficult to test & debug
3. Easy to adopt newer technologies if needed 3. Complex deployment process
4. Less dependency on other team deliverables 4. Performance Issues
Spring Cloud vs Kubernetes
Spring Cloud Config Server
catalog-service
Config Repo
(git, DB, Consul, etc)
order-service
notification-service
Service Registry (Eureka, Consul)
102.119.12.10
catalog-service
instance 1
102.119.12.20
Client catalog-service catalog-service
instance 2
102.119.12.30
catalog-service
instance 3
Kubernetes
102.119.12.10
catalog-service
pod 1
service
102.119.12.20
Client catalog-service catalog-service
pod 2
102.119.12.30
catalog-service
pod 3
Package Structure
Controller Service Repository
DB
Package By Layer Package By Feature
root
root
- controllers
- products
- ProductController
- ProductController
- CustomerController
- ProductService
- services
- ProductRepository
- ProductService
- CustomerService - customers
- CustomerController
- repositories
- ProductRepository
- CustomerService
- CustomerRepository - CustomerRepository
Package By Component
root
- web
- products
- ProductController ProductController ProductService ProductRepository
- customers
- CustomerController
- domain
CustomerController CustomerService CustomerRepository
- products
- ProductService
- ProductRepository
- customers
- CustomerService
- CustomerRepository
RabbitMQ
Exchange
Consumer 1
Consumer 2
Producer
Consumer 3
Consumer 4
Exchange Types
1. Direct Exchange
2. Topic Exchange
3. Fanout Exchange
Direct Exchange
Binding Key: Simple String
Ex: orders, cancellations, accounts, new-orders, delivered-orders, etc
Topic Exchange
Binding Key: Support Patterns and Wildcards
Ex: orders.new.*, orders.*.cancelled, orders.new.#, etc
Fanout Exchange
Binding Key: Ignored
Broadcast the messages to all the queues
Direct Exchange
routingKey=orders-new
Producer
routingKey=orders-delivered
RoutingKey
orders-new routingKey=orders-cancelled
orders-delivered
orders-cancelled
Resilience Patterns
1. Timeout (or) TimeLimiter
2. Retry
3. Bulk Head
4. Circuit Breaker
5. Rate Limiter
Service A
Endpoint 1
Service B
Endpoint 2
Endpoint 3
Endpoint 4
Endpoint 5
Make changes to data in DB
Publish message(s) to Queue
OutBox Pattern
Make changes to data in DB
Persist message(s) in events
table
Scheduled Job
Read Events from DB
Publish message(s) to Queue
Consumer
NOTE: Consumer should be idempotent.
i.e, should be able to handle duplicate
messages
API Gateway
MicroService 1
Client 1
MicroService 2
API Gateway
MicroService 3
Client 2
MicroService 4
API Gateway
1. Hides internal Technical Architecture complexities from Clients
2. Allows to re-design the backend services without affecting clients
3. Dynamic routing to different API versions
4. Centralized Security, Rate Limiting, Timeout, etc.
Cross Origin Resource Sharing
otherservice.com
myapp.com
Same Origin
Request
myapp.com
Cross Origin Request
Traditional Server Rendered Web Apps
Request
Response (HTML)
Single Page Applications (SPAs)
Initial Request
Host Page Response (HTML)
API call
JSON Response
API call
JSON Response
Multi Page Applications
Page 1 Request
Page 1 Response (HTML)
API call
JSON Response
API call
JSON Response
Page 2 Request
Page 2 Response (HTML)
API call
JSON Response
Single Server Rendered Web Application
HTTP Session or store client state on DB
SPA Frontend and REST API Backend
React/
Angular/
Vue
Server creates JWT token and the token is stored at client
side and pass with each request
Microservices
Auth
Server
Spring MVC
WebApp
API Gateway Microservice 1
React/
Angular/
Vue
Microservice 2
Microservice 3
1. Client logins using Auth Server Login page and get access_token and id_token
2. Client applications include the access_token for each API request as Authorization header
pp _ q
3. API Gateway or Microservices validates the access_token and process or reject the request
Observability
1. Logs
2. Metrics
3. Traces
Trace
Span 1
Span 2
Span 3