Tuesday

Software Architecture



Introduction to Software Architecture:

Software architecture is a critical discipline in software engineering that deals with the high-level structure and organization of a software system. It provides a blueprint for designing and building software, defining how components interact, and determining the system's behaviour. Software architecture plays a vital role in achieving the desired quality attributes, such as scalability, performance, security, and maintainability.


Definition of Software Architecture:

Software architecture refers to the fundamental structure of a software system, which comprises its components, their relationships, and their interactions. It defines the system's organization, making it easier to understand, develop, and maintain. The architecture influences the system's quality attributes and helps stakeholders make informed decisions throughout the development process.


Architectural Qualities:

Architectural qualities, also known as architectural attributes or non-functional requirements, are characteristics that determine a software system's quality and performance. Some common architectural qualities include:

- Scalability: The ability to handle increased workloads by adding resources.

- Performance: How efficiently the system responds to user actions.

- Reliability: The system's ability to function correctly and consistently.

- Security: Protecting the system from unauthorized access and vulnerabilities.

- Maintainability: The ease with which the system can be modified, repaired, or extended.


Identifying Architectural Qualities:

Identifying architectural qualities involves understanding the stakeholders' needs, project requirements, and constraints. For example, to ensure high security, you may employ encryption and access controls. To achieve scalability, you might use distributed computing or microservices.


Basic Architecture Patterns:

Architecture patterns are templates that address common architectural challenges. Some foundational patterns include:

- Monolithic Architecture: A single-tiered architecture where all components are tightly integrated.

- Layered Architecture: Components are organized into horizontal layers, such as presentation, business logic, and data access.


                        image credit: researchgate


Monolithic Architecture:

In a monolithic architecture, all components are tightly coupled into a single, self-contained unit. It simplifies development but may hinder scalability and maintenance.


Layered Architecture:

A layered architecture divides the application into layers, each responsible for a specific aspect, such as presentation, business logic, and data access. This separation enhances maintainability and allows for changes in one layer without affecting others.


Client-Server Architecture:

The client-server architecture separates the application into clients and servers, where clients request services from servers. Variations include two-tier, three-tier, and n-tier architectures. Common communication protocols include HTTP and TCP/IP.


The client-server model is a distributed system architecture in which multiple clients request and receive a service from a central server. The client is a software application that requests a service from the server. The server is a software application that provides a service to the client.


The client-server model is used in a wide variety of applications, including web browsers, email clients, and file transfer applications.


Variations of the client-server model


There are many variations of the client-server model, including:

Peer-to-peer (P2P) networks: In a P2P network, there is no central server. Instead, all of the clients act as both clients and servers. P2P networks are often used for file sharing and distributed computing.

Three-tier architecture: In a three-tier architecture, the client, server, and database are separated into three distinct layers. This makes it easier to develop and maintain the application, as each layer can be developed and maintained independently. Three-tier architectures are often used in enterprise applications.

N-tier architecture: In an N-tier architecture, the application is divided into multiple layers, each of which performs a specific function. N-tier architectures are often used in complex applications, such as e-commerce websites and online banking applications.


Communication protocols


Communication protocols are the rules that govern how data is exchanged between two or more devices. There are many different communication protocols, but two of the most common are HTTP and TCP/IP.


HTTP (Hypertext Transfer Protocol): HTTP is the protocol used to transfer web pages and other resources over the internet.

TCP/IP (Transmission Control Protocol/Internet Protocol): TCP/IP is a suite of protocols that enables devices to communicate over the Internet.


Practical examples

Here are some practical examples of the client-server model:


Web browsers and web servers: When you visit a website, your web browser sends a request to the web server for the website's HTML, CSS, and JavaScript files. The web server then sends a response back to your web browser with the requested files.

Email clients and mail servers: When you send an email, your email client sends a request to the mail server to send the email. The mail server then sends the email to the recipient's mail server.

File transfer applications and file servers: When you transfer a file using a file transfer application, the application sends a request to the file server to transfer the file. The file server then transfers the file to the application.

The client-server model is a very versatile and powerful architecture that can be used to develop a wide variety of applications.


Practical Examples:

