0% found this document useful (0 votes)
42 views18 pages

Unit 2 - Micro-Services

Uploaded by

bodamaddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views18 pages

Unit 2 - Micro-Services

Uploaded by

bodamaddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Using containers instead of virtual machines (VMs) is becoming

increasingly popular in modern application deployment due to the


advantages containers offer in terms of efficiency, performance, and
scalability. Let’s dive into the detailed comparison between containers and
virtual machines, their architectures, benefits, and how they impact
infrastructure.

1. Understanding Containers

a. What are Containers?

Containers are lightweight, standalone packages that contain everything


needed to run a piece of software: code, runtime, libraries, and system
tools. They provide an isolated environment to run applications but share
the host operating system’s kernel with other containers.

b. Container Architecture

Containers operate on top of the host operating system and share the
kernel, unlike VMs that require a full OS instance for each VM. This makes
containers much lighter and faster in terms of performance and startup
time.

 Container Engine (e.g., Docker): Manages the lifecycle of


containers (e.g., creating, running, and destroying them).

 Shared Kernel: Containers run isolated processes using the same


OS kernel, which reduces overhead.

 Namespaces and Cgroups: Linux technologies used to isolate and


allocate resources to containers (e.g., process ID namespace, user
namespace, etc.).

plaintext

Copy code

+-------------------------------+

| Application A | Container A

|-------------------------------|

| Binaries & Libraries |

|-------------------------------|

| Shared Operating System Kernel|

+-------------------------------+

| Application B | Container B
|-------------------------------|

| Binaries & Libraries |

|-------------------------------|

| Shared Operating System Kernel|

+-------------------------------+

c. Key Characteristics of Containers

 Lightweight: Containers are smaller and require fewer resources


compared to VMs. Typically, containers are measured in megabytes
(MB), whereas VMs can be gigabytes (GB).

 Fast Start-Up: Since containers share the OS kernel and do not


need to boot an entire operating system, they can start up in
seconds, unlike VMs which may take minutes.

 Isolation: Although containers share the host OS kernel, they are


isolated from each other through namespaces and control groups,
ensuring they don’t interfere with each other’s processes and
resources.

 Portability: Containers encapsulate all dependencies, making them


portable across different environments (e.g., from a developer’s
laptop to production servers).

2. Understanding Virtual Machines (VMs)

a. What are Virtual Machines?

A virtual machine is an emulation of a physical computer. VMs run on


hypervisors, and each VM includes its own full operating system, along
with virtualized hardware, such as CPUs, memory, storage, and network
interfaces. This provides strong isolation but at the cost of resource
efficiency.

b. VM Architecture

VMs run on a hypervisor that manages the allocation of physical resources


(CPU, memory, storage) and abstracts them for each virtual machine.
Each VM runs a separate instance of a guest operating system (OS),
making it resource-intensive.

 Hypervisor: A layer that enables virtualization and manages VMs.


There are two types:

o Type 1 (Bare-Metal Hypervisors): Run directly on the host


hardware (e.g., VMware ESXi, Microsoft Hyper-V).
o Type 2 (Hosted Hypervisors): Run on a host operating
system (e.g., VirtualBox, VMware Workstation).

 Guest OS: Each VM contains its own guest OS, requiring significant
resources, including memory and storage.

plaintext

Copy code

+-------------------------------+

| Application A | VM A

|-------------------------------|

| Binaries & Libraries |

|-------------------------------|

| Guest OS (Full) |

+-------------------------------+

| Application B | VM B

|-------------------------------|

| Binaries & Libraries |

|-------------------------------|

| Guest OS (Full) |

+-------------------------------+

| Hypervisor |

+-------------------------------+

| Host OS (If using Type 2) |

+-------------------------------+

| Physical Hardware |

+-------------------------------+

c. Key Characteristics of VMs

 Heavyweight: VMs require a full guest OS for each instance,


consuming more CPU, memory, and storage compared to
containers.
 Slower Start-Up: Since each VM needs to boot its own OS, it takes
longer to start (measured in minutes), especially compared to
containers.

 Strong Isolation: VMs provide stronger isolation than containers


because each VM is fully separate from others with its own OS and
virtualized hardware.

 Resource Overhead: VMs duplicate resources such as OS files,


libraries, and binaries, leading to higher overhead compared to
containers.

3. Key Differences: Containers vs. Virtual Machines

Aspect Containers Virtual Machines (VMs)

Process-level isolation using


Isolation Full OS isolation
namespaces

Startup Time Seconds Minutes

Resource Highly efficient, shares OS Less efficient, requires


