How to trace a request end to end through a codebase
Follow one HTTP request from the browser to the database and back: a method for tracing execution across frontend, API, middleware, services and storage.
Tracing a request means following one action, such as clicking "Place order", through every piece of code it touches until a response comes back. It's the fastest way to understand how a system is put together, because a single trace crosses almost every layer: UI, network, routing, auth, business logic, data access and error handling.
Pick one concrete request
Not "orders", but "submitting the checkout form with a valid card". Concrete inputs decide which branches run. Write down the starting state too: logged in, cart with two items.
Layer 1: the user interface
Find the component that renders the button and its event handler. Follow the handler to the network call: a fetch, an API client function, a form submission, a Server Action or a tRPC procedure. Note the HTTP method, the URL and the payload shape.
Layer 2: routing and middleware
On the server, find what handles that URL. Then look at everything that runs before the handler:
- Global middleware (body parsing, CORS, logging, rate limits)
- Authentication and session loading
- Validation of the payload
In Express this is the order of app.use calls; in NestJS it's middleware, guards, interceptors and pipes; in Next.js it's proxy.ts and the route handler or action. Middleware is where requests are most often rejected or changed silently.
Layer 3: the handler and services
The handler usually does little itself. It calls services that hold business rules. This is where the interesting decisions live: prices, permissions, state transitions. For each call, note what it reads and what it changes.
Layer 4: data access
Find the actual queries: ORM calls, raw SQL, cache reads, calls to other services. Note whether several writes happen inside a transaction. Missing transactions are a common source of inconsistent data.
Layer 5: side effects
Many requests trigger work after the response: emails, webhooks, queued jobs, events. Look for queue add calls, event emits and after or waitUntil style functions. These paths are easy to miss because nothing calls them directly.
Layer 6: the response and errors
Follow the return value back to the client and see how the UI updates. Then follow the failure path: where errors are caught, how they're logged and what the user sees.
Record it as a numbered path
components/Checkout.tsx:88:onSubmitcallsplaceOrder(cart)lib/api.ts:31:POST /api/ordersserver/middleware/auth.ts:12: loads the session; 401 if missingserver/routes/orders.ts:20: validates the body with a schemaserver/services/orders.ts:45: computes totals, reserves stockserver/db/orders.ts:70: inserts the order and line items in a transactionserver/jobs/receipts.ts:9: queues the receipt emailcomponents/Checkout.tsx:96: redirects to the confirmation page
This list is the deliverable. It's quick to review, easy to share and makes later debugging much faster.
Confirm it
Run the request with a debugger or log lines at each step. If a step you expected doesn't run, your trace is wrong somewhere upstream, which is valuable to learn.
Getting a first draft from AI
Ask RepoGrasp "trace what happens when a user submits the checkout form, from the click to the database write" and it returns a path like the one above with file:line citations. Treat it as a draft: open each cited line, check the order, and look for side effects it didn't mention.
Common surprises
- Validation happening twice, on client and server, with different rules
- Middleware that changes the request body
- A cache that returns data before the database is touched
- Work done in an event listener registered far from the handler