Advanced Web Technologies

Chapter 11
Performance, Scalability and Reliability

Bachelors Program in Computer Applications
Advanced Web Technologies   •   Bachelor

Performance, Scalability and Reliability

Introduction

A web application is successful only when it remains useful under realistic conditions: pages must appear quickly, interactions must respond promptly, requests must continue to succeed during traffic growth, and failures must be contained rather than amplified. Performance, scalability and reliability are related but distinct properties. Performance concerns the speed and efficiency of individual operations. Scalability concerns the ability to maintain acceptable service as demand or data volume increases. Reliability concerns the probability that a service performs its intended function over time.

These qualities are not achieved by a single tool. They emerge from coordinated decisions about browser rendering, JavaScript, images, networks, databases, caching, deployment architecture, capacity, monitoring and operational response. Optimizing one layer can damage another: aggressive caching can serve stale data, database indexes can accelerate reads while slowing writes, and adding servers can increase cost without solving a database bottleneck.

This chapter presents a systematic approach. It begins with user-visible performance, then examines application and infrastructure behavior, followed by scalability patterns, resilience, capacity planning and observability.

Learning Objectives

After studying this chapter, you should be able to:

  • explain Core Web Vitals and design measurement strategies;
  • analyze browser rendering, assets and network behavior;
  • optimize database access and apply caching appropriately;
  • distinguish vertical and horizontal scaling and select suitable architectures;
  • apply load balancing, CDNs and resilience patterns;
  • estimate capacity using demand, concurrency and utilization models;
  • design logs, metrics and traces for useful observability;
  • evaluate trade-offs involving cost, freshness, reliability, security and accessibility.

1. A Model of Web-System Performance

A user request passes through several stages: DNS resolution, connection establishment, request transmission, server processing, database or service calls, response transmission, parsing, rendering and interaction. A simplified decomposition is:

Ttotal = Tdns + Tconnect + Trequest + Tserver + Tresponse + Trender

Here each term is a time component, measured in milliseconds or seconds. The decomposition is approximate: stages may overlap through connection reuse, multiplexing, streaming and parallel downloads. It is therefore a diagnostic model, not an exact universal identity. Nevertheless, it helps engineers ask where time is being spent.

Average latency is insufficient. A small number of very slow requests can damage user experience, so percentiles are preferred. The 50th percentile (p50) describes the median; p95 is the value below which 95% of observations fall; p99 describes the slowest one percent. A service with a 200 ms median and a 5-second p99 may feel unreliable even though its average appears acceptable.

A useful principle is to define a service-level objective (SLO) before optimizing. For example: “99% of product-page requests complete in less than 800 ms over a calendar month.” An SLO is an operational target; an SLA may be a contractual commitment with consequences. The difference matters because optimization should serve explicit user and business requirements rather than arbitrary benchmark scores.

2. Measuring the User Experience

Core Web Vitals

Core Web Vitals are field-oriented indicators of loading, interactivity and visual stability:

  • Largest Contentful Paint (LCP): how quickly the largest relevant image or text block becomes visible. A commonly used “good” threshold is at or below 2.5 seconds.
  • Interaction to Next Paint (INP): responsiveness across interactions, representing the delay between an interaction and the next rendered update. A commonly used good threshold is at or below 200 ms.
  • Cumulative Layout Shift (CLS): unexpected visual movement during the page lifecycle. A commonly used good threshold is at or below 0.1.

Thresholds are practical classifications, not physical laws, and can change as web standards evolve. They should be interpreted by device, connection type, geography and page template. Field data, known as Real User Monitoring (RUM), captures actual users; laboratory tools such as Lighthouse provide repeatable diagnostic conditions. Both are necessary: lab tests help reproduce causes, while field data reveals real variability.

CLS is calculated from layout-shift entries. Conceptually, each unexpected shift contributes an impact fraction multiplied by a distance fraction. A large element moving a long distance produces a larger score than a small movement. Developers reduce CLS by reserving image and advertisement dimensions, avoiding insertion above existing content, and controlling font swaps.

LCP is improved by reducing server response time, prioritizing the hero resource, compressing images, using responsive image variants, preloading only genuinely critical resources, and removing render-blocking work. INP is improved by reducing long JavaScript tasks, splitting bundles, postponing nonessential work and optimizing event handlers. A task lasting more than 50 ms is often treated as a long task for responsiveness analysis, although the appropriate target depends on device and workload.

Measurement discipline

A sound measurement process has five stages:

  1. define the user journey and target metric;
  2. establish a baseline under controlled and field conditions;
  3. identify the dominant bottleneck using traces and profiles;
  4. change one meaningful variable where possible;
  5. compare distributions, not only a single best result.

Synthetic tests should document browser version, viewport, CPU throttling, network profile, cache state and geographic location. RUM should avoid collecting unnecessary personal data and should sample responsibly. Performance budgets can specify limits such as JavaScript transfer size, image weight, LCP and error rate, but budgets must be reviewed when product requirements change.

3. Browser Rendering and Front-End Assets

The browser parses HTML into a DOM tree and CSS into a CSSOM; it combines them to construct render information, calculates geometry, paints pixels and composites layers. JavaScript can interrupt this process by reading or modifying layout-related properties. Repeated alternation between reads and writes can cause forced synchronous layout, commonly called layout thrashing.

Practical rendering strategies include:

  • deliver meaningful HTML early, especially for server-rendered or statically generated pages;
  • use defer for scripts that need document parsing but not immediate execution, and use async only when execution order is unimportant;
  • split JavaScript by route or feature and remove unused code through tree shaking;
  • avoid excessive DOM size and repeated style recalculation;
  • batch DOM reads and writes;
  • use CSS containment and virtualization for very large lists where appropriate;
  • reserve dimensions for images, video and dynamic components;
  • prefer transform and opacity for suitable animations, while respecting reduced-motion preferences.

Asset optimization must preserve quality and accessibility. Modern image formats such as AVIF and WebP can reduce transfer size, but fallbacks and decoding support should be considered. srcset and sizes allow the browser to select an appropriate image. Images below the fold can use lazy loading, whereas the principal above-the-fold image should not be lazily deferred without evidence that it helps.

Compression reduces bytes but consumes CPU. Brotli is often effective for text resources, while gzip remains widely supported. Minification removes unnecessary characters; it does not replace code-splitting or architectural improvement. Cache-busting filenames such as app.8f3c.js permit long-lived immutable caching while ensuring new content receives a new URL.

Accessibility is part of quality, not a performance sacrifice. A visually fast page that cannot be navigated by keyboard, screen reader or users with motion sensitivity is not a high-quality system.

4. Networks, HTTP and Content Delivery

Network performance depends on distance, congestion, protocol behavior, connection setup and resource count. HTTP/2 multiplexes requests over a connection and compresses headers; HTTP/3 uses QUIC over UDP and can reduce some head-of-line blocking effects. Neither protocol automatically makes a poorly designed page fast. Large payloads, excessive JavaScript and slow origins remain problems.

Important techniques include connection reuse, TLS session resumption, compression, streaming, response prioritization and reducing unnecessary round trips. Resource hints such as preconnect can help for a genuinely critical cross-origin connection, but excessive hints compete for bandwidth and increase connection work.

A CDN caches content at edge locations near users. Static assets are especially suitable. Dynamic or personalized responses require careful cache-control policies, key design and privacy analysis. A CDN can also absorb traffic, terminate TLS, provide image transformation and mitigate some attacks, but it does not eliminate origin bottlenecks or application vulnerabilities.

Cache directives express policy. max-age specifies freshness duration; no-store requests that a response not be stored; private limits storage to a user agent or private cache. ETag and Last-Modified support revalidation, allowing a client to ask whether its stored representation remains valid. Cache keys must include every input that changes the response, such as language, device variant or authorization context. Accidentally caching personalized data as public content is a serious confidentiality failure.

5. Application and Database Performance

Application profiling should identify time spent in CPU work, waiting, locking, network calls and database operations. A fast algorithm can be defeated by repeated remote calls. Batching, connection pooling, asynchronous processing and appropriate concurrency limits often produce larger gains than micro-optimizations.