A classic example is a web application where a user's browser (client) communicates with a web server (server) over HTTP to retrieve web pages.


Microservice Architecture Pattern:

Microservices are small, independently deployable services that work together to create a complete application. They provide flexibility and scalability by allowing individual services to be developed, deployed, and scaled independently.

Introduction to Microservices:

Microservices break down a system into small, self-contained units that can be developed and scaled independently.

Advantages and Challenges:

Advantages include improved scalability, fault isolation, and flexibility. Challenges include managing a larger number of services.

Design Principles and Best Practices:

Key principles include designing services around business capabilities, using containers for deployment, and employing automated testing and monitoring.

Event-Driven Architecture:

Event-driven architecture is based on the concept of events, which trigger reactions in the system. It's an alternative to request-driven systems, where a client sends requests to a server.

Concepts of Event-Driven Architecture:

In event-driven systems, components communicate through events, which can be published, subscribed to, or triggered.

Event-Driven vs. Request-Driven:

In a request-driven system, clients initiate actions by sending requests to a server. In an event-driven system, components react to events they subscribe to or receive.

Implementing Event-Driven Systems:

Event-driven systems use event brokers or message queues to facilitate communication. Examples include Apache Kafka, RabbitMQ, and AWS SNS.

Architectural Patterns for Scalability and Performance:

To improve scalability and performance, architectural patterns such as load balancing, horizontal and vertical scaling, and caching are employed.

Load Balancing Strategies:

Load balancing ensures that incoming requests are distributed across multiple servers. Strategies include round-robin, least connections, and IP hash-based load balancing.

Scaling Techniques (Horizontal and Vertical):

Horizontal scaling adds more instances of a component to handle increased loads, while vertical scaling involves increasing the resources of an individual instance.

Caching Strategies for Performance Optimization:

Caching involves storing frequently accessed data in a faster, closer location. Strategies include object caching, page caching, and content delivery networks (CDNs).

By applying these architectural patterns and strategies, developers can design systems that are robust, performant, and flexible to meet the demands of modern applications.


Let's look at examples of both monolithic and microservices architectures, as well as an event-driven architecture:


Monolithic Architecture Example:


E-commerce Application

Imagine you are developing an e-commerce application, and you decide to use a monolithic architecture. In this architecture:


Components:

1. Frontend: This component handles user interactions, displays product catalogues, and processes customer orders.

2. Backend: The backend component includes business logic and data processing. It's responsible for handling product management, order processing, and customer data.


Architecture:

In a monolithic architecture, both the frontend and backend are tightly integrated into a single application. They share the same codebase and database. When a user places an order, the frontend communicates with the backend to process the order, and the entire application is deployed as a single unit.


Pros:

- Simplicity: Easier to develop and test as everything is in one place.

- Less communication overhead: Components share the same memory, so there's no network latency.


Cons:

- Scalability: Difficult to scale individual components independently.

- Maintenance: Changes in one area can affect others, making maintenance challenging.

- Performance: As the application grows, it can become slower due to the monolithic nature.


Microservices Architecture Example:


Online Retail Platform

In contrast, let's consider an online retail platform that utilizes a microservices architecture:


Components:

1. Product Service: Responsible for managing product catalogues and inventory.

2. Order Service: Handles order processing and customer payments.

3. User Service: Manages user accounts, authentication, and personalization.

4. Recommendation Service: Provides product recommendations based on user behaviour.


Architecture:

In a microservices architecture, each of these services operates independently and communicates through APIs or message queues. For instance, when a user places an order, the Order Service communicates with the Product Service for inventory check and the User Service for payment processing.


Pros:

- Scalability: Each service can be scaled independently, allowing for better resource utilization.

- Flexibility: Easier to adapt and make changes to individual services.

- Fault Isolation: Failures in one service don't necessarily impact the entire system.


Cons:

- Complexity: Requires additional effort for service management, monitoring, and communication.


Event-Driven Architecture Example:


Social Media Platform

An event-driven architecture can be found in a social media platform like Twitter:


Components:

1. User Service: Manages user profiles and authentication.

2. Tweet Service: Handles creating and retrieving tweets.

3. Notification Service: Sends notifications to users about new tweets and interactions.


Architecture:

In an event-driven architecture, when a user tweets, the Tweet Service generates an event, such as "NewTweetCreated," and publishes it to a message broker like Apache Kafka. The Notification Service subscribes to this event and sends notifications to followers of the user who tweeted.


Pros:

- Asynchronous: Allows for non-blocking and efficient processing of events.

- Scalability: Can handle large numbers of events and subscribers.

- Loose Coupling: Services are decoupled and can evolve independently.


Cons:

- Complexity: Implementing event-driven systems can be more complex than request-driven ones.

- Event Processing: Requires robust event handling and processing mechanisms.


These are just examples to illustrate the differences between monolithic, microservices, and event-driven architectures. The choice of architecture depends on your specific project requirements and goals.


Now, let's explore an example of a microservices architecture with AWS and how it can incorporate event-driven design. We'll use a simplified e-commerce platform as an example.


Microservices Architecture on AWS:

Imagine you're building an e-commerce platform like Amazon. The application comprises several microservices, each responsible for specific functionalities.


Components:

1. Product Service: Manages product listings, pricing, and inventory.

2. Order Service: Handles order creation, payment processing, and shipping.

3. User Service: Manages user profiles, authentication, and recommendations.

4. Review Service: Allows users to write product reviews and ratings.


Tools and Services:

- AWS Lambda: AWS Lambda is used to create serverless functions that can be triggered by events. Each microservice might have multiple Lambda functions.

- Amazon API Gateway: It enables you to create RESTful APIs that connect your microservices to the web.

- Amazon RDS: For persistent data storage like user profiles, product listings, and order history.

- Amazon S3: Stores static assets, such as product images.

- Amazon SQS: Simple Queue Service can be used for asynchronous communication between microservices.

- Amazon SNS: Simple Notification Service is used for event notifications.

- Amazon DynamoDB: For high-throughput and NoSQL database requirements, like maintaining shopping carts.


How It Works:

1. User Service & Event-Driven Recommendation: When a user logs in, the User Service sends a "UserLoggedIn" event to an Amazon SNS topic. The Recommendation Service subscribes to this topic and processes the event, generating personalized product recommendations. It then stores the recommendations in DynamoDB, where the User Service can retrieve them.

2. Product Service & Inventory Updates: The Product Service monitors inventory levels. When inventory drops below a certain threshold, it sends an "InventoryLow" event to an Amazon SQS queue. The Order Service is subscribed to this queue and can initiate replenishment orders.

3. Order Service & Payment Processing: When a user places an order, the Order Service orchestrates the payment process. It sends an "OrderPlaced" event to an SNS topic. The Payment Service subscribes to this topic, processes the payment, and sends a "PaymentSuccessful" or "PaymentFailed" event back to the Order Service.

4. Review Service & Notification: When a user submits a product review, the Review Service stores the review in RDS and sends a "ReviewSubmitted" event to an SNS topic. The Notification Service subscribes to this topic and sends notifications to the seller and the user who wrote the review.


Scalability and Performance:

- Scaling Microservices: Each microservice can be independently scaled based on its workload. AWS Lambda functions, for example, can automatically scale in response to traffic.

- Load Balancing: Elastic Load Balancing (ELB) can be used to distribute incoming application traffic across multiple targets, enhancing the performance and fault tolerance of your microservices.

- Caching: For frequently accessed data, Amazon ElastiCache can be used to improve application response times.

- CDN: To serve static assets like product images to users around the world, use Amazon CloudFront, AWS's Content Delivery Network.


Monitoring and Management:

- AWS CloudWatch: Monitor performance, set alarms, and visualize logs and metrics.

- AWS X-Ray: Trace requests as they travel through the microservices to pinpoint bottlenecks and troubleshoot performance issues.


This architecture leverages the benefits of microservices such as scalability, flexibility, and independent evolution, combined with event-driven design for asynchronous and efficient communication between microservices. It allows for the creation of a robust and high-performing e-commerce platform on AWS.

You can find several related articles eg. Docker, Kubernetes etc in my blog.  Thank you.

No comments: