SOA Notes
SOA Notes
Updated
15th Apr 2024
Table of Contents
SOA Syllabus 4
Unit 1: Introduction to Service-Oriented Architecture 4
Unit 2: SOA Design and Modeling 4
Unit 3: SOA Implementation Technologies 4
Unit 4: Security and Governance in SOA 5
Unit 5: SOA Emerging Trends 5
Lab Exercises 5
Reference Books 6
Terminology 7
Unit-1 Introduction to Service Oriented Architecture 8
Motivations of SOA 8
What is SOA 10
Key Characteristics of SOA 11
SOA Components 12
Evolution and Historical Context of SOA 13
Early Web Services 14
E-commerce Integration - Amazon.com 15
Travel Booking System - Expedia.com 15
Enterprise Resource Planning - SAP ERP 16
Emergence of SOA Standards 16
Adoption in Industry 17
Benefits and Challenges of SOA 17
Benefits of SOA 17
Contemporary Trends of SOA 19
Cloud Computing and SOA 20
Serverless Computing and SOA 21
Unit-2 SOA Design and Modeling 23
Service Design Principles and Patterns 23
Service Coupling 23
Service Cohesion 24
Applying Coupling and Cohesion to SOA 24
Design for Change 26
Service Contract Design and Management 26
Contract-First Design 27
Versioning and Evolution 27
Interface Definition Languages (IDLs) 28
Definition of IDLs 28
Features of IDLs 28
Application of IDLs in SOA 29
2
Protocol Buffers (protobuf) 29
Apache Thrift 30
Designing for Scalability and Resilience 31
Load Balancing 32
Fault Tolerance 32
Circuit Breaker Pattern 32
Unit-3 SOA Implementation Techniques 34
Web Services Standards 34
Simple Object Access Protocol 34
Representational State Transfer (REST) 34
Graph QL 35
Examples and Code Snippets 35
Microservices Architecture and its Relationship with SOA 36
Decentralized Data Management: 36
Independent Deployment: 37
Infrastructure Automation 38
Relationship with SOA 39
Containerization and Orchestration 40
Containerization 40
Docker Container 40
Kubernetes Orchestration 42
Service Mesh Technologies 43
Event-Driven Architecture 44
Event Sourcing 44
Command Query Responsibility Segregation (CQRS) 44
Event-Driven Messaging Systems 45
API Management and Governance 45
API Design Principles 46
Developer Portals 46
Rate Limiting and Quotas 46
Lab Exercises - Solution 48
Exercise 1: Overview of SOA: Implement a REST Web Service 48
REST Web Service - Python Implementation (GET and POST Methods) 48
REST Web Service - Spring Boot (Java) Implementation 50
Exercise 2: Principles and Concepts of SOA 53
Pub-Sub: Demonstrate a Publisher-Subscriber message exchange using RabbitMQ. 53
RabbitMQ tutorial - "Hello world!" 54
Exercise 3: Demonstrate a Content Delivery Network (CDN) 54
Design a simple Content Delivery Network (CDN) using Python with focus on distributing
content efficiently to users from multiple edge servers 55
Exercise 4: Build a AI-driven Customer Support service and integrate into a SOA application
57
Design a simple AI driven Customer support SOA service using ML models and
integrate it into a SOA. 58
3
Reference Articles 60
4
SOA Syllabus
5
- Event Sourcing, Command Query Responsibility Segregation (CQRS)
- Event-Driven Messaging Systems
Lab Exercises
Exercise 1: Overview of Service-Oriented Architecture
6
- Code Example: Develop a simple web service using a framework like Flask (Python),
Spring Boot (Java), or Express (Node.js). Demonstrate how clients can consume this
service to retrieve or manipulate data.
Reference Books
1. Service-Oriented Architecture: Concepts, Technology and Design by Thomas Erl
7
Terminology
8
Unit-1 Introduction to Service Oriented Architecture
Motivations of SOA
9
NPCI Transactions: https://www.npci.org.in/statistics/monthly-metrics
10
Source:https://paytm.com/blog/investor-relations/how-paytm-achieved-operational-profit
ability/
What is SOA
12
flexibility and agility in system design, as services can be modified or replaced without
impacting other components.
3. Flexible: SOA is inherently flexible, allowing for the composition and recomposition
of services to meet changing business requirements. Services can be combined and
orchestrated in various ways to create new functionalities, enabling organizations to
adapt to evolving needs.
SOA Components
13
Evolution and Historical Context of SOA
Early web services emerged in the late 1990s and early 2000s as a means of enabling
interoperability and communication between disparate systems over the Internet.
15
● Travel Booking Systems: Travel agencies use web services to integrate with
airline reservation systems, allowing customers to search for flights, book tickets
and receive real-time updates on flight availability.
● Enterprise Resource Planning (ERP): Organizations utilise web services to
integrate ERP systems with third-party vendors for tasks such as inventory
management, supply chain optimization and financial reporting.
● Use Case Scenario: In the mid-90s, Amazon.com began its journey as an online
bookstore, but it quickly evolved into a platform selling a wide range of products.
To handle the complex nature of its e-commerce operations, Amazon likely
employed service-oriented principles, even before SOA was formally recognized.
● SOA Characteristics: Amazon's platform likely utilised loosely coupled services
to handle various aspects of its operations, such as inventory management, order
processing, payment processing and customer relationship management (CRM).
These services would have communicated with each other using standardized
protocols such as HTTP and XML
● Use Case Scenario: Expedia, founded in the late 1990s, revolutionized the travel
industry by offering an online platform for booking flights, hotels, rental cars and
vacation packages. To provide a seamless booking experience to its users,
Expedia likely employed SOA principles in its system architecture.
● SOA Characteristics: Expedia's platform likely consisted of various services
responsible for different functions, such as flight search, hotel booking, payment
processing and itinerary management. These services would have been loosely
coupled, allowing for flexibility and scalability. For example, the flight search
service could communicate with airline reservation systems via standardized
interfaces, such as SOAP or XML over HTTP.
16
Enterprise Resource Planning - SAP ERP
● Use Case Scenario: SAP, a leading provider of ERP software, has been in the
industry since the early 1970s. While its early systems may not have fully
embraced SOA principles, SAP likely transitioned towards a more
service-oriented approach in the late 1990s and early 2000s to address the
growing complexity of enterprise operations.
● SOA Characteristics: SAP's ERP system would consist of various modules or
services responsible for different business functions, such as finance, human
resources, supply chain management and customer relationship management.
These services would have been designed to be reusable and interoperable,
allowing organizations to customize their ERP implementations based on their
specific needs.
● Key Standards:
● SOAP (Simple Object Access Protocol): A protocol for exchanging
structured information in the implementation of web services.
● WSDL (Web Services Description Language): A standard for describing
the functionality of web services, facilitating their discovery and invocation.
● XML (eXtensible Markup Language): A markup language used for
encoding documents in a format that is both human-readable and
machine-readable.
● Importance:
● SOA standards provided a common framework for designing,
implementing and consuming web services.
● They promoted interoperability and reusability by establishing common
protocols and formats for communication between systems.
17
Adoption in Industry
● Drivers for Adoption:
● The adoption of SOA in industry was driven by the need for more flexible,
scalable and interoperable solutions to address complex business
challenges.
● Organizations sought to modernize their IT infrastructure and improve
agility by adopting SOA principles.
● Benefits of Adoption:
● SOA adoption enabled organizations to achieve greater flexibility and
agility in responding to changing business requirements.
● It promoted interoperability between disparate systems, allowing for
seamless communication and integration across the enterprise.
● Challenges:
● Despite its benefits, SOA adoption presented challenges such as cultural
resistance to change, complexity in implementation and concerns about
Return of Investment.
● Organizations faced hurdles in transitioning from traditional monolithic
architectures to a more service-oriented approach.
Benefits of SOA
● Business Agility:
● SOA enables organizations to respond quickly to changing market
conditions and business requirements.
● Real Use Case Netflix: Netflix employs SOA to continuously innovate its
streaming platform. With SOA, Netflix can rapidly introduce new features,
personalize recommendations and scale its infrastructure to accommodate
fluctuations in viewer demand. For example, Netflix's recommendation
service analyzes user preferences in real-time, leveraging microservices
to deliver personalized content recommendations instantly.
● Interoperability:
18
● SOA promotes interoperability by standardizing communication protocols
and data formats.
● Real Use Case: Salesforce.com: Salesforce.com leverages SOA to
integrate its cloud-based CRM platform with various third-party
applications and services. Through standardized APIs and web services,
Salesforce enables seamless data exchange between its CRM system
and other business systems, such as marketing automation tools, ERP
systems and customer support platforms.
Challenges in Implementation
19
Realizing the Benefits
● Key Characteristics:
20
● Service Decomposition: Applications are decomposed into smaller,
independently deployable services, each responsible for a specific
business capability.
● Decentralized Data Management: Each service manages its own
database or data store, enabling greater autonomy and scalability.
● Polyglot Persistence: Services can use different databases or data
storage technologies based on specific requirements.
● Infrastructure Automation: Microservices rely on automation for
deployment, scaling and monitoring to ensure resilience and reliability.
● Real Use Case: Netflix
● Netflix transitioned from a monolithic architecture to a
microservices-based architecture to support its rapid growth and global
expansion.
● Each microservice at Netflix handles a specific function, such as user
authentication, content recommendation, billing and streaming.
● This architecture enables Netflix to scale its services independently,
deploy updates faster and deliver personalized experiences to millions of
users worldwide.
● Key Characteristics:
● On-Demand Self-Service: Users can provision and manage computing
resources, such as servers and storage, without human intervention.
● Resource Pooling: Cloud providers pool and dynamically allocate
resources to multiple users, optimizing resource utilization and scalability.
● Pay-Per-Use Billing: Users pay only for the resources they consume,
enabling cost-effective and scalable solutions.
● Scalability and Elasticity: Cloud services can scale up or down based on
demand, ensuring performance and availability.
21
● Real Use Case: Airbnb
● Airbnb leverages cloud computing services, such as Amazon Web
Services (AWS), to power its online marketplace for lodging and tourism
experiences.
● By using cloud infrastructure, Airbnb can quickly scale its services to
accommodate spikes in demand during peak booking seasons or events.
● Additionally, cloud-based analytics and machine learning services enable
Airbnb to personalize search results, recommend listings and optimize
pricing for hosts.
Serverless computing is a cloud computing model where cloud providers manage the
infrastructure, dynamically allocating resources as needed and users only pay for the
compute resources consumed by their applications.
● Key Characteristics:
● No Server Management: Users do not need to provision, manage, or
maintain servers or infrastructure, allowing for faster development and
deployment.
● Event-Driven Architecture: Serverless applications are event-driven and
respond to triggers or events, such as HTTP requests, database changes,
or messages from queues.
● Auto-Scaling: Serverless platforms automatically scale resources based
on demand, ensuring high availability and performance without user
intervention.
● Pay-Per-Use Billing: Users are billed based on the actual resources
consumed by their applications, offering cost savings and efficiency.
● Real Use Case: Lyft
● Lyft utilizes serverless computing for its backend infrastructure to handle
millions of ride requests and data-intensive operations in real-time.
● By adopting a serverless architecture on AWS Lambda, Lyft can
dynamically scale its backend services in response to user demand,
ensuring low-latency responses and optimal performance.
22
● Serverless computing enables Lyft to focus on building and improving its
core ride-sharing platform without worrying about managing servers or
infrastructure.
23
Unit-2 SOA Design and Modeling
Service Coupling
Definition: Service Coupling refers to the degree of interdependence between any two
business processes or services within a system.
● Explanation:
● Weak coupling allows services to evolve independently, reducing the risk
of unintended consequences when modifications or updates are made.
● Services with weak coupling can adapt more easily to changes in
business requirements, ensuring that adjustments in one part of the
system do not propagate unexpectedly to other interconnected services.
● Example:
● A service employing standardized interfaces and protocols can interact
with other services more loosely, minimizing the impact of changes in one
service on others.
● Example: Consider an e-commerce platform where a "Checkout Service"
encapsulates functionalities such as processing payment, updating
inventory and sending order confirmation emails. This service
demonstrates strong cohesion by focusing on a cohesive set of operations
related to completing the checkout process.
24
Service Cohesion
Definition: Service Cohesion refers to the degree of functional relatedness and focus of
operations within a service.
Preferable State: Strong cohesion is preferred in SOA, indicating that a service should
encapsulate closely related and well-defined functionalities.
● Explanation:
○ Strong cohesion ensures that a service encapsulates a well-defined and
closely related set of functionalities, enhancing clarity, maintainability and
usability.
○ Cohesive services promote reusability and contribute to a modular and
extensible architecture.
● Example:
○ A service responsible for order processing should encapsulate
functionalities such as order validation, payment processing and inventory
management, exhibiting strong cohesion.
25
GET, POST, PUT and DELETE methods, leading to more cohesive service
interactions and easier integration across the system.
● Creating understandable and maintainable Web service orchestrations requires
considering the cohesion of services being orchestrated.
Service Granularity
Service design principles such as cohesion, granularity and design for change are
fundamental to creating effective and maintainable service-oriented architectures. By
adhering to these principles, organizations can develop robust and adaptable systems
capable of meeting the dynamic needs of the business environment.
26
Design for Change
This section explores critical aspects of designing and managing service contracts in a
Service-Oriented Architecture (SOA).
A service contract serves as the interface between service providers and consumers,
defining the obligations, responsibilities and expectations of both parties. This enables
creation of interoperable and extendable service contracts that facilitate seamless
integration and collaboration within distributed systems.
27
Contract-First Design
Example: A cloud storage provider (e.g. AWS S3, DropBox) introduces a new version of
its API to support additional features and improve performance. To ensure backward
compatibility, the provider maintains support for the previous API version while allowing
clients to migrate to the new version at their own pace. Through versioning and effective
communication of changes, the provider minimizes disruptions for existing clients and
facilitates the adoption of new features by offering clear migration paths.
28
Interface Definition Languages (IDLs)
Interface Definition Languages (IDLs) are formal languages used to describe the
interfaces of software components, enabling communication and interaction between
distributed systems. This lecture explores the role of IDLs in service-oriented
architectures (SOAs), their key features and their application in modern software
development.
Definition of IDLs
Features of IDLs
Types of IDLs
29
● Data-Oriented IDLs: Primarily used for defining data structures and
messages exchanged between systems. Examples include Google
Protocol Buffers, Apache Thriftand Apache Avro.
30
● Example: In a distributed messaging system, Protocol Buffers is used to
define message formats for communication between microservices,
ensuring efficient data serialization and deserialization.
Apache Thrift
31
Designing for Scalability and Resilience
Designing for Scalability and Resilience is essential for building robust and adaptable
service-oriented architectures (SOAs) that can handle varying workloads and maintain
availability under challenging conditions. This section describes key design principles
and patterns, including Load Balancing, Fault Tolerance and the Circuit Breaker Pattern,
along with modern application examples to illustrate their practical implementation.
32
Load Balancing
Fault Tolerance
● Definition: The Circuit Breaker Pattern is a design pattern used to handle faults
and failures in distributed systems by temporarily suspending requests to a failing
service.
● b: In SOA, circuit breakers monitor the health of downstream services and prevent
cascading failures by quickly detecting and isolating faulty components.
33
● Example: In a microservices architecture for a social media platform, a circuit
breaker is implemented in the Notification Service to prevent excessive retries
when sending notifications to users. If the Notification Service experiences a high
error rate, the circuit breaker opens, temporarily halting requests to the service
and preventing overload.
34
Unit-3 SOA Implementation Techniques
35
Graph QL
● GraphQL is a query language and runtime for APIs developed by Facebook.
● It allows clients to specify exactly what data they need, enabling more efficient
and flexible data retrieval compared to traditional REST APIs.
● With GraphQL, clients can request multiple resources in a single query and
receive only the data they ask for, reducing over-fetching and under-fetching of
data.
● GraphQL APIs are introspective, meaning they expose a schema that describes
the types of data available and the operations that can be performed.
SOAP:
● Example: Integrating a payment gateway API into an e-commerce platform.
● Code Snippet: SOAP
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:web="http://www.example.com/webservice">
<soapenv:Header/>
<soapenv:Body>
<web:ProcessPayment>
<web:Amount>100.00</web:Amount>
<web:CardNumber>1234567890123456</web:CardNumber>
<!-- Additional Payment Details -->
</web:ProcessPayment>
</soapenv:Body>
</soapenv:Envelope>
REST:
● Real-life Example: Retrieving weather data from a public API.
● Code Snippet (Python using requests library)
import requests
url = "https://api.weather.com/data"
params = {"city": "Bangalore", "format": "json"}
36
response = requests.get(url, params=params)
weather_data = response.json()
print(weather_data)
GraphQL:
● Example: Fetching user profile data from a social media platform API.
● Code Snippet (GraphQL query)
query {
user(id: "[email protected]") {
id
name
email
posts {
id
title
content
}
}
}
● In microservices architecture, each service manages its own data store, which is
often optimized for the service's specific requirements.
● This decentralized approach to data management allows services to be more
autonomous and reduces dependencies between services.
● Services can choose the most suitable data storage technology for their needs,
such as relational databases, NoSQL databases, or in-memory caches.
37
Example:
Code Snippet
class UserService:
def __init__(self, db):
self.db = db
# Example usage
user_db = UserDatabase()
user_service = UserService(user_db)
user_service.create_user(user_data)
Independent Deployment:
Example:
In a retail application, the product catalog service can be updated with new product
information independently of the checkout service. This allows the product team to
38
release updates to the catalog without waiting for the checkout team, enabling faster
innovation.
Code Snippet
Infrastructure Automation
Example:
Code Snippet:
Example
Code Snippet
@Autowired
private TransactionService transactionService;
@PostMapping("/process")
public ResponseEntity<Transaction>
processTransaction(@RequestBody TransactionRequest request) {
Transaction transaction =
transactionService.processTransaction(request);
return ResponseEntity.ok(transaction);
}
}
40
Containerization and Orchestration
Containerization
Source: https://www.xenonstack.com/insights/containerization
Docker Container
41
Source: Alexsoft
Reference: https://www.xenonstack.com/insights/containerization
● Pros
○ Portability - no dependency on hardware, containers (dockers) abstracts
running of application on any host
○ Lightweight - contains only application specific requirements and no
unnecessary OS overhead, keep it lightweight
○ Speed - more faster and efficient in application bring up
○ Cost-effective - cost of running containers is much lower than running
virtual machines
● Cons
○ Security - vulnerability of container engine and poor access control has
associated risks
○ Manageability - managing large number of containers is challenging
○ Monitoring - needs a good monitoring system for effective maintenance
and troubleshooting.
42
Example: A web application running in a Docker container:
# Dockerfile
FROM python:3.9-slim
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
Kubernetes Orchestration
43
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp
spec:
replicas: 3
selector:
matchLabels:
app: webapp
template:
metadata:
labels:
app: webapp
spec:
containers:
- name: webapp
image: my-webapp:latest
ports:
- containerPort: 80
# destination-rule.yaml
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: default-mtls
spec:
host: "*.default.svc.cluster.local"
44
trafficPolicy:
tls:
mode: ISTIO_MUTUAL
Event-Driven Architecture
Event-Driven Architecture (EDA) is an architectural pattern where the production,
detection, consumption, and reaction to events are central to the design. EDA enables
decoupled, scalable, and responsive systems by promoting loose coupling between
components and allowing them to communicate asynchronously through events.
Event Sourcing
producer = KafkaProducer(bootstrap_servers='localhost:9092',
value_serializer=lambda v:
json.dumps(v).encode('utf-8'))
46
provide a positive developer experience. It encompasses various aspects such as API
design, documentation, security, versioning, and usage policies.
Designing an API for a weather service that provides endpoints like /weather/{city} to
retrieve weather information for a specific city and /forecast/{city} to get a weather
forecast.
Developer Portals
The GitHub Developer Portal offers comprehensive documentation, tutorials, and API
reference guides for developers integrating with GitHub's APIs.
● Rate Limiting and Quotas control the number of requests an API consumer can
make within a specific time frame to prevent abuse and ensure fair usage.
● Rate limits are typically enforced based on factors such as API keys, user
authentication, IP addresses or subscription plans.
47
Source: https://systemsdesign.cloud/SystemDesign/RateLimiter
Example: Implementing rate limiting for a social media API to restrict users to 1000
requests per hour to prevent spamming and ensure server stability.
app = Flask(__name__)
limiter = Limiter(
app,
key_func=get_remote_address,
default_limits=["1000 per hour"]
)
redis = Redis(host='localhost', port=6379)
@app.route('/api/resource')
@limiter.limit("10 per minute")
def get_resource():
return jsonify({'data': 'Resource data'})
if __name__ == '__main__':
app.run(debug=True)
48
Lab Exercises - Solution
Prerequisites:
● Python Installation version 3.X
● Install Flask - use pip or pip3 based on your installation of python
● Update host firewall (if configured) to allow
● curl command
app = Flask(__name__)
# Sample data
books = [
{"id": 1, "title": "Book 1", "author": "Author 1"},
{"id": 2, "title": "Book 2", "author": "Author 2"},
{"id": 3, "title": "Book 3", "author": "Author 3"}
]
49
# Endpoint to get a specific book by id
@app.route('/books/<int:id>', methods=['GET'])
def get_book(id):
book = next((book for book in books if book['id'] == id),
None)
if book:
return jsonify(book)
else:
return jsonify({"error": "Book not found"}), 404
if __name__ == '__main__':
app.run(debug=True)
```
python app.py
The Flask application should be running. Invoke the service using You can consume
this service using HTTP client such as curl
50
curl -X POST -H "Content-Type: application/json" -d
'{"title":"New Book","author":"New Author"}'
http://localhost:5000/books
Alternate command
// Create pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
import org.springframework.boot.SpringApplication;
import
org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
51
@SpringBootApplication
@RestController
public class Application {
@GetMapping("/books")
public List<Book> getBooks() {
return books;
}
@GetMapping("/books/{id}")
public Book getBook(@PathVariable int id) {
Optional<Book> result = books.stream().filter(book ->
book.getId() == id).findFirst();
return result.orElse(null);
}
@PostMapping("/books")
public Book addBook(@RequestBody Book book) {
book.setId(books.size() + 1);
books.add(book);
return book;
}
}
class Book {
private int id;
private String title;
private String author;
Run the application. Spring Boot will automatically start an embedded Tomcat server
on port 8080 by default. You can now access the service using “curl” HTTP client.
curl http://localhost:8080/books
curl http://localhost:8080/books/1
53
Exercise 2: Principles and Concepts of SOA
- Code Example: Implement a basic service demonstrating loose coupling by using
asynchronous messaging (e.g., RabbitMQ or Kafka). Create a publisher service that
sends messages to a message broker and a consumer service that receives and
processes these messages independently.
Terminology
Prerequisites:
● Install Erlang
○ Erlang is a programming language developed by Ercisson in 1986. Erlang
is the programming language used to code WhatsApp
○ RabbitMQ is written in Erlang
○ Erlang Installation:
■ Windows Installer:
https://github.com/erlang/otp/releases/download/OTP-26.2.3/otp_wi
n64_26.2.3.exe
54
● Install RabbitMQ
○ https://www.rabbitmq.com/docs/install-windows#installer
● This consists of two programs in Python; a producer (sender) that sends a single
message and a consumer (receiver) that receives messages and prints them out.
It's a "Hello World" of messaging.
● https://www.rabbitmq.com/tutorials/tutorial-one-python
55
Exercise 3: Demonstrate a Content Delivery Network (CDN)
Design a simple Content Delivery Network (CDN) using Python with focus
on distributing content efficiently to users from multiple edge servers
Tech Stack:
# edge_server.py
app = Flask(__name__)
@app.route('/content/<path:path>')
def serve_content(path):
content_dir = 'content'
file_path = os.path.join(content_dir, path)
return send_file(file_path)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
# load_balancer.py
app = Flask(__name__)
56
@app.route('/')
def load_balancer():
# randomly select one of the servers
selected_server = random.choice(edge_servers)
return redirect(selected_server)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)
python edge_server.py
python load_balancer.py
Sample Results:
● When accessing content through the load balancer, you'll notice that the requests are
redirected to different edge servers randomly, demonstrating load balancing.
● Each time you refresh the page, the image file (image.jpg) will be served from a different
edge server, showcasing content distribution.
● You can add more content to the content directory and access them through the CDN to
observe the distribution of different content files.
This basic setup demonstrates the concept of a Content Delivery Network (CDN) using Python and
Flask, focusing on load balancing and content distribution among multiple edge servers.
57
Exercise 4: Build a AI-driven Customer Sentiment analysis service
To build a simple AI-driven service using machine learning models and integrate it into a
Service-Oriented Architecture (SOA), you can follow these steps and use the following
tools and code snippets:
app = Flask(__name__)
nlp = pipeline("sentiment-analysis")
@app.route("/analyze_sentiment", methods=["POST"])
def analyze_sentiment():
data = request.json
text = data["text"]
result = nlp(text)[0]
return jsonify({"text": text, "sentiment": result["label"],
"confidence": result["score"]})
58
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
Output:
{
"text": "I like this product! It's awesome.",
"sentiment": "POSITIVE",
"confidence": 0.9998
}
Output:
59
{
"text": "This product is bad! Don’t buy it.",
"sentiment": "NEGATIVE",
"confidence": 0.9985
}
Reference Articles
60