Monolith vs Microservices
Monolith
- A monolith is an application where all feature are deployed together as a single unit.
- Consider ecommerce application: we have
- user logins
- Catalog
- Cart
- Payments
- Orders
- Notification
- Deployement:
- Entire application has to be redeployed irrespective of size/volume of change
- Scaling:
- Entire application has to be scaled.
- docker compose for monolith
version: "3.9"
services:
app:
image: ecommerce-monolith:1.0
container_name: ecommerce-app
restart: unless-stopped
depends_on:
- mysql
- redis
environment:
APP_ENV: production
DB_HOST: mysql
DB_PORT: 3306
DB_NAME: ecommerce
DB_USER: ecommerce_user
DB_PASSWORD: ecommerce_password
REDIS_HOST: redis
REDIS_PORT: 6379
PAYMENT_GATEWAY_KEY: demo_key
SMTP_HOST: mail.example.com
SMTP_PORT: 587
networks:
- ecommerce-network
mysql:
image: mysql:8.4
container_name: ecommerce-mysql
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: ecommerce
MYSQL_USER: ecommerce_user
MYSQL_PASSWORD: ecommerce_password
volumes:
- mysql-data:/var/lib/mysql
networks:
- ecommerce-network
redis:
image: redis:7-alpine
container_name: ecommerce-redis
restart: unless-stopped
volumes:
- redis-data:/data
networks:
- ecommerce-network
nginx:
image: nginx:alpine
container_name: ecommerce-nginx
restart: unless-stopped
depends_on:
- app
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
networks:
- ecommerce-network
networks:
ecommerce-network:
driver: bridge
volumes:
mysql-data:
redis-data:
- Diagram
Internet
|
v
+-------------+
| Nginx |
+-------------+
|
v
+---------------------+
| Monolith App |
| |
| - Product Catalog |
| - Orders |
| - Cart |
| - Payments |
| - Users |
| - Inventory |
+---------------------+
| |
| |
v v
+------------+ +-----------+
| MySQL | | Redis |
+------------+ +-----------+
Microservices
- In microservices application is split into smaller independent services
- Ecommerce:
- User service
- Product Service
- Cart Service
- Payment Service
- Order Service
- Notification Service
- With microservices:
- we can indvidually deploy the services
- We can individually scale the services rather than whole application
- Reusability will be more
- Management of multiple services will be complex to handle
- IN microservices:
- To run individual service, majorly container technology is used (Docker)
- To manage multiple services/containers we need container orchestration (Kubernetes)
- Diagram
Internet
|
v
+-------------+
| API Gateway |
| (Nginx) |
+-------------+
|
------------------------------------------------
| | | | |
v v v v v
+---------+ +---------+ +---------+ +---------+ +---------+
| Product | | User | | Order | | Payment | | Cart |
| Service | | Service | | Service | | Service | | Service |
+---------+ +---------+ +---------+ +---------+ +---------+
| | | | |
-----------------------------------------------
|
v
+---------------+
| Message Broker|
| RabbitMQ |
+---------------+
| | | | |
v v v v v
+--------+ +--------+ +--------+ +--------+ +--------+
|Product | | UserDB | |OrderDB | |PayDB | |CartDB |
|DB | | | | | | | | |
+--------+ +--------+ +--------+ +--------+ +--------+
+-------------+
| Redis |
+-------------+
- Docker compose
version: "3.9"
services:
gateway:
image: nginx:alpine
container_name: api-gateway
ports:
- "80:80"
depends_on:
- user-service
- product-service
- order-service
- payment-service
- cart-service
networks:
- ecommerce
user-service:
image: ecommerce/user-service:1.0
environment:
DB_HOST: user-db
DB_PORT: 3306
depends_on:
- user-db
networks:
- ecommerce
product-service:
image: ecommerce/product-service:1.0
environment:
DB_HOST: product-db
DB_PORT: 3306
depends_on:
- product-db
networks:
- ecommerce
order-service:
image: ecommerce/order-service:1.0
environment:
DB_HOST: order-db
DB_PORT: 3306
RABBITMQ_HOST: rabbitmq
depends_on:
- order-db
- rabbitmq
networks:
- ecommerce
payment-service:
image: ecommerce/payment-service:1.0
environment:
DB_HOST: payment-db
DB_PORT: 3306
RABBITMQ_HOST: rabbitmq
depends_on:
- payment-db
- rabbitmq
networks:
- ecommerce
cart-service:
image: ecommerce/cart-service:1.0
environment:
REDIS_HOST: redis
depends_on:
- redis
networks:
- ecommerce
user-db:
image: mysql:8
environment:
MYSQL_DATABASE: users
MYSQL_ROOT_PASSWORD: root
volumes:
- user-data:/var/lib/mysql
networks:
- ecommerce
product-db:
image: mysql:8
environment:
MYSQL_DATABASE: products
MYSQL_ROOT_PASSWORD: root
volumes:
- product-data:/var/lib/mysql
networks:
- ecommerce
order-db:
image: mysql:8
environment:
MYSQL_DATABASE: orders
MYSQL_ROOT_PASSWORD: root
volumes:
- order-data:/var/lib/mysql
networks:
- ecommerce
payment-db:
image: mysql:8
environment:
MYSQL_DATABASE: payments
MYSQL_ROOT_PASSWORD: root
volumes:
- payment-data:/var/lib/mysql
networks:
- ecommerce
redis:
image: redis:7-alpine
networks:
- ecommerce
rabbitmq:
image: rabbitmq:3-management
ports:
- "15672:15672"
networks:
- ecommerce
networks:
ecommerce:
volumes:
user-data:
product-data:
order-data:
payment-data:
Google And Containers
- Google has been using containers even before docker and they were using this in very large customer bases
-
To manage this containers (Container orchestration), Google has internal projects named
- Omega
- Borg
-
When the usage of docker has grown, Google has taken the best practices from their experience in running containers in Production and released Kubernetes with
Take Docker containers to Production - Kuberentes is an opensource project handled by CNCF Refer Here