Efficiency kernel separate OS for each VM

Storage Size Lightweight, typically MBs Heavy, typically GBs

Less portable due to OS


Portability Portable across environments
dependencies

Easy to manage with container


More complex due to the
Management orchestration tools like
full OS stack
Kubernetes

Near-native performance due to Slight overhead from


Performance
shared OS kernel hypervisor abstraction

Weaker isolation (shared OS Strong isolation


Security
kernel) (dedicated OS)

4. When to Use Containers vs. VMs

Use Cases for Containers:

 Microservices Architecture: Containers are ideal for


microservices where each service is deployed independently, and
fast scaling is needed.

 CI/CD Pipelines: Containers' fast startup and teardown times make


them perfect for continuous integration and deployment
environments.
 Portability: If you need to run applications consistently across
multiple environments (development, testing, production),
containers ensure portability.

 Efficient Resource Utilization: When resource efficiency is crucial


(e.g., high-density computing environments), containers are
preferable due to their smaller footprint.

Use Cases for Virtual Machines:

 Legacy Applications: Some legacy applications may require full


OS-level isolation and might not be compatible with
containerization.

 Stronger Security Requirements: In environments where


isolation is paramount (e.g., financial institutions, secure
government environments), VMs provide a stronger separation
between workloads.

 Running Different OSs: If you need to run multiple different


operating systems (e.g., Linux, Windows) on the same physical
hardware, VMs are required.

5. Modern Infrastructure Trends: Container Orchestration

While containers provide lightweight isolation and portability, managing


containers at scale requires orchestration tools like Kubernetes, Docker
Swarm, or OpenShift. These platforms automate the deployment,
scaling, and management of containerized applications across clusters of
machines.

 Key Features of Orchestration:

o Auto-scaling: Automatically scales containers up or down


based on demand.

o Load Balancing: Distributes traffic evenly across container


instances.

o Self-healing: Automatically replaces failed containers to


ensure high availability.

o Service Discovery: Simplifies the discovery and connectivity


between services in a distributed environment.

6. Hybrid Approaches: Combining Containers and VMs

In some environments, a hybrid approach using both containers and VMs


is common. For example, running containers inside VMs can provide an
extra layer of isolation while leveraging the benefits of containers for
application deployment.

Conclusion

 Containers: Offer lightweight, fast, and resource-efficient


environments, making them ideal for modern, cloud-native
applications, microservices, and DevOps workflows.

 Virtual Machines: Provide stronger isolation and are suitable for


running legacy systems or when different operating systems need to
coexist.

For most cloud-native applications, containers are the preferred choice,


but VMs are still valuable in specific scenarios requiring stringent isolation
or full operating system environments. The right choice depends on the
specific use case, security requirements, and infrastructure goals.

Microservices architecture is an approach to building software systems


that structures an application as a collection of small, loosely coupled,
independently deployable services. Each service is focused on a specific
business capability and can be developed, deployed, and scaled
independently of other services. This contrasts with the
traditional monolithic architecture, where an application is built as a
single, unified unit.

Microservices address some of the challenges of monolithic architectures


by dividing the system into smaller services that can evolve
independently. This makes it easier to develop, test, deploy, and scale
large, complex applications, especially as they grow.

Key Characteristics of Microservices

1. Independent Deployment: Each microservice can be deployed


independently without impacting the entire system, allowing for
more agile and frequent releases.

2. Loosely Coupled: Services are loosely coupled, meaning they are


independent of each other. They communicate over lightweight
protocols (such as HTTP or messaging queues).

3. Business Capability-Oriented: Each microservice focuses on a


specific business function, such as payment processing, order
management, or inventory tracking.

4. Technology Diversity: Microservices allow teams to choose the


best technology stack for each service (polyglot programming). For
example, one service might be built in Python, while another could
be in Java.

5. Decentralized Data Management: Each microservice can


manage its own database, leading to decentralized and more
flexible data management.

6. Fault Isolation: If one microservice fails, it doesn’t necessarily


impact the entire system, ensuring higher resilience.

Microservices Architecture in Detail

a. Service Decomposition

Microservices architecture begins by decomposing a monolithic


application into smaller, autonomous services. The services are usually
modeled around business domains following the Domain-Driven Design
(DDD) approach. Each microservice typically handles one specific task or
functionality.

For example:

 Order Service: Manages orders and order statuses.

 Payment Service: Handles payment processing and payment


statuses.

 Inventory Service: Manages product stock levels and availability.

Each service is isolated and encapsulates its functionality, which can then
be managed independently. These services communicate with each other
using well-defined APIs.

b. Communication Between Microservices

Microservices often communicate over network protocols, such as:

1. Synchronous Communication (HTTP/REST, gRPC): Services


communicate in real-time using HTTP or gRPC protocols. RESTful
APIs are a common approach for this communication.

Example: The Order Service might send an HTTP request to


the Payment Service to initiate payment processing.

plaintext

Copy code

Order Service --> HTTP Request --> Payment Service

2. Asynchronous Communication (Message Brokers): In more


complex environments, asynchronous communication using
message brokers (e.g., RabbitMQ, Kafka) is preferred. This
decouples services and allows them to process tasks in the
background.

Example: When a new order is placed, the Order Service publishes an


event to a message queue (e.g., Kafka). The Inventory
Service and Payment Service can consume this message to update
inventory or process payments.

plaintext

Copy code

Order Service --> Event Published to Kafka --> Payment Service, Inventory
Service

c. API Gateway

An API Gateway acts as a single entry point for external clients to


interact with multiple microservices. It simplifies client interactions,
manages requests, and can handle cross-cutting concerns such as:

 Authentication and Authorization.

 Rate Limiting.

 Load Balancing.

For example, rather than directly exposing each microservice to external


users, the API Gateway routes requests to the appropriate microservice.
The API Gateway might route a request for user login to the Auth
Service, while an order-related request might be routed to the Order
Service.

plaintext

Copy code

Client --> API Gateway --> [Auth Service, Order Service, Payment Service]

d. Decentralized Data Management

Unlike monolithic systems that rely on a centralized database,


microservices allow for decentralized data management. Each service
can have its own database suited for its needs. This can involve different
database technologies based on service requirements (e.g., relational
databases for one service, NoSQL for another).

For example:

 The Order Service might use a relational database


like PostgreSQL.
 The Inventory Service could use a MongoDB NoSQL database for
better scalability and flexible schema.

plaintext

Copy code

Order Service --> PostgreSQL

Inventory Service --> MongoDB

Payment Service --> MySQL

This approach improves scalability and service independence but comes


with challenges like managing data consistencyacross services (solved
using techniques like Eventual Consistency or Sagas).

e. Service Discovery

In a microservices architecture, services often need to discover and


communicate with each other dynamically. A Service Registry keeps
track of all active service instances and their locations (IP address, port,
etc.). Tools like Consul, Eureka, and Zookeeper manage service
discovery.

For example, when the Order Service needs to communicate with


the Payment Service, it queries the service registry to find the current
address of the Payment Service.

plaintext

Copy code

Order Service --> Service Registry --> Payment Service

f. Containerization and Orchestration

Microservices are often deployed using containers (e.g., Docker).


Containers ensure that each service and its dependencies are packaged
together, making deployment consistent across environments.

To manage multiple containers at scale, microservices use orchestration


tools like Kubernetes or Docker Swarm. These tools manage the
deployment, scaling, and failover of services.

g. Event-Driven Architecture

In microservices, event-driven architecture is commonly used to


decouple services. When a significant event happens in a service, it can
publish that event, which other services listen to and react to.

Example: In an e-commerce system:


1. Order Service places a new order.

2. It publishes an event, OrderCreated.

3. The Inventory Service listens for this event and reduces stock.

4. The Payment Service listens and processes the payment.

plaintext

Copy code

Order Service --> Event (OrderCreated) --> Inventory Service, Payment


Service

Advantages of Microservices Architecture

1. Scalability: Services can be scaled independently, improving


resource utilization. For example, if the Order Service receives
more traffic than the Payment Service, only the Order
Service can be scaled up.

2. Fault Isolation: If one service fails, it doesn’t bring down the entire
system. Other services can continue operating, leading to better
fault tolerance.

3. Agility: Microservices enable teams to develop, test, and deploy


independently. This increases the speed of development and
reduces dependencies between teams.

4. Technology Diversity: Each service can use the technology stack


best suited to its needs. This flexibility allows organizations to adopt
modern technologies without overhauling the entire system.

Challenges of Microservices Architecture

1. Complexity: The number of services increases the complexity of


managing the infrastructure and communication between services.

2. Data Management: Managing distributed data across multiple


services can lead to consistency challenges and require
implementing eventual consistency.

3. Latency: Cross-service communication, especially over a network,


introduces latency. Proper design of service interaction is necessary
to mitigate performance issues.

4. Monitoring and Debugging: Debugging issues in a distributed


system is more challenging compared to monolithic applications.
Distributed tracing and centralized logging become essential.

Example: E-Commerce System in Microservices Architecture


In an e-commerce platform, various business functionalities can be broken
into separate microservices:

1. User Service: Manages user registration, login, and user profiles.

2. Product Service: Handles product details, descriptions, and


inventory.

3. Cart Service: Manages users’ shopping carts.

4. Order Service: Processes orders and tracks order statuses.

5. Payment Service: Manages payment processing, credit card


details, and billing.

6. Notification Service: Sends out notifications (email, SMS) about


order updates.

In this example:

 The User Service and Product Service interact through APIs.

 The Order Service communicates with the Payment Service to


process payments asynchronously using a message broker (e.g.,
Kafka).

 The Notification Service listens for events (e.g., order placed) and
triggers notifications accordingly.

Conclusion

Microservices architecture offers significant benefits in terms of scalability,


agility, and fault tolerance. It’s well-suited for large, complex applications
that need to evolve quickly and scale effectively. However, this comes at
the cost of increased complexity in managing inter-service
communication, deployment, and data consistency.

What is Monolithic Architecture?

A monolithic architecture is a traditional model for designing software


where the entire application is built as a single, cohesive unit. All
components of the application—such as the user interface, business logic,
and data access layer—are tightly coupled and run as a single process.

Characteristics of Monolithic Architecture

 Single Codebase: The entire application exists in a single


codebase, often managed as one project.
 Tight Coupling: All components (UI, database, business logic, etc.)
are tightly coupled, meaning they depend on each other and are
deployed together.

 Single Deployment: The application is deployed as a single


package (e.g., a WAR or JAR file in Java). Any update or change
requires redeploying the entire application.

 Centralized Data Management: The application typically uses a


centralized database shared by all components.

 Simple Communication: All components interact with each other


via in-memory calls, meaning internal communication is fast and
straightforward.

Monolithic Architecture Example

For instance, in an e-commerce application with a monolithic design, all


services such as:

 Product management,

 Order management,

 Inventory,

 Payment processing,

 User authentication, would be bundled together in a single


application.

Whenever a developer makes changes to one part of the system (e.g.,


adding a new feature to the payment system), the entire application must
be rebuilt and redeployed.

plaintext

Copy code

+-----------------------------------------------------+

| User Interface |

+-----------------------------------------------------+

| Business Logic (Product, Payment, Order, etc.)|

+-----------------------------------------------------+

| Data Access Layer (Shared Database) |

+-----------------------------------------------------+

Advantages of Monolithic Architecture


1. Simple Development: Since everything is in one codebase, it’s
easier for developers to work on a single system.

2. Easy to Deploy: Deployment is straightforward because the entire


application is packaged and deployed as one unit.

3. Simple to Test: Testing can be easier because the entire


application runs as a single process.

4. Performance: Intra-process communication is faster than network-


based communication, providing faster performance.

Disadvantages of Monolithic Architecture

1. Lack of Agility: A change in one part of the application requires


redeploying the entire system, slowing down the release cycle.

2. Scalability Issues: Scaling a monolithic application is difficult since


it must be scaled as a whole, even if only one part of the application
is experiencing high demand.

3. High Risk of Failure: If one part of the monolithic system fails, it


can bring down the entire application.

4. Hard to Maintain: As the application grows, it becomes more


difficult to understand, maintain, and update due to tight coupling
between components.

5. Limited Technology Flexibility: A monolithic application typically


uses a single technology stack, making it harder to adopt new
technologies.

2. What is Microservices Architecture?

Microservices architecture is an approach where an application is built


as a collection of small, independent services that run in isolation. Each
microservice focuses on a specific business capability and can be
developed, deployed, and scaled independently.

Characteristics of Microservices Architecture

 Independent Services: Each service represents a specific business


functionality, such as payment processing, inventory management,
or user authentication. These services operate independently of
each other.

 Decentralized Development: Teams can develop each


microservice in isolation, using different programming languages,
frameworks, or tools.
 Independent Deployment: Microservices can be deployed and
updated individually without impacting other services.

 Polyglot Persistence: Each microservice can manage its own


database, leading to more flexible and decentralized data
management.

 Service Communication: Microservices typically communicate


over a network using lightweight protocols such as HTTP/REST,
gRPC, or messaging queues (asynchronous messaging).

Microservices Architecture Example

In a microservices-based e-commerce application, each service—such


as Product Service, Order Service, Payment Service, and Inventory
Service—is developed, deployed, and scaled independently.

plaintext

Copy code

+------------------------------------+ +-------------------------------------+

| Product Service | | Payment Service |

+------------------------------------+ +-------------------------------------+

