PadhaikoAll subjects

Web Application Deployment and Modern Trends

Web Application Programming (ENCT 302)

Open in the Padhaiko app

6.1 Full-stack development

Full-stack web development encompasses the complete software engineering lifecycle across both the client-side presentation tier (front-end) and the server-side logic and persistence tier (backend). A full-stack engineer possesses comprehensive proficiency in markup, styling, client scripting, server runtimes, database architecture, network protocols, and deployment infrastructure.

Evolution of Full-Stack Technology Stacks

Full-stack development has evolved through distinct technological eras:

Evolution of Prominent Full-Stack Stacks
Stack AcronymPrimary TechnologiesDominant ArchitectureKey Strengths
LAMP StackLinux, Apache, MySQL, PHP/Python/PerlServer-side rendered multi-page applications (MPA)Rock-solid stability, mature ecosystem, ubiquitous cheap hosting
MERN / MEAN StackMongoDB, Express.js, React / Angular, Node.jsSingle Page Applications (SPA) with JSON REST APIsSingle language (JavaScript/TypeScript) across client and server
Django + React / VuePython (Django), PostgreSQL, React/VueDecoupled client-server architecture with REST/GraphQLRapid backend scaffolding, robust ORM, rich client-side interactivity
Modern Hybrid (Next.js)React, Node.js / Edge runtimes, Vercel/CloudflareServer-Side Rendering (SSR) + Static Site Generation (SSG)Optimal SEO, instant static loading, seamless serverless functions

Core Responsibilities Across the Full-Stack Spectrum

  1. Presentation Layer: Designing responsive, mobile-first layouts using semantic HTML5, modern CSS (Flexbox, CSS Grid), accessible component libraries, and reactive client state management.
  2. Application & API Layer: Structuring RESTful or GraphQL endpoints, implementing business validation logic, managing authentication sessions and JWT tokens, and coordinating background task workers.
  3. Data Layer: Schema design, relational normalization, database indexing, ORM query optimization, and cache management via Redis or Memcached.
  4. DevOps & Infrastructure: Containerizing application modules using Docker, configuring reverse proxies (Nginx), automating testing pipelines, and provisioning cloud servers.

6.2 Testing and QA

Software Quality Assurance (QA) verifies that web applications perform correctly according to specifications, maintain high performance under peak loads, and remain free of regressions when new code is introduced.

The Testing Pyramid

Originally formulated by Mike Cohn, the Testing Pyramid outlines the optimal distribution and hierarchy of automated tests in a mature web engineering project:

The Modern Testing PyramidText
             / \
            /   \
           / E2E \         End-to-End (E2E) Tests (10%)
          / Tests \        - High cost, slow execution, real browsers (Playwright, Cypress)
         /---------\
        /           \
       / Integration \     Integration Tests (20%)
      /    Tests      \    - Verifies component interaction, API + DB (Supertest, PyTest)
     /-----------------\
    /                   \
   /     Unit Tests      \  Unit Tests (70%)
  /                       \ - Lowest cost, sub-second execution, isolated functions (Jest)
 /-------------------------\
  1. Unit Testing (Base Layer, ~70% of test suite):
    • Focuses on testing individual functions, algorithms, or isolated methods in complete isolation.
    • External dependencies (database queries, network calls, filesystem operations) are replaced with mock objects or stubs.
    • Extremely fast (executing thousands of tests in seconds), providing immediate feedback to developers.
    • Tools: Jest, Vitest, PyTest, JUnit.
  2. Integration Testing (Middle Layer, ~20% of test suite):
    • Validates that multiple discrete modules interact correctly together.
    • Common scenarios: Testing that an API endpoint receives an HTTP POST request, validates payload fields, correctly persists records into a test database, and returns the expected status code.
    • Tools: Supertest, Django TestCase, Spring Boot Test.
  3. End-to-End (E2E) Testing (Top Layer, ~10% of test suite):
    • Simulates realistic user journeys through a real headless web browser (clicking buttons, filling forms, observing DOM updates).
    • Verifies the entire integrated stack from front-end to database.
    • Slower and more fragile to minor visual UI shifts.
    • Tools: Playwright, Cypress, Selenium.

Test-Driven Development (TDD)

TDD is an iterative development discipline governed by the Red-Green-Refactor Cycle:

  1. Red: Write a failing automated unit test that specifies a desired function or behavior before writing any implementation code.
  2. Green: Write the minimal amount of production code necessary to make the test pass.
  3. Refactor: Clean up the codebase, eliminate duplication, and optimize structure while ensuring all tests continue to pass.

Quality Assurance Metrics


6.3 DevOps, continuous integration (CI) and continuous delivery (CD)

DevOps is a set of cultural philosophies, automated practices, and tools that merges software development (Dev) with system operations (Ops) to shorten the systems development life cycle and provide continuous high-quality delivery.

Continuous Integration (CI) and Continuous Delivery/Deployment (CD)

Definition

CI/CD Pipeline

A CI/CD Pipeline is an automated series of sequential steps that code undergoes from source commit to production deployment:

Text
Developer Git Commit -> GitHub Actions CI -> Linters & Unit Tests -> Integration Tests
                                                                           |
Production Deployment <-- Docker Container Build <-- Build Artifacts <-----+

Containerization with Docker

A container is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries, and settings.

AspectVirtual Machines (VMs)Containers (Docker)
Virtualization LevelHardware-level virtualization via Hypervisor (Type 1 or 2)OS-level virtualization sharing the host OS Linux kernel
Guest Operating SystemEach VM bundles a full, separate guest OS (gigabytes in size)Zero guest OS; containers isolate processes via kernel cgroups and namespaces
Startup LatencyMinutes to boot guest operating systemMilliseconds to initiate an isolated process
Resource FootprintHeavy: Pre-allocated RAM and CPU slicesLightweight: Minimal overhead; shares host system resources dynamically
Portability GuaranteeVM image portability dependent on hypervisor format"Build Once, Run Anywhere": Docker images run identically across development and cloud

Key Docker Concepts:

Modern Deployment Strategies

Zero-downtime production deployments deploy updates without interrupting active user traffic:

  1. Rolling Deployment: Gradually replaces instances of the old version with instances of the new version behind a load balancer until all servers are updated.
  2. Blue-Green Deployment: Provisions two identical production environments: "Blue" runs active traffic; "Green" hosts the new release. Once Green passes validation, the load balancer switches all traffic to Green instantly. If errors occur, rollback is instant by switching back to Blue.
  3. Canary Deployment: Routes a tiny percentage of live user traffic (e.g., 5%) to the new release while monitoring error rates and performance metrics. If stable, traffic is gradually shifted to 100%.

6.4 Progressive web apps (PWAs), responsive design and usability

Modern web engineering bridges the divide between standard desktop web pages and high-performance native mobile applications.

Progressive Web Applications (PWAs)

A Progressive Web App (PWA) is a web application constructed with modern web APIs that delivers native-app-like user experiences: installable on device home screens, fully functional offline, capable of background synchronization, and supporting push notifications.

Core Pillars of a PWA:

  1. Web App Manifest (manifest.json): A JSON configuration file providing metadata about the application when installed on a mobile device or desktop:
Text
{
  "short_name": "Institute of Engineering Exam Portal",
  "name": "Tribhuvan University Examination Portal",
  "icons": [
    {
      "src": "/assets/icons/icon-192.png",
      "type": "image/png",
      "sizes": "192x192"
    },
    {
      "src": "/assets/icons/icon-512.png",
      "type": "image/png",
      "sizes": "512x512"
    }
  ],
  "start_url": "/?source=pwa",
  "background_color": "#0f0e0c",
  "theme_color": "#00a86b",
  "display": "standalone",
  "orientation": "portrait"
}

"display": "standalone" hides the browser address bar and navigation controls, giving the app a native full-screen interface.

  1. Service Workers: An event-driven, client-side programmable network proxy running on a separate background thread, independent of the web page:
    • Intercepts all outgoing network HTTP requests (fetch event).
    • Manages programmatic offline caching via the browser Cache API.
    • Listens for push notifications even when the browser application is completely closed.
  2. HTTPS Requirement: Because Service Workers possess the capability to intercept all network traffic, browsers mandate that Service Workers register exclusively over encrypted HTTPS origins (with localhost allowed for development).

Service Worker Caching Strategies

Standard Service Worker Caching Strategies
Strategy NameOperational SequenceBest Suited For
Cache-First (Cache Falling Back to Network)Checks cache; if found, returns immediately. If missing, fetches from network, caches result, and returns.Static assets that rarely change: fonts, CSS stylesheets, bundled JS, brand icons
Network-First (Network Falling Back to Cache)Tries network fetch first. If successful, updates cache. If network fails (offline), falls back to cached copy.Fresh dynamic content: syllabus updates, student grades, exam notices
Stale-While-RevalidateServes cached representation immediately for instant speed, while concurrently fetching fresh copy in background to update cache for next time.Frequently updated non-critical content: avatars, course listings
Network-OnlyExclusively fetches from network; never consults cache.Non-cacheable transactions: bank payments, login authentications

Responsive Web Design (RWD) and Usability

Responsive Web Design ensures web layouts adapt gracefully to any viewport width, screen density, or orientation (mobile phones, tablets, laptops, widescreen monitors).

Three Technical Tenets of RWD (Ethan Marcotte):

  1. Fluid Grids: Sizing layout containers using relative units (%, vw, fr) rather than fixed pixels (px).
  2. Flexible Media: Ensuring images and videos scale proportionally without overflowing their parent containers: img { max-width: 100%; height: auto; }.
  3. CSS Media Queries: Applying conditional style blocks based on viewport dimensions:
CSS
/* Mobile-first base styles apply below 768px */
.course-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
}

/* Tablet and Desktop breakpoint */
@media (min-width: 768px) {
  .course-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 24px;
  }
}

Usability and Web Accessibility (WCAG 2.1)


Worked Examples

Example

Service Worker Cache Hit Latency and Performance Calculation

A student portal mobile web app implements a Progressive Web App (PWA) with a Cache-First Service Worker strategy for all static assets. Performance testing on a 4G mobile network reveals:

During a study session, a student makes 1,000 asset requests, achieving an 85% Cache Hit Ratio (850 requests hit cache, 150 requests miss and hit network).

  1. Calculate the total elapsed loading time in milliseconds for all 1,000 requests under the PWA Service Worker caching architecture.
  2. Calculate the total elapsed time if no Service Worker were installed (meaning all 1,000 requests must travel over the 200 ms network).
  3. Calculate the average latency per request under the PWA architecture.
  4. Calculate the percentage reduction in total latency achieved by the PWA Service Worker cache.

Calculate Total Latency Under PWA Service Worker: Total requests = 1,000. Cache Hits = 85% of 1,000 = 850 requests. Cache Misses = 15% of 1,000 = 150 requests.

Time for cache hits = 850 * 5 ms = 4,250 ms. Time for network fallbacks = 150 * 200 ms = 30,000 ms. Total PWA Latency = 4,250 ms + 30,000 ms = 34,250 ms (34.25 seconds).

Calculate Total Latency Without Service Worker (Network-Only): All 1,000 requests travel over the network at 200 ms each: Total Network-Only Latency = 1,000 * 200 ms = 200,000 ms (200.0 seconds).

Calculate Average Latency Per Request with PWA: Average Latency = Total PWA Latency / 1,000 requests Average Latency = 34,250 ms / 1,000 = 34.25 ms per request.

Calculate Percentage Reduction in Latency: Latency Savings = 200,000 ms - 34,250 ms = 165,750 ms. Percentage Reduction = (165,750 / 200,000) * 100 Percentage Reduction = 0.82875 * 100 = 82.875% (approximately 82.88%).

Answer:
  1. Total PWA loading time is 34,250 milliseconds (34.25 seconds).
  2. Without a Service Worker, total loading time is 200,000 milliseconds (200.0 seconds).
  3. The average latency per request with PWA is 34.25 milliseconds.
  4. The PWA Service Worker achieves an 82.88% reduction in total network latency.
Example

Continuous Integration Pipeline Specification (GitHub Actions)

Design an enterprise GitHub Actions CI workflow file (.github/workflows/ci.yml) for a full-stack Django and React application. The pipeline must:

  1. Trigger automatically on every push and pull request to the main branch.
  2. Spin up an isolated PostgreSQL 15 service container.
  3. Install dependencies, execute linting checks (flake8), and execute the Django unit test suite with database migrations.
  4. Build the front-end React production assets.

Construct the GitHub Actions YAML Pipeline:

Text
name: Continuous Integration Pipeline

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  test-and-build:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:15-alpine
        env:
          POSTGRES_DB: test_tu_portal
          POSTGRES_USER: tu_admin
          POSTGRES_PASSWORD: secure_test_password
        ports:
          - 5432:5432
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

    steps:
      - name: Checkout Source Code
        uses: actions/checkout@v4

      - name: Set up Python 3.11 Runtime
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'
          cache: 'pip'

      - name: Install Backend Dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
          pip install flake8

      - name: Run Backend Static Linting
        run: |
          flake8 . --count --max-line-length=120 --statistics

      - name: Run Database Migrations and Test Suite
        env:
          DATABASE_URL: postgres://tu_admin:secure_test_password@localhost:5432/test_tu_portal
          SECRET_KEY: ci-temporary-secret-key-for-testing-only
        run: |
          python manage.py migrate
          python manage.py test --verbosity=2

      - name: Set up Node.js 20 Runtime
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'

      - name: Build Front-End React Bundle
        run: |
          cd frontend
          npm ci
          npm run build

Explain Pipeline Guarantees:

  • If any stage fails (e.g., a linting violation, database migration failure, or broken unit test), GitHub Actions halts execution immediately, flags the commit with a red badge, and blocks merging into main.
  • The PostgreSQL service container ensures tests run against an authentic production-grade relational database rather than an in-memory SQLite surrogate.
Answer:

The workflow specifies: (1) event triggers for push and pull_request on main, (2) an automated PostgreSQL 15 service container, (3) Python dependency installation, flake8 linting, and manage.py test execution, and (4) Node.js front-end bundle compilation (npm ci && npm run build).

Example

Service Worker Implementation with Cache-First Strategy

Implement a production-ready Service Worker (service-worker.js) that:

  1. Caches a core static cache shell (/, /index.html, /styles.css, /app.js, /offline.html) during the install event.
  2. Cleans up stale old cache versions during the activate event.
  3. Implements a Cache-First strategy for all GET fetch events, returning an offline fallback page (/offline.html) if both cache and network fail.

Implement the Service Worker Script:

JavaScript
// service-worker.js
const CACHE_NAME = 'ioe-portal-cache-v1';
const STATIC_ASSETS = [
  '/',
  '/index.html',
  '/styles.css',
  '/app.js',
  '/offline.html',
  '/assets/icons/icon-192.png'
];

// 1. Install Event: Pre-cache static application shell
self.addEventListener('install', event => {
  event.waitUntil(
    caches.open(CACHE_NAME).then(cache => {
      console.log('Pre-caching static assets');
      return cache.addAll(STATIC_ASSETS);
    }).then(() => self.skipWaiting())
  );
});

// 2. Activate Event: Delete previous stale cache generations
self.addEventListener('activate', event => {
  event.waitUntil(
    caches.keys().then(cacheNames => {
      return Promise.all(
        cacheNames.map(name => {
          if (name !== CACHE_NAME) {
            console.log('Deleting obsolete cache:', name);
            return caches.delete(name);
          }
        })
      );
    }).then(() => self.clients.claim())
  );
});

// 3. Fetch Event: Intercept network calls with Cache-First strategy
self.addEventListener('fetch', event => {
  // Only handle GET requests
  if (event.request.method !== 'GET') return;

  event.respondWith(
    caches.match(event.request).then(cachedResponse => {
      if (cachedResponse) {
        return cachedResponse; // Cache Hit: Return cached asset immediately
      }

      // Cache Miss: Fetch from network and dynamically cache response
      return fetch(event.request).then(networkResponse => {
        if (!networkResponse || networkResponse.status !== 200 || networkResponse.type !== 'basic') {
          return networkResponse;
        }

        const responseToCache = networkResponse.clone();
        caches.open(CACHE_NAME).then(cache => {
          cache.put(event.request, responseToCache);
        });

        return networkResponse;
      }).catch(() => {
        // Network failure and cache miss: Return offline fallback
        if (event.request.headers.get('accept').includes('text/html')) {
          return caches.match('/offline.html');
        }
      });
    })
  );
});
Answer:

The script implements: (1) install event caching of STATIC_ASSETS with skipWaiting(), (2) activate event pruning of obsolete cache keys via caches.keys(), and (3) fetch event interception matching caches.match(request) first, fetching from network on miss, and serving /offline.html upon network disconnects.


Exercises

Practice Exercises — Web Deployment and Modern Trends

Question 1

Describe the Testing Pyramid. Explain the distinct roles, execution speeds, and cost-benefit trade-offs of Unit Tests, Integration Tests, and End-to-End (E2E) Tests.

The Testing Pyramid Distribution:

  1. Unit Tests (70%): Test isolated functions and single classes without external I/O.
    • Speed: Sub-millisecond execution; thousands run in seconds.
    • Cost: Lowest development and maintenance cost.
    • Limitation: Does not test how independent modules communicate.
  2. Integration Tests (20%): Test interactions between multiple modules (e.g., verifying an API controller correctly stores records in a real database).
    • Speed: Slower than unit tests (seconds), involving I/O.
    • Cost: Moderate cost.
    • Benefit: Catches interface mismatches, schema bugs, and serialization errors.
  3. End-to-End Tests (10%): Simulate real user interactions through an actual browser engine (e.g., navigating to login, entering credentials, asserting that the dashboard renders).
    • Speed: Slowest (taking minutes).
    • Cost: Highest maintenance cost; vulnerable to minor visual shifts.
    • Benefit: Validates complete end-to-end user journeys across the entire production stack.
Answer:

The Testing Pyramid advocates for ~70% fast, low-cost Unit Tests for isolated logic, ~20% Integration Tests to verify module and database interactions, and ~10% comprehensive End-to-End (E2E) Tests to validate complete user journeys in real browsers.

Question 2

Define Continuous Integration (CI) and Continuous Delivery/Deployment (CD). What automated pipeline steps must execute on every pull request before code can be safely merged into production?

Definitions:

  • Continuous Integration (CI): The practice of automating the integration of code changes from multiple contributors into a shared central repository multiple times a day. Automated builds and tests run on every commit.
  • Continuous Delivery (CD): Automates the packaging and testing of code so that verified releases can be deployed to production at any time with a single manual approval.
  • Continuous Deployment (CD): Automatically deploys every change that passes all pipeline stages directly to production without manual intervention.

Mandatory Automated Pipeline Steps:

  1. Source Code Checkout: Pulling the latest branch commit.
  2. Dependency Resolution & Caching: Installing verified package versions from lockfiles (package-lock.json, poetry.lock).
  3. Static Analysis & Linting: Running automated syntax and style checkers (ESLint, flake8, Black) to enforce standards.
  4. Security Vulnerability Scanning: Scanning dependencies for known vulnerabilities (using npm audit, safety, or Snyk).
  5. Automated Test Execution: Running unit and integration tests against a containerized database.
  6. Artifact Compilation: Building production bundles (minifying CSS/JS, compiling Docker images).
Answer:

CI automates merging, building, and testing code continuously. CD automates release preparation or deployment. On every pull request, mandatory automated steps include: dependency resolution, static linting, security vulnerability scanning, automated test suite execution, and production build artifact compilation.

Question 3

What is a Progressive Web App (PWA)? Explain the core technical role of the Web App Manifest and Service Workers, and contrast the Cache-First strategy with the Network-First strategy.

PWA Definition and Core Components: A PWA is a web application enhanced with native-app features: installability, offline execution, and push notifications.

  • Web App Manifest (manifest.json): Metadata file defining home screen app icons, app name, theme colors, orientation, and "display": "standalone", enabling the app to run without browser URL bars.
  • Service Worker: An event-driven background proxy thread running outside web pages. It intercepts network fetch events, manages offline asset caches via the Cache API, and handles push messages.

Cache-First vs. Network-First Strategies:

  • Cache-First: Checks local cache first; if present, returns asset instantly (5 ms). If missing, fetches from network and caches result. Ideal for static assets that rarely change (CSS, fonts, JS bundles, logos).
  • Network-First: Tries network request first to fetch freshest server state. If the user is offline or the network drops, falls back to the cached copy. Ideal for dynamic, frequently changing data (exam notices, student grades).
Answer:

PWAs combine web reach with native app capabilities. The Web App Manifest enables home screen installation; Service Workers provide background network interception and offline caching. Cache-First returns cached static assets instantly with network fallback, whereas Network-First prioritizes fresh server data and uses cache as an offline fallback.

Question 4

Explain how containerization with Docker differs from traditional Virtual Machines (VMs). How does containerization solve the classic "it works on my machine" software deployment problem?

Docker Containers vs. Virtual Machines:

  • Virtual Machines: Virtualize physical hardware via a hypervisor. Each VM requires its own full guest operating system (OS kernel, system binaries, disk image measuring several gigabytes). Startup requires minutes, and resource pre-allocation is rigid.
  • Docker Containers: Virtualize at the operating system level. Containers run as isolated processes on the host Linux OS, sharing the single underlying host Linux kernel via namespaces (isolating processes, networks, and mount points) and cgroups (limiting CPU, RAM, and I/O usage). Containers launch in milliseconds and have negligible memory overhead.

Resolving "It Works on My Machine": A Docker container packages application source code together with its exact system libraries, language runtime, environment variables, and configuration files into an immutable Docker Image. Because the image contains the entire execution environment, it executes with 100% identical behavior on a developer's laptop, a CI/CD server, and a cloud production Kubernetes cluster.

Answer:

VMs virtualize hardware and run separate heavy guest operating systems, whereas Docker containers virtualize the OS, sharing the host kernel for millisecond startup and minimal overhead. Docker solves environment discrepancies by packaging code, runtime, and all system libraries into an immutable image that runs identically everywhere.

Question 5

Compare Rolling Deployment, Blue-Green Deployment, and Canary Deployment strategies for production web applications. What are the advantages and rollback characteristics of each?

Comparison of Deployment Strategies:

  1. Rolling Deployment:
    • Mechanism: Incrementally updates server instances behind a load balancer (e.g., updating 25% of nodes at a time until 100% are on the new version).
    • Advantage: Requires zero duplicate infrastructure.
    • Rollback: Slow: rolling back requires a reverse rolling deployment of the old version.
  2. Blue-Green Deployment:
    • Mechanism: Operates two identical production environments: Blue (active production) and Green (new release). Once Green is verified, the router/load balancer instantly flips all traffic to Green.
    • Advantage: Instant cutover and near-instant rollback (switching traffic back to Blue).
    • Drawback: Requires double the infrastructure hardware/cost during deployment.
  3. Canary Deployment:
    • Mechanism: Routes a small fraction of live user traffic (e.g., 5%) to the new release while monitoring error rates, CPU load, and response latency. If healthy, traffic percentage is progressively increased to 100%.
    • Advantage: Limits the blast radius of unforeseen bugs to a tiny subset of users.
    • Rollback: Instant: shifting the 5% traffic back to the stable baseline.
Answer:

Rolling Deployment updates server nodes gradually with zero extra infrastructure but slower rollbacks. Blue-Green Deployment provisions two identical environments for instant traffic switching and instant rollback at the expense of duplicate hardware. Canary Deployment tests new releases on a small percentage of live users, minimizing failure blast radius before full rollout.

← Web Application Security