1. How Far We Have Traveled
Across Course 09, we systematically constructed the complete web application mental model layer by layer:
For an early-stage startup or internal tool, this linear stack is more than sufficient. You launch with 100 users, grow to 1,000 users, and eventually reach 100,000 users. But what happens when your service reaches 1,000,000+ simultaneous users?
It cannot. This is where System Architecture and High-Scale Engineering take center stage.
2. What is Web Application Architecture?
Architecture is the overarching structural blueprint that governs how every software component operates in harmony:
- How components are modularized and decoupled.
- How services communicate (REST, gRPC, event-driven queues).
- Where data resides and how persistence guarantees are enforced.
- How incoming user traffic is distributed across compute resources.
- How the overall system survives hardware crashes, datacenter power outages, and network splits.
3. The Simplest Production Architecture
Over 95% of successful applications originate on the simplest possible foundation:
In this setup, a single Virtual Private Server (VPS) hosts both the application web process and the relational database. It is straightforward, cost-effective, and ideal for prototyping.
4. What is a Monolithic Architecture?
When an entire business domain—authentication, user profiles, catalog, cart, payments, invoicing, and admin utilities—resides within a single codebase and compiles into a single deployable process, it is termed a Monolithic Architecture:
┌────────────────────────────────────────────────────────┐
│ MONOLITHIC APPLICATION │
│ │
│ [ Auth ] [ Users ] [ Catalog ] [ Orders ] │
│ [ Payments ] [ Shipping ] [ Reviews ] [ Admin ] │
└───────────────────────────┬────────────────────────────┘
│ (TCP Socket)
▼
┌───────────────┐
│ PostgreSQL │
└───────────────┘
5. Is Monolith a Bad Pattern?
Absolutely not! This is one of the most pervasive misconceptions in software engineering.
Chasing industry hype, junior engineers often assume "Monolith = legacy or bad." In reality, multi-billion-dollar global giants—including Shopify, GitHub, Basecamp, and Stack Overflow—power enormous user bases on robust, highly optimized monolithic architectures.
Pragmatic Advantages of a Monolith:
- • Rapid Development: Single Git repository; local setup requires just
npm run dev. - • Zero Operational Overhead: No need for complex Kubernetes clusters, service meshes, or distributed orchestration.
- • Simplified Testing: End-to-end tests run in-process without network hops or mock distributed endpoints.
- • Consistent Data Integrity: Native ACID transactions execute within a single database without complex two-phase commits.
6. Scaling Challenges in Growing Applications
As application scale and organizational size expand exponentially, architectural strain emerges:
- Scaling Hotspots: If search queries spike by 100x during a promotional campaign while checkout remains normal, you cannot scale search independently; you must duplicate the entire monolithic process in memory.
- Blast Radius & Single Points of Failure: A memory leak in an obscure PDF report generator crashes the entire process, taking down authentication and payment systems simultaneously.
- Team Coordination Overhead: Hundreds of engineers committing to a single repository create merge congestion, deployment lockouts, and extended CI pipeline durations.
7. Vertical Scaling (Scale Up)
When a single server encounters performance degradation, the most direct remedy is upgrading its physical hardware:
Pros: Zero code modifications; architectural simplicity is preserved.
Cons: Hardware reaches a hard physical ceiling (the largest commercially available motherboard). Moreover, high-tier server pricing increases exponentially rather than linearly.
8. Horizontal Scaling (Scale Out)
Instead of purchasing a supercomputer, horizontal scaling provisions an elastic pool of commodity server instances running in parallel:
Pros: Virtually infinite scale. When traffic surges, auto-scalers spin up 20 new containers; when traffic subsides, containers terminate to minimize cloud costs. Furthermore, if any single node suffers hardware failure, remaining nodes absorb the load without user interruption (High Availability).
9. What is a Load Balancer?
With multiple web servers active, incoming client traffic must be systematically directed to appropriate nodes.
The Load Balancer (e.g. Nginx, AWS ALB, HAProxy) operates as a reverse proxy, accepting client connections and distributing them across the backend cluster:
10. Active Health Checks
Load balancers continuously inspect backend instances via periodic health check pings (e.g. GET /healthz every 5 seconds).
If Server #3 suffers a kernel panic or out-of-memory error, the load balancer instantly evicts Server #3 from rotation, routing 100% of user traffic strictly to healthy nodes. Users never encounter broken connection errors.
11. What is a Stateless Application?
The golden architectural law of horizontal scaling: "Application Servers must be completely STATELESS."
An application server must never store user session state, uploaded files, or mutable data within its local operating system memory or local disk drive.
User Request #2 → Handled by Server B
Server B must be able to fulfill Request #2 with zero prior knowledge of Request #1.
12. Managing Distributed Session State
If local server memory cannot store login sessions, where does session data live in a multi-server cluster?
In a centralized, shared in-memory datastore—most commonly Redis:
Regardless of which server receives the incoming request, it queries the shared Redis cluster to validate the session token in sub-millisecond time.
13. What is a Cache?
Executing the same heavy SQL join across thousands of concurrent requests for popular products wastes server resources.
A Cache stores pre-computed, frequently accessed data in high-speed volatile memory, eliminating repetitive database queries.
14. The Cache-Aside Pattern
The standard caching workflow implemented in high-performance backends:
2. Application inspects cache: Check Redis key "product:100"
• CACHE HIT: Data found in RAM! Return JSON payload in 0.5ms.
• CACHE MISS: Key absent. Query PostgreSQL database, populate Redis with key "product:100" (TTL: 1 hour), and return JSON.
15. Why In-Memory Caching is Lightning Fast
Relational databases commit data to persistent non-volatile disks (NVMe/SSD), requiring 5 to 50 milliseconds of I/O latency, lock management, and query planning. Caching systems like Redis store datasets entirely in DRAM, delivering data access in microseconds.
16. The Challenge of Cache Invalidation
Computer scientist Phil Karlton famously noted:
If a merchant lowers a product price from $100 to $90 in the database, but the cache continues serving the stale $100 record, users will see incorrect prices. Systems must balance Time-to-Live (TTL) expiration with event-driven invalidation to maintain data consistency.
17. Content Delivery Networks (CDNs)
A CDN (e.g. Cloudflare, Fastly, AWS CloudFront) is a globally distributed network of edge proxy servers.
If your application server is located in North America, a user connecting from Tokyo or Dhaka would experience 200–300ms of transatlantic network latency for every image, stylesheet, and bundle. CDNs cache static assets at hundreds of edge Points of Presence (PoPs) worldwide, serving assets locally within 10ms.
18. Is a CDN a Replacement for Backends?
No. A CDN delivers static assets and cacheable public endpoints. Stateful operations, private user profiles, and transactional mutations must route through to origin backend servers.
19. Why Background Jobs are Essential
When a user completes registration, the application must issue a verification email, generate a welcome PDF, and sync analytics.
Connecting to external SMTP servers takes 2–5 seconds. Forcing the browser to wait creates an unacceptable user experience.
The Solution: Commit the database user record and reply immediately with 200 OK in 10ms. Delegate time-consuming tasks to asynchronous background workers:
Asynchronous Pipeline → Worker sends verification email in background...
20. What is a Message Queue?
A Message Queue (such as RabbitMQ, BullMQ with Redis, Apache Kafka, or AWS SQS) acts as a persistent, durable FIFO buffer between web producers and worker consumers:
The web server enqueues a lightweight message: "Job #401: Send Welcome Email to user@example.com".
21. What is a Worker?
A Worker is a dedicated backend background process that continually monitors the message queue, pulling jobs and executing them asynchronously:
↓ ↓ ↓
Fleet: [ Worker 1 ] [ Worker 2 ] [ Worker 3 ]
22. Why Message Queues are Indispensable
- Traffic Spike Absorption: If 100,000 users register simultaneously, web servers do not crash; jobs buffer safely in the queue while workers process them at a steady rate.
- Decoupling Failure: If the email gateway experiences an outage, jobs remain safely preserved in the queue rather than throwing errors to users.
23. Retries and Dead-Letter Queues (DLQ)
Background processing employs Exponential Backoff with Jitter (retrying after 1s, 2s, 4s, 8s) to avoid overwhelming recovering services.
When a job repeatedly fails after reaching maximum attempts, it is routed to a Dead-Letter Queue (DLQ) for engineer inspection.
24. Why the Database is the Ultimate Bottleneck
Application web nodes are stateless and easily multiplied from 10 to 100. Relational databases, however, maintain stateful truth, disk locks, and ACID transaction logs.
Flooding a primary database with 50,000 queries per second causes disk saturation, connection exhaustion, and cascading system failure.
25. Database Read Replicas
In modern web architectures, read queries account for ~95% of traffic, while writes account for only ~5%.
We split database roles accordingly:
- Primary Database (Writer): Processes all
INSERT,UPDATE, andDELETEoperations. - Read Replicas (Readers): Receive asynchronous replication streams from the primary and handle all
SELECTqueries across multiple read-only instances.
27. Database Connection Pooling
Establishing a fresh TCP and TLS connection to a database per HTTP request consumes significant CPU time and memory. Connection Pools (e.g. PgBouncer, HikariCP) maintain an active pool of pre-warmed database connections, reusing them across incoming requests.
28. What is a Microservices Architecture?
Decomposing a large system into smaller, independently deployable services organized around specific business domains:
Each service manages its own isolated database and communicates over the network via HTTP REST or gRPC.
29. The Realities of Microservices
Microservices introduce significant distributed systems complexity:
- Network latency, distributed transaction failures, eventual consistency challenges.
- Debugging requires cross-service correlation IDs; tracing a bug requires inspecting logs across dozens of containers.
- Substantial infrastructure management costs and dedicated DevOps teams.
30. The Modular Monolith: Pragmatic Balance
The Modular Monolith structures code into strictly defined internal modules (User Module, Order Module) within a single deployable application. You obtain clean architectural boundaries without paying the distributed network penalty of microservices.
31. What is an API Gateway?
In distributed architectures, an API Gateway serves as the unified reverse proxy portal for external clients, centralizing cross-cutting concerns:
33. Observability: The Three Pillars
Diagnosing issues across 50 production containers requires robust telemetry:
- Structured Logs: JSON-formatted event records capturing timestamps, error stacks, and user IDs.
- Metrics: Numerical timeseries graphs tracking Requests Per Second (RPS), p95/p99 latency, error rates, and CPU/RAM usage.
- Distributed Tracing: Unique trace IDs propagated across service boundaries to measure execution time across each hop.
37. Failure Handling and Timeouts
Hardware and network failures are normal in distributed environments. Core defensive strategies:
- Timeouts: If a downstream service fails to reply within 3 seconds, abort the connection. Never wait indefinitely.
- Circuit Breakers: If an external payment API fails repeatedly, temporarily trip the breaker and return fallback responses to protect server resources.
39. Complete Modern Web Architecture
The unified production topology of a high-scale web system:
[ GLOBAL CLIENTS ]
│
▼
[ CDN (Cloudflare / Fastly) ]
(Edge Static Cache & WAF)
│
▼
[ TLS LOAD BALANCER ]
(AWS ALB / Nginx Gateway)
│
┌──────────────────┼──────────────────┐
▼ ▼ ▼
[ APP NODE 1 ] [ APP NODE 2 ] [ APP NODE 3 ]
(Stateless) (Stateless) (Stateless)
│ │ │
└──────────────────┼──────────────────┘
│
┌────────────────────┼────────────────────┐
▼ ▼ ▼
[ REDIS CACHE ] [ MESSAGE QUEUE ] [ S3 STORAGE ]
(Sessions & Hits) (RabbitMQ / BullMQ) (Media Files)
│
▼
[ WORKER FLEET ]
(Emails, Reports)
│
▼
[ PRIMARY DATABASE ]
(Writes & ACID)
│ (Replication Stream)
▼
[ READ REPLICA POOL ]
(High-Speed Reads)
42. The Pragmatic Scaling Mindset
Architecture is an evolutionary progression:
Avoid premature complexity. Do not build Google-scale distributed clusters for a product with 100 users. Build clean, modular systems and scale incrementally as genuine operational bottlenecks demand.
43. 13 Core Architectural Principles
- Architecture is the Blueprint: Defining how components communicate, scale, and recover from failure.
- Start with a Clean Monolith: Monoliths are fast, productive, and operationally reliable for most products.
- Scale Up vs Scale Out: Vertical scaling upgrades server hardware; horizontal scaling provisions multiple coordinated nodes.
- Load Balancers Ensure Availability: Distribute traffic evenly and isolate failed nodes automatically.
- Stateless Compute is Mandatory: Keep application nodes stateless so any server can handle any user request.
- Centralize Session State: Use in-memory Redis clusters for shared session management.
- In-Memory Caching Relieves Databases: Read data from RAM to achieve sub-millisecond latencies.
- CDNs Bring Data to the Edge: Deliver static assets from edge locations nearest to users globally.
- Background Jobs Protect Response Times: Dequeue slow tasks outside the synchronous user HTTP cycle.
- Database Read Replicas Scale Queries: Separate read-heavy queries from transactional write operations.
- Connection Pooling Conserves Sockets: Reuse open database connections to eliminate handshake overhead.
- Observability is Non-Negotiable: Maintain logs, timeseries metrics, and distributed tracing.
- Simplicity is Strength: Avoid premature microservices; evolve architecture based on measurable bottlenecks.
Knowledge Check: Test Your Capstone Mastery
1. Why must application web servers be kept completely Stateless to achieve horizontal scaling?
2. Why should slow operations like sending welcome emails be delegated to Message Queues and Background Workers?
3. What is the primary benefit of introducing Database Read Replicas in high-traffic applications?
What's Next?
We have designed high-scale web topologies: load balancers, reverse proxies, microservices, caches, and message queues. But where does the application state actually live and persist forever? How do relational databases model data, prevent race conditions, guarantee consistency with ACID transactions, and index billions of records?
Up Next: Lesson 9.13 — Web Application Data: Database কীভাবে একটি Real Application-এর Data পরিচালনা করে?