| Own Database | | Own Database |

+------------------------------------+ +-------------------------------------+

| |

| |

+------------------------------------+ +-------------------------------------+

| Order Service | | Inventory Service |

+------------------------------------+ +-------------------------------------+

| Own Database | | Own Database |

+------------------------------------+ +-------------------------------------+

Each microservice communicates with the others using APIs or message


brokers. For example, when a user places an order, the Order
Service might call the Payment Service to process the payment and
the Inventory Service to update stock levels.

Advantages of Microservices Architecture


1. Independent Deployment: Each service can be updated or
redeployed independently, enabling faster and more frequent
releases.

2. Scalability: Each microservice can be scaled individually based on


its needs, making the system more resource-efficient.

3. Fault Isolation: If one service fails, it doesn’t necessarily bring


down the entire system. Other services can continue to function.

4. Technology Flexibility: Teams can choose different technology


stacks for different microservices based on their requirements,
allowing more flexibility.

5. Agility and Faster Development: Teams can work on different


services concurrently, enabling faster development and deployment
cycles.

6. Improved Maintainability: Smaller codebases for each service are


easier to maintain and evolve than a single monolithic codebase.

Disadvantages of Microservices Architecture

1. Complexity: Managing many independent services can become


complex, especially in terms of service discovery, orchestration, and
communication.

2. Distributed Data Management: Maintaining data consistency


across services can be challenging, especially with decentralized
databases.

3. Inter-Service Communication Overhead: Network-based


communication between services introduces latency, error handling
complexity, and potential performance bottlenecks.

4. Monitoring and Debugging: Monitoring and debugging a


distributed system is more difficult than with a monolithic
application.

5. Deployment Complexity: Managing deployments, scaling, and


versioning for multiple services requires sophisticated tools and
processes.

3. Detailed Comparison: Monolithic vs. Microservices


Microservices
Aspect Monolithic Architecture
Architecture

Collection of independent
Structure Single unified application
services

One codebase for the entire Separate codebases for each


Codebase
application microservice

Entire application deployed Each service is deployed


Deployment
as a single package independently

Each service can be scaled


Entire application is scaled
Scaling independently based on its
as a single unit
needs

Fault Failure in one part can Failure in one service doesn't


Tolerance affect the entire application impact others; fault isolation

Allows use of different


Technology Typically uses a single
technologies for different
Stack technology stack
services (polyglot)

Network-based
Communicatio
In-memory calls (fast) communication (HTTP, gRPC,
n
messaging)

Data Decentralized databases


Centralized database
Management (polyglot persistence)

More complex to test due to


Easier to test as a single
Testing multiple independent
application
services

Development Slower as the codebase Faster for small teams


Speed grows working independently

Simple to start, but grows More complex to manage due


Complexity in complexity as the to multiple services and their
application scales interactions

Suitable for small to


Ideal for large, complex, and
Use Case medium-sized applications
scalable applications
with fewer scaling needs

Less flexible; changes Highly flexible; each service


Agility require redeploying the can be updated and
entire system deployed independently
Microservices
Aspect Monolithic Architecture
Architecture

Higher latency due to


Low latency due to in-
Latency network communication
memory calls
between services

4. Use Cases: Monolithic vs. Microservices

When to Use Monolithic Architecture

 Small and Simple Applications: Monolithic architecture is more


appropriate for small applications with straightforward requirements
where high scalability and agility are not needed.

 Tight Deadlines: Monolithic architecture is easier to develop in the


short term due to the simplicity of managing a single codebase and
deployment pipeline.

 Startups or MVPs: For companies building a minimum viable


product (MVP), monolithic applications are often a better choice due
to faster initial development.

When to Use Microservices Architecture

 Large, Complex Applications: Microservices architecture is ideal


for large, complex applications where different parts of the system
require independent scaling and faster development cycles.

 High Scalability: Microservices are suited for applications that


need to scale efficiently. Each service can be scaled horizontally
based on its specific needs.

 Rapid Development and Deployment: Microservices are beneficial for


organizations that want to frequently release new features and updates with minimal
downtime.

Conclusion
Both monolithic and microservices architectures have their own strengths and
weaknesses. Monolithic architectures are simpler to develop, deploy, and maintain for small
applications but become harder to manage as the application grows. Microservices
architectures, on the other hand, offer greater flexibility, scalability, and resilience for large-
scale systems but come with the trade-offs of increased complexity and higher operational
overhead.
Ultimately, the choice between the two architectures depends on the specific requirements of
the project, including factors like scalability, team size, complexity, and time-to-market
constraints.

You might also like