Tale of Software Architect(ure): Part 12 (Service Oriented Architecture)
Story:
In a kingdom called GanjamLand, the king struggled to manage the ever-growing needs of his people due to a massive, complicated infrastructure or large arrangement where every change disrupted the entire system. A wise architect proposed a new approach: instead of expanding the complicated arrangement, they built a village of small, independent houses, each responsible for one task—such as handling payments, managing inventory, or sending notifications. These houses worked together but could grow or change independently without affecting others. This new system, known as Service-Oriented Architecture (SOA), made the kingdom more efficient, adaptable, and easier to maintain.
Service Oriented Architecture:
Service-Oriented Architecture (SOA) is a architecture pattern used in software development where services are provided to other components by application components through a network, typically via a communication protocol such as REST or gRpc. In SOA, different services work together to form a distributed application. Each service is a self-contained unit that performs a specific function and can be reused by different applications or systems.
Components of SOA
There are typically four main components in SOA:
Context
Modern businesses and enterprises often rely on complex systems that involve numerous functionalities. Whether it's an e-commerce platform, a banking system, or a healthcare network, companies need systems that handle multiple tasks, such as processing transactions, managing customer data, and integrating third-party services.
Traditionally, these systems were developed as monolithic applications where all features and functionalities were tightly integrated into a single codebase. As businesses grow, these monolithic systems face several challenges:
Problem
Organizations need to:
The challenge is to create a system that is modular, scalable, flexible, and maintainable, while also ensuring that all parts of the system can communicate seamlessly across different platforms.
Solution
SOA addresses these problems by dividing the system into independent, loosely coupled services that communicate over a network. Each service is a self-contained unit that performs a specific business function and can be reused or scaled independently.
Key characteristics of the solution:
Example Solution:
Context: A growing e-commerce platform needs to handle a variety of functions like browsing products, processing payments, managing inventory, and shipping orders.
Problem: The current monolithic system struggles to keep up with demand. When traffic increases (e.g. during sales), the payment system overloads, causing the entire platform to slow down. Furthermore, updating one part of the system often introduces bugs in other areas, leading to frequent downtime.
Each of these services can now be scaled independently, making the system more robust and flexible. The platform can also integrate third-party shipping services or payment gateways with ease, thanks to SOA’s emphasis on interoperability.
Pseudocode:
# This is the Order Service that orchestrates the process of placing an order.
class OrderService:
function placeOrder(orderDetails):
# 1. Check product availability by calling Product Service
productAvailable = ProductService.checkAvailability(orderDetails.productId)
if not productAvailable:
return "Product is out of stock"
# 2. Process payment by calling Payment Service
paymentSuccessful = PaymentService.processPayment(orderDetails.paymentDetails)
if not paymentSuccessful:
return "Payment failed"
# 3. Reduce the stock by calling Inventory Service
inventoryUpdated = InventoryService.reduceStock(orderDetails.productId, orderDetails.quantity)
if not inventoryUpdated:
return "Failed to update inventory"
# 4. Send confirmation email by calling Notification Service
NotificationService.sendEmail(orderDetails.customerEmail, "Order Confirmed", "Your order has been placed successfully.")
return "Order placed successfully"
# Product Service: Checks if the product is in stock
class ProductService:
function checkAvailability(productId):
# Query the product database to see if stock is available
stock = ProductDatabase.getStock(productId)
if stock > 0:
return true
else:
return false
# Payment Service: Processes the payment
class PaymentService:
function processPayment(paymentDetails):
# Interact with payment gateway (e.g., Stripe, PayPal)
paymentResponse = PaymentGateway.process(paymentDetails)
if paymentResponse.success:
return true
else:
return false
# Inventory Service: Reduces stock once the order is placed
class InventoryService:
function reduceStock(productId, quantity):
stock = ProductDatabase.getStock(productId)
if stock >= quantity:
ProductDatabase.updateStock(productId, stock - quantity)
return true
else:
return false
# Notification Service: Sends confirmation email to customer
class NotificationService:
function sendEmail(toEmail, subject, body):
# Send email using an email service provider
EmailProvider.send(toEmail, subject, body)
Summary:
Service-Oriented Architecture (SOA) is an architectural style that enables the development of scalable, reusable, and loosely coupled services. It enhances system flexibility, maintainability, and interoperability, making it suitable for complex and large-scale enterprise applications. Despite the challenges of managing security, complexity, and governance, SOA continues to be a critical architecture for enterprises looking to build systems that can adapt to changing business needs.