Database performance begins with data access patterns. Indexes reduce search work for suitable predicates, joins and ordering, but they consume storage and make writes more expensive. Query plans should be inspected using the database’s execution-plan tools. A query that is fast on a small development dataset may become slow when cardinality, skew or concurrency changes.

Common problems include N+1 queries, unbounded result sets, missing indexes, excessive columns, long transactions, lock contention and connection-pool exhaustion. Pagination should be stable; cursor-based pagination is often more efficient than large offsets for changing datasets. Read replicas can scale read traffic, but replication lag means that a user may not immediately observe a write.

Connection pools require restraint. If every application instance opens too many connections, the database can be overloaded even when application CPU is low. Pool size must be considered across all instances and environments. Transactions should be short and should enforce the required consistency level rather than the strongest possible level by habit.

Caching models

Caching may occur in the browser, CDN, reverse proxy, application memory or a distributed cache. Each layer has different capacity, latency, invalidation and consistency properties. The central design question is not merely “Can this be cached?” but “What correctness guarantee is acceptable when the cached value is old?”

A cache hit ratio can be defined as:

H = Nhit ÷ (Nhit + Nmiss)

where Nhit is the number of hits and Nmiss is the number of misses during a specified interval. The ratio has no unit. It is meaningful only when the observation window, cache key and request population are defined. A high hit ratio does not guarantee good performance if misses are extremely expensive or if cached data is incorrect.

Common invalidation strategies include time-to-live expiry, explicit deletion after a write, versioned keys and write-through or write-behind approaches. Write-behind can improve write latency but risks data loss if the cache fails before persistence. Stampede protection—such as request coalescing, probabilistic early refresh or bounded stale serving—prevents many clients from rebuilding the same expired object simultaneously.

6. Scalability and Distributed Architecture

Vertical scaling increases the capacity of one machine, for example by adding memory or CPU. It is simple but bounded by hardware limits and may create a large failure domain. Horizontal scaling adds instances and usually requires stateless application design, external session storage, shared object storage and load distribution.

A load balancer selects among healthy backends using algorithms such as round robin, weighted routing or least connections. Health checks must test meaningful service readiness, not merely whether a process is alive. Draining allows existing connections to finish before an instance is removed. Sticky sessions may simplify legacy state management but reduce flexibility and create uneven load; shared session storage or signed client tokens are often better alternatives.

A simple utilization relationship is expressed by Little’s Law:

L = λW

L is the average number of items in a stable system, λ is the average arrival rate in items per unit time, and W is the average time an item spends in the system. Units are consistent: requests/second multiplied by seconds/request yields requests. The law assumes a stable system over the measurement interval and applies to queues, requests in flight or other flow systems. It does not by itself predict latency or prove that a system is stable.

For example, if an API receives 120 requests per second and the average end-to-end time is 0.25 seconds, the average number of requests in flight is:

L = 120 × 0.25 = 30 requests

This estimate helps size concurrency, but it does not imply that 30 workers are always sufficient. Variability, blocking, limits and tail latency must also be considered.

Stateless services scale more easily, but state still exists somewhere: databases, queues, object stores, caches or clients. Distributed systems introduce partial failure, clock differences, duplicate messages and consistency trade-offs. Idempotency keys are important for operations such as payments or order creation, where retries might otherwise create duplicates.

7. Reliability and Resilience Patterns

Reliability is improved by preventing failures, detecting them quickly, limiting their spread and recovering safely. Important patterns include:

  • Timeouts: bound waiting time; every outbound call should have a deliberate timeout.
  • Retries: useful for transient failures, but should use bounded attempts, exponential backoff and jitter. Retrying non-idempotent operations without safeguards can duplicate effects.
  • Circuit breakers: stop calls to a failing dependency temporarily, allowing recovery and protecting resources.
  • Bulkheads: isolate pools or workloads so that one failure does not consume all capacity.
  • Rate limiting: protect services and allocate scarce capacity fairly.
  • Load shedding: reject lower-priority work when the system cannot safely accept more.
  • Graceful degradation: provide cached, partial or read-only functionality when dependencies fail.
  • Queues: absorb bursts and decouple producers from consumers, but require monitoring for backlog, poison messages and delivery semantics.

A retry policy should specify which errors are retryable, maximum elapsed time, attempt count and jitter. Retrying a 400-level validation error is generally wasteful; retrying a transient connection failure may be appropriate. At the system level, uncontrolled retries can form a retry storm and make an outage worse.

Availability is often described as the proportion of time a service is usable. For a simple series of components that all must work, availability is approximately:

Aseries = A1 × A2 × … × An

For two independent redundant components where either can serve the request:

Aparallel = 1 − (1 − A1)(1 − A2)

These formulas assume statistically independent failures and clearly defined component boundaries. Real systems violate independence through shared networks, deployments, power, credentials and software defects; therefore redundancy is not a guarantee of the calculated value.

8. Capacity Planning

Capacity planning connects demand forecasts to resource limits. Measure requests per second, concurrent sessions, payload size, CPU, memory, disk I/O, database connections, queue depth and latency percentiles. Forecast normal growth and exceptional events such as campaigns.

A basic resource-utilization ratio is:

U = D ÷ C

where D is observed demand in a defined unit, C is sustainable capacity in the same unit, and U is dimensionless utilization. Capacity should be measured under an agreed SLO, not at the point where a benchmark machine becomes completely saturated. Headroom is required for bursts, failover and maintenance.

Capacity testing should include load tests, stress tests, soak tests and failure tests. Test data must resemble production in size and distribution, while privacy requirements must be respected. A benchmark that uses unrealistically small objects or no contention can produce false confidence.

9. Observability: Logs, Metrics and Traces

Monitoring answers whether a known condition is healthy; observability supports investigation of unknown conditions. The three principal signals are:

  • Logs: discrete records of events. They should include timestamp, severity, service, environment, request or trace identifier and useful structured fields. Avoid passwords, tokens and unnecessary personal information.
  • Metrics: numeric time series such as request rate, error rate, duration, saturation, cache hit ratio and queue depth. Histograms are preferable to averages when latency distributions matter.
  • Traces: a request’s path across services, represented by spans with timing and attributes. Trace context propagation links frontend, gateway, service and database activity.

A useful operational framework is the four golden signals: latency, traffic, errors and saturation. RED emphasizes Rate, Errors and Duration for services; USE emphasizes Utilization, Saturation and Errors for resources. These frameworks complement, rather than replace, business indicators such as completed checkouts or failed searches.

High-cardinality labels can make metrics expensive and difficult to query. User IDs and unbounded URLs generally belong in traces or carefully controlled logs rather than metric dimensions. Alerting should be actionable, tied to user impact and resistant to short-lived noise. Error budgets quantify the amount of unreliability permitted by an SLO and help balance feature delivery against reliability work.

Deployment safety includes canary releases, feature flags, automated rollback, database migration compatibility and tested backups. Backups are not sufficient unless restoration is regularly verified. Recovery objectives should distinguish RTO—maximum acceptable restoration time—from RPO—maximum acceptable data loss measured in time.

10. Illustrative Case: An Online Retail Platform

Background

An online retailer experiences traffic spikes during promotional events. Its product pages are served by a web application, an API, a relational database and a third-party payment service.

Problem

Field data shows that the 75th-percentile LCP is 4.1 seconds, checkout p95 latency is 2.4 seconds, and error rates increase during promotions. Database CPU reaches 90%, while application servers remain relatively idle. Customers occasionally submit duplicate orders after refreshing.

Analysis

Tracing reveals that product pages request oversized images and perform an N+1 query for inventory by warehouse. The checkout path waits synchronously for a recommendation service and payment authorization. Logs show repeated payment requests with no idempotency key. The database has a missing composite index and receives connections from every application instance without a global pool limit.

Approach and solution

The team introduces responsive image variants, reserves image dimensions, and moves noncritical recommendation work after the initial response. Product data is cached at the CDN with versioned asset URLs; inventory uses a short-lived application cache with explicit invalidation after stock changes. The N+1 query is replaced by a batched query and an appropriate composite index validated through an execution plan.

Checkout receives a bounded timeout for recommendations and proceeds without them if necessary. Payment requests use an idempotency key tied to the order attempt. Retries are limited to explicitly transient failures. Database connections are capped per deployment, and read-only catalog traffic is directed to read replicas while checkout writes remain on the primary.

A canary deployment is instrumented with traces, p95/p99 latency, error rate, database saturation and completed-order rate. A circuit breaker protects the recommendation service. Queue-based processing handles email and analytics after order completion.

Outcome

In this illustrative scenario, the principal improvements come from removing unnecessary work and controlling dependencies, not simply adding application servers. The team must continue checking cache freshness, replica lag, payment correctness and promotional traffic behavior. Reported improvements should use comparable field populations and time windows rather than one favorable laboratory run.

Learning points

  1. User-visible metrics connect technical work to experience.
  2. Traces identify cross-layer bottlenecks that host metrics can hide.
  3. Reliability patterns must preserve business correctness, especially for payments.
  4. Scaling one tier cannot compensate for a saturated database.
  5. Every optimization introduces a policy trade-off involving freshness, cost or complexity.

11. Governance, Ethics and Contemporary Practice

Performance engineering must respect privacy, accessibility and security. RUM data should be minimized, anonymized where possible and processed under applicable privacy law and organizational policy. Logs must not expose credentials, health information or payment data. CDN and cache configuration require special care for personal and regulated content.

Sustainability is also relevant: efficient queries, smaller payloads and right-sized infrastructure reduce energy use and cost. AI-assisted coding and automated performance recommendations can identify patterns, generate profiling hypotheses or summarize traces, but outputs require human validation. AI-generated changes may introduce insecure caching, excessive telemetry or incorrect retry behavior. Production decisions should remain auditable and tested.

Modern systems increasingly combine edge computing, server components, streaming, WebAssembly, service meshes and managed databases. These technologies can improve particular workloads but add operational and debugging complexity. Adoption should follow measured need, clear ownership and a rollback plan rather than novelty.

Best-Practice Checklist

  • Define SLOs and measure percentiles using both RUM and laboratory tests.
  • Optimize the critical rendering path before low-impact micro-optimizations.
  • Compress and correctly size assets; preserve accessibility and responsive behavior.
  • Inspect database plans, query counts, locks and connection pools.
  • Design cache keys, freshness and invalidation as correctness policies.
  • Keep services stateless where practical and protect stateful dependencies.
  • Use timeouts, bounded retries, jitter, circuit breakers and bulkheads deliberately.
  • Plan capacity with realistic traffic, headroom and failure scenarios.
  • Correlate structured logs, metrics and traces without leaking sensitive data.
  • Test rollback, backup restoration, failover and degraded modes.

Chapter Summary

Performance is a user and system property spanning networks, browsers, servers and databases. Core Web Vitals provide important field-oriented indicators, but meaningful optimization requires controlled measurement, percentiles and traces. Rendering improves through efficient scripts, appropriate assets, stable layout and reduced main-thread work. CDNs, compression, HTTP capabilities and caching reduce delivery cost when freshness and privacy policies are correct.

Scalability may be vertical or horizontal; horizontal designs require attention to state, load balancing, databases and distributed-system failure. Little’s Law supports concurrency analysis under stable conditions, while capacity planning must include variability and headroom. Reliability is strengthened through timeouts, bounded retries, circuit breakers, bulkheads, graceful degradation, idempotency and tested recovery. Observability combines logs, metrics and traces, guided by latency, traffic, errors and saturation. Effective engineering balances speed, correctness, cost, security, accessibility, sustainability and operational simplicity.

Key Terms

Core Web Vitals: User-oriented metrics for loading, responsiveness and visual stability.

LCP: Largest Contentful Paint, a loading-performance indicator.

INP: Interaction to Next Paint, a responsiveness indicator across interactions.

CLS: Cumulative Layout Shift, a measure of unexpected visual movement.

RUM: Real User Monitoring based on actual user experiences.

CDN: Content Delivery Network that serves content from distributed edge locations.

Cache invalidation: The process of expiring or removing data that should no longer be served.

Load balancer: A component that distributes traffic among service instances.

Horizontal scaling: Increasing capacity by adding instances.

Vertical scaling: Increasing the resources of an existing instance.

SLO: Service-Level Objective defining a measurable reliability or performance target.

Percentile: A distribution value below which a stated percentage of observations falls.

Idempotency: Property allowing repeated equivalent requests without repeated unintended effects.

Circuit breaker: A control that temporarily stops calls to a failing dependency.

Bulkhead: Isolation of resources to limit failure propagation.

Little’s Law: Relationship L = λW among population, arrival rate and time in system.

Observability: Ability to infer internal system state from outputs such as logs, metrics and traces.

RTO: Recovery Time Objective, the acceptable time to restore service.

RPO: Recovery Point Objective, the acceptable amount of data loss measured in time.

Review Questions

Short Answer Questions

  1. Distinguish performance, scalability and reliability.
  2. What do LCP, INP and CLS measure?
  3. Why are latency percentiles often more useful than averages?
  4. State two risks of indiscriminate caching.
  5. What information do logs, metrics and traces provide?

Long Answer Questions

  1. Explain how browser rendering, JavaScript execution and asset delivery affect Core Web Vitals.
  2. Compare vertical and horizontal scaling, including their effects on state management and failure domains.
  3. Discuss database indexes, query plans, connection pools and the N+1 query problem.
  4. Explain timeouts, retries, circuit breakers, bulkheads and graceful degradation as a coordinated resilience strategy.
  5. Describe a complete observability and capacity-planning approach for a multi-service web application.

Analytical / Application Questions

  1. An API receives 200 requests per second and has an average response time of 0.15 seconds. Use Little’s Law to estimate the average number of requests in flight. State the assumptions and limitations of your estimate.
  2. A product page has acceptable server latency but poor LCP and CLS. Diagnose likely front-end causes and propose a prioritized measurement and remediation plan.
  3. During a campaign, application CPU is moderate but database connections, queue depth and p99 latency rise sharply. Analyze possible causes and design a scaling and resilience response without relying only on adding web servers.

Multiple Choice Questions with Answers

Question 1

Which Core Web Vital primarily measures unexpected visual movement during page loading?

A. LCP

B. INP

C. CLS

D. RUM

Correct Answer: C. CLS

Question 2

An API receives 100 requests per second and has an average time in system of 0.2 seconds. According to Little’s Law, what is the average number of requests in flight?

A. 5

B. 20

C. 100

D. 500

Correct Answer: B. 20

Question 3

Which technique most directly reduces layout shift caused by images?

A. Removing all image compression

B. Reserving image dimensions

C. Increasing database connections

D. Disabling TLS

Correct Answer: B. Reserving image dimensions

Question 4

What is a primary purpose of a CDN?

A. To replace database transactions

B. To serve content from locations closer to users

C. To guarantee zero application errors

D. To make all responses personalized

Correct Answer: B. To serve content from locations closer to users

Question 5

Which retry behavior is most appropriate for transient failures?

A. Unlimited immediate retries

B. Retries with bounded attempts, backoff and jitter

C. Retry every validation error indefinitely

D. Retry without considering idempotency

Correct Answer: B. Retries with bounded attempts, backoff and jitter

Question 6

What is the main danger of caching personalized responses as public content?

A. Higher image resolution

B. Loss of keyboard navigation

C. Disclosure of one user’s data to another

D. Lower CPU utilization

Correct Answer: C. Disclosure of one user’s data to another

Question 7

Which observability signal is best suited to showing a request’s path across multiple services?

A. Trace

B. Static asset

C. Database index

D. DNS record

Correct Answer: A. Trace

Question 8

What does horizontal scaling generally involve?

A. Adding resources to one machine only

B. Adding service instances

C. Reducing all replicas to one

D. Removing load balancing

Correct Answer: B. Adding service instances

Question 9

Which pattern limits calls to a dependency that is currently failing?

A. Circuit breaker

B. Image lazy loading

C. Tree shaking

D. Read replica

Correct Answer: A. Circuit breaker

Question 10

If two independent redundant components each have availability 0.99 and either can serve the request, what is the approximate combined availability?

A. 0.0199

B. 0.9801

C. 0.99

D. 0.9999

Correct Answer: D. 0.9999

Advanced Web Technologies
Chapter 11 of 15