In this lesson, you will learn how to orchestrate multiple internal service calls to construct a single, unified response for your client.
The Problem: The "Chatty" Client
When building complex applications, a single user action often requires data from several distinct internal modules. If your client had to make separate HTTP requests to fetch user profiles, order history, and current notifications, the network overhead would degrade performance significantly. Instead of forcing the client to do the heavy lifting, we use a Composite Request Architecture.
The Solution: Response Merging
At the architectural level, a Composite Request acts as a gateway or a "composer" service. When a request hits your route, your controller logic triggers multiple internal service calls (leveraging your existing Service Layer Abstraction and Repository Pattern) simultaneously. Once these responses return, you merge them into a single JSON structure before sending it back to the client.
Consider this simplified pattern:
// A composite route handler
app.get('/dashboard', async (req, res) => {
// Execute calls in parallel to keep latency low
const [profile, orders, alerts] = await Promise.all([
userService.getProfile(req.user.id),
orderService.getRecent(req.user.id),
notificationService.getActive(req.user.id)
]);
// Merge responses into one unified structure
const compositeResponse = {
user: profile,
recentActivity: orders,
systemAlerts: alerts,
timestamp: new Date()
};
res.json(compositeResponse);
});
Why this works
By consolidating these responses, you minimize the round-trip latency between the client and the server. Because we are already using Service Layer Abstraction, these internal methods remain testable in isolation. The "composite" logic here is purely about the arrangement of data, not the logic of data retrieval itself.
This approach keeps your client implementation clean, as it only needs to understand a single data schema rather than managing the state of three separate API calls. By combining this with Request Context Propagation, you ensure that every internal service involved in the composition remains aware of the user's identity and original request metadata throughout the lifecycle of the operation.
Key Takeaway: Composite Request Architectures reduce client complexity and network overhead by aggregating data from multiple internal services into a single, cohesive response payload.