
When you tap “Place order,” the browser does not talk directly to PostgreSQL. The request crosses networking, edge security, routing, application code, caches, databases, and often an asynchronous event pipeline before the user sees a response. Understanding that path gives you a practical mental model for debugging and system design.
This guide follows one request through a modern backend application architecture, then follows the asynchronous work that continues after the response. It connects the site’s deeper guides on what happens when you type a URL, API gateways, caching, message queues, and backend interview preparation.
Table of Contents
Open Table of Contents
- The End-to-End Picture
- Step 1: Browser, DNS, and the Edge
- Step 2: Load Balancer and API Gateway
- Step 3: Application Service Request Handling
- Step 4: Cache and Database Reads
- Step 5: Writes, Transactions, and Events
- Step 6: Workers and Object Storage
- Step 7: The Response and Observability
- What Happens When a Dependency Fails
- How to Choose the Right Components
- Interview Questions
- 1. What is the difference between an API gateway and a load balancer?
- 2. Why use both a cache and a database?
- 3. When should work move to a queue?
- 4. How do you prevent duplicate processing?
- 5. What happens if the application instance crashes after a database commit?
- 6. How would you scale this across regions?
- Conclusion
- References
- YouTube Videos
The End-to-End Picture
The exact products vary, but the responsibilities are remarkably stable:
flowchart TD
A[Browser or Mobile Client] --> B[DNS]
B --> C[CDN and WAF]
C --> D[Load Balancer]
D --> E[API Gateway]
E --> F[Application Service]
F --> G{Cache hit?}
G -->|Yes| H[Return cached data]
G -->|No| I[(PostgreSQL or NoSQL)]
I --> F
F --> J[Outbox transaction]
J --> K[Kafka or Queue]
K --> L[Background Worker]
L --> M[Object Storage or External API]
M --> L
H --> N[Response]
F --> N
N --> A
L -. failure .-> O[Retry and DLQ]
O --> K
classDef client fill:#e3f2fd,stroke:#1565c0,stroke-width:2px,color:#000000;
classDef edge fill:#ede7f6,stroke:#4527a0,stroke-width:2px,color:#000000;
classDef data fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px,color:#000000;
classDef async fill:#fff3e0,stroke:#ef6c00,stroke-width:2px,color:#000000;
class A,N client;
class B,C,D,E,F,G,H edge;
class I,J data;
class K,L,M,O async;
The synchronous path should contain only the work needed to answer the user safely. The asynchronous path handles work that can be retried, batched, or completed after the response, such as email, analytics, search indexing, media processing, and shipment notifications.
Step 1: Browser, DNS, and the Edge
The browser first resolves the domain, negotiates a secure connection, and sends an HTTP request. A local DNS or connection cache may make this fast; a cold navigation includes resolver, TCP or QUIC, and TLS work. The URL lifecycle guide covers those protocol details in depth.
At the edge, a CDN can serve static JavaScript, CSS, images, and cacheable API responses without waking the application. A WAF filters malicious patterns and a rate limiter limits abusive traffic. These controls improve availability because expensive requests are rejected or served before they consume application threads.
The edge must not cache personalized or state-changing responses casually. A public product document may be cacheable by URL and version; an account balance or order mutation needs authorization and a clear cache policy.
Step 2: Load Balancer and API Gateway
The load balancer distributes connections across healthy application instances, usually across availability zones. Health checks should test whether an instance can serve real traffic, not only whether its process is listening. The load balancing guide explains the difference between routing traffic and detecting health.
An API gateway provides a policy boundary: authentication, authorization context, routing, request size limits, quotas, and correlation IDs. It may aggregate calls for a client, but it should not become the only place where business authorization happens. The owning service still checks whether a buyer can read a cart or a seller can edit a product.
An incoming request might look like this:
POST /v1/orders HTTP/1.1
Host: api.example.com
Authorization: Bearer <token>
Idempotency-Key: 5d2c...
X-Request-Id: req_8a1...
Content-Type: application/json
{"cart_id":"cart_42","shipping_address_id":"addr_7"}
The gateway validates the envelope and attaches trusted identity and tracing context. It does not decide whether the inventory is available or whether the order can be canceled; those decisions belong to the domain service.
Step 3: Application Service Request Handling
The application instance typically processes a request in layers:
- Middleware checks authentication context, request IDs, deadlines, and input size.
- Handler or controller validates the request shape and maps it to a command.
- Service layer applies business rules and coordinates repositories or downstream clients.
- Repository or data access layer executes bounded queries and transactions.
- Serializer returns a stable response contract and safe error code.
Keep the application stateless when possible. Session state can live in a shared store, and any instance can handle the next request. That makes horizontal scaling and zone failover simpler, but it also means the service must not hide critical state in local memory.
Set a deadline for the whole request and pass the remaining budget to downstream calls. Without a deadline, one slow database or payment provider can hold every request thread until the load balancer starts failing healthy-looking instances.
Step 4: Cache and Database Reads
Most read paths try a cache before the database:
- Build a versioned cache key from the resource identity and relevant authorization scope.
- Return a fresh-enough value on a hit.
- On a miss, query the source of truth, then populate the cache with a bounded TTL.
- On cache failure, fall back to the database with concurrency limits rather than stampeding it.
This is the cache-aside pattern described in cache-aside design. It is a performance optimization, not a correctness boundary. Decide how stale the value may be, who invalidates it, and what happens if the cache is unavailable.
Use a relational database such as PostgreSQL when relationships, transactions, and constraints are central. A document or key-value store may fit flexible catalog data, high-volume sessions, or access patterns that do not need joins. Search engines serve ranking and filtering; they should not silently become the only source of truth for payments or inventory.
For a product read, the application may fetch a denormalized catalog record from Redis and return it in milliseconds. For an order write, it should use a transaction that validates ownership, records the state transition, and stores an outbox event before acknowledging success.
Step 5: Writes, Transactions, and Events
Writes need an explicit boundary. A local transaction should include the data that must change together:
BEGIN;
UPDATE orders
SET state = 'CONFIRMED',
state_version = state_version + 1,
updated_at = CURRENT_TIMESTAMP
WHERE order_id = :order_id
AND state = 'PAYMENT_AUTHORIZED';
INSERT INTO outbox_events (event_id, event_type, aggregate_id, payload)
VALUES (:event_id, 'OrderConfirmed', :order_id, :payload);
COMMIT;
The outbox prevents a dangerous gap where the database commits but the event publish fails. A relay publishes committed rows to Kafka or a queue and marks them sent. Consumers are idempotent because delivery may be duplicated.
Do not use a distributed transaction just because several services are involved. Prefer a local transaction plus an explicit workflow: reserve inventory, authorize payment, confirm the order, then emit fulfillment work. If a later step fails, compensate or reconcile according to the business invariant.
Step 6: Workers and Object Storage
Workers consume durable events after the request returns. A fulfillment worker can call a shipping provider; a notification worker can send email; an image worker can resize a file in object storage. Each worker should have:
- a bounded lease or visibility timeout
- an idempotency key derived from the event ID
- a retry policy for transient errors
- a dead-letter queue for poison messages
- metrics for queue age and processing duration
Object storage is appropriate for large, immutable or versioned blobs such as images, exports, and attachments. The application stores metadata and authorization in the database, then gives the client a short-lived signed URL when direct upload or download is safe. That keeps large byte streams away from application servers.
Step 7: The Response and Observability
The response travels back through the gateway, load balancer, CDN, and connection to the client. A successful synchronous response should say what is committed and what is still pending. For example, 201 Created can mean an order record exists; a separate fulfillment_status: PENDING prevents the client from assuming shipment is complete.
Instrument each hop with a trace ID and record structured logs with the request ID, actor, route, status, latency, and dependency outcome. Metrics should include request rate, error rate, p50/p95/p99 latency, cache hit rate, database saturation, queue age, retry count, and stale projection age. The APM guide explains why these signals complement logs and traces.
Observability is part of the architecture because an end-to-end request is a chain. If the browser is fast but p99 API latency is high, inspect the service spans. If the API is fast but the user waits for a notification, inspect queue age and worker retries rather than adding more web replicas.
What Happens When a Dependency Fails
Failure behavior should be designed per dependency:
| Failure | Safe response | Recovery signal |
|---|---|---|
| CDN unavailable | Route to origin with strict rate limits | Edge error rate |
| Cache unavailable | Bounded database fallback | Cache errors and DB saturation |
| Database timeout | Fail closed for writes; retry only safe operations | Timeout rate and pool usage |
| Kafka unavailable | Commit only if the outbox is durable; publish later | Outbox age |
| Worker crashes | Let the lease expire and retry | Oldest queue age |
| External provider times out | Keep a pending state and reconcile | Unresolved provider intents |
Retries are not automatically resilience. A retry against an overloaded dependency can multiply traffic and create a retry storm. Use deadlines, exponential backoff with jitter, circuit breakers, and a maximum attempt count. The backend interview scenarios include focused examples of dependency timeouts, retry storms, and duplicate events.
How to Choose the Right Components
Choose components from requirements and access patterns, not from a default diagram:
- CDN: static or publicly cacheable content and edge delivery.
- Load balancer: distribute connections and remove unhealthy instances.
- API gateway: shared edge policy, routing, quotas, and request context.
- Application service: business rules and domain ownership.
- Redis or similar cache: low-latency, disposable or reconstructable data.
- PostgreSQL: transactional relationships, constraints, and durable state.
- Kafka or a queue: durable asynchronous work, replay, and backpressure.
- Object storage: large files and lifecycle-managed artifacts.
- Observability stack: evidence for latency, errors, saturation, and recovery.
A modular monolith can use these responsibilities without deploying every box separately. Split services when independent scaling, ownership, deployment cadence, or failure isolation justifies the network and operational cost.
Interview Questions
1. What is the difference between an API gateway and a load balancer?
A load balancer primarily distributes network connections or HTTP requests across healthy instances. An API gateway adds application-facing policy such as authentication context, quotas, routing, request transformation, and aggregation. They can be combined in small systems, but separating the responsibilities makes scaling and failure analysis clearer.
2. Why use both a cache and a database?
The database owns durable state and correctness; the cache reduces repeated reads and tail latency. A cache can be evicted, stale, or unavailable, so the application needs a safe fallback and an invalidation or versioning policy. Never place the only copy of a payment or inventory invariant in a cache.
3. When should work move to a queue?
Move work to a queue when it can complete after the response, needs retry or backpressure, or has a different scaling profile from the request path. Email, indexing, analytics, and media processing are common examples. Keep the synchronous path responsible for the state the user must know immediately, and expose a pending state for the rest.
4. How do you prevent duplicate processing?
Give each command or event a durable identity and enforce idempotency at the consumer’s state boundary. A unique constraint, inbox table, or processed-event record can turn a duplicate delivery into a no-op. The consumer should return or reuse the original result rather than performing the side effect twice.
5. What happens if the application instance crashes after a database commit?
If the event is in the same transaction through an outbox, a relay can publish it after the instance restarts. If the application acknowledged before committing, the client must retry with an idempotency key. The design should make both outcomes observable rather than guessing from a missing response.
6. How would you scale this across regions?
Serve static and read-heavy data near users, then choose a clear write authority for each consistency-sensitive entity. Replicate projections asynchronously and use a region-aware routing policy. Active-active writes are possible, but only after you explain conflict resolution, ordering, and the business behavior during a partition.
Conclusion
- A modern backend is a request path plus an asynchronous work path, not just an application server and database.
- DNS, edge caching, load balancing, and gateways protect the application before business logic runs.
- Services should own business rules, use caches as accelerators, and keep durable writes inside explicit transactions.
- Outbox events, idempotent workers, retries, and dead-letter queues make background work recoverable.
- Traces, metrics, logs, and queue-age signals explain where end-to-end latency and failure actually occur.
The next topic in this series covers how background jobs work in web applications. For a complementary system-design exercise, compare this request path with the system design interview cheatsheet and identify which requirements would change the architecture.
References
- AWS - Building a containerized and scalable web application - https://docs.aws.amazon.com/solutions/latest/constructs/building-a-containerized-and-scalable-web-application.html
- MDN - HTTP overview - https://developer.mozilla.org/en-US/docs/Web/HTTP/Overview
- OpenTelemetry documentation - https://opentelemetry.io/docs/
YouTube Videos
- “Web Application Architecture: Full Request-Response Lifecycle” - https://www.youtube.com/watch?v=xv0Be4QfkH0