Why are cross service queries hard in microservices?

In a monolith, a query like "show this customer's last ten orders with shipment status and current loyalty tier" is a single SQL statement. The database joins three tables, applies one consistent snapshot, and returns rows. Microservices take that option away on purpose.

The database-per-service pattern gives each service exclusive ownership of its data. The Orders service can't read the Shipping service's tables, and the Loyalty service can change its schema without asking anyone. That isolation is what makes independent deployment and scaling possible. It also removes two tools engineers lean on without thinking about them: cross-table joins and transactions that span all the data involved.

Once those are gone, any query that crosses a service boundary forces a decision about where data aggregation happens and how stale the result is allowed to be. There are two broad answers:

  • API composition: at request time, a composer calls each owning service and merges the responses in memory.
  • CQRS read model: ahead of time, a dedicated store subscribes to events from the owning services and maintains a denormalized view shaped for the query.

Composition keeps data at its source and adds latency and failure points to every request. A read model makes reads fast and simple, but you take on event pipelines, a second copy of the data, and eventual consistency.

This article treats API composition as the default. Its costs are visible and local. Move to a CQRS read model only when a specific query shows that composition can't meet its latency, availability, or filtering requirements.

Who should use API composition or a CQRS read model?

These patterns are for teams that already run, or are moving to, a database-per-service architecture and need to answer questions that span more than one service's data. If your services still share a database, a join is usually simpler and you don't need either pattern yet.

API composition suits most teams starting out:

  • Teams building a backend-for-frontend or gateway route that assembles a screen from a few services.
  • Endpoints that look up data by ID or by a key one service owns, with modest fan-out.
  • Organizations that don't yet operate a message broker, or don't want to own one for a single query.
  • Data that must be current at read time, such as live stock levels or payment status.

A CQRS read model suits teams with a specific query that composition demonstrably can't serve:

  • Search, reporting, or list screens that filter and sort across fields owned by different services.
  • High-traffic read endpoints whose load would overwhelm the owning services.
  • Endpoints with latency or availability targets stricter than the combined fan-out can deliver.
  • Teams that already run event infrastructure and have someone who will own the projector, the store, and the lag alerts.

If no one on the team can name which of these applies, that is a signal to stay with composition.

How does it work?

Both patterns answer the same question: where and when does the cross-service join happen?

  • API composition joins at read time. A request arrives at a composer. The composer calls each owning service's API, ideally in parallel and with batched lookups, waits for the responses within a deadline, merges them in memory, and returns the result. Nothing is stored. The answer is as fresh as the owning services, and as slow and as available as the calls it depends on.
  • A CQRS read model joins at write time. When an owning service changes state, it records a domain event through a transactional outbox or change data capture. A broker delivers the event to a projector, which updates a denormalized table shaped for the query. When a request arrives, the query API reads that table and never calls the owning services. Reads are fast and isolated from upstream failures, but they lag behind the source by however long the pipeline takes.

The rest of this article covers each mechanism in detail, then how to choose between them.

How does API composition work?

A composer is the component that receives the query, calls each owning service through its public API, and joins the responses in memory. It can be a backend-for-frontend owned by the UI team, a route in the API gateway, or a dedicated query service when several clients need the same view. Wherever it lives, it owns no data. It only knows which service answers which part of the question.

Parallel versus dependent calls

Calls that need only the original request parameters, such as fetching an order and the customer's loyalty status, should run concurrently. The total latency is then roughly that of the slowest call rather than the sum of all of them. Some calls depend on an earlier result: you cannot fetch product details until the order tells you which product IDs it contains. Each dependent step adds a full round trip, so keep those chains short.

Batching and timeouts

When an order has 40 line items, calling the catalog service 40 times is the N+1 problem moved across a network. Owning services should expose batch endpoints that accept a list of IDs and return all matching records in one response. Give every downstream call its own timeout, set below the composer's overall deadline. That way one slow dependency cannot use up the whole latency budget, and the composer can still return a partial response.

The example below fetches the order and customer in parallel, each with its own timeout. It then resolves all line items with a single batched catalog request.

Why it is cheap

Composition needs no new infrastructure: no broker, no projection store, and no replay tooling. Each piece of data has one source of truth, so there is no copy to drift out of sync. Ownership is also clear. Each service team keeps its API, and the composer's team owns the join logic.

// Placeholder internal service URLs
const ORDERS = "";
const CUSTOMERS = "";
const CATALOG = "";

type LineItem = { productId: string; quantity: number };
type Product = { id: string; name: string; price: number };

// Each downstream call gets its own timeout, set below the composer's overall deadline
async function getJson(url: string, timeoutMs: number): Promise<any> {
  const res = await fetch(url, { signal: AbortSignal.timeout(timeoutMs) });
  if (!res.ok) throw new Error(`${url} -> ${res.status}`);
  return res.json();
}

// customerId comes from the request (e.g. the authenticated user), so both calls are independent
export async function getOrderView(orderId: string, customerId: string) {
  // Independent calls run concurrently: latency is roughly the slowest call, not the sum
  const [orderRes, loyaltyRes] = await Promise.allSettled([
    getJson(`${ORDERS}/orders/${encodeURIComponent(orderId)}`, 300),
    getJson(`${CUSTOMERS}/customers/${encodeURIComponent(customerId)}/loyalty`, 200),
  ]);
  // The order is required; without it there is nothing to compose
  if (orderRes.status === "rejected") throw orderRes.reason;
  const order = orderRes.value as { id: string; lineItems: LineItem[] };

  // Dependent step: one batched catalog request instead of one call per line item (N+1)
  const ids = [...new Set(order.lineItems.map((li) => li.productId))];
  let catalogFailed = false;
  const products: Product[] = ids.length === 0 ? [] : await getJson(
    `${CATALOG}/products?ids=${ids.map(encodeURIComponent).join(",")}`, 300,
  ).catch(() => { catalogFailed = true; return []; }); // degrade instead of failing the whole view
  const byId = new Map(products.map((p) => [p.id, p]));

  // In-memory join; the composer owns no data, only the shape of the answer
  return {
    orderId: order.id,
    loyalty: loyaltyRes.status === "fulfilled" ? loyaltyRes.value : null,
    lineItems: order.lineItems.map((li) => ({ ...li, product: byId.get(li.productId) ?? null })),
    partial: loyaltyRes.status === "rejected" || catalogFailed,
  };
}

A TypeScript composer fetches the order and loyalty status in parallel with per-call timeouts. It then resolves all line items with one batched catalog request and returns a partial response flag when an optional dependency fails.

When does API composition break down?

The trouble starts when a query filters, sorts, or paginates on fields that live in different services. Take "orders over $500 for customers in the EU, sorted by customer name, page 3." The order service can filter by amount but knows nothing about region or names. The customer service knows region and names but not order totals. The composer has to pull every matching order and every EU customer, join them in memory, sort, and then slice out page 3. Most of what it fetched is thrown away. Deep pages and broad filters make this worse, and memory use in the composer grows with the result set, not with the page size.

Latency is the second problem. A composed response is only as fast as its slowest call. Sequential dependencies, where you fetch orders first and then customers by ID, add their latencies together. Fan-out across many instances pushes the response toward each service's p99.

Availability compounds in the same way. Illustrative estimate: if five required dependencies each succeed 99.9% of the time and fail independently, the composed request succeeds about 0.999⁵ ≈ 99.5% of the time. That is roughly five times the failure rate of any single service.

This is why degradation strategy matters more than each service's uptime. Useful options include:

  • Returning partial responses with the missing fields marked.
  • Falling back to defaults.
  • Serving cached reference data.

Retries need care. During an incident, every layer retrying a slow service multiplies traffic exactly when that service can least handle it. Use budgets, backoff with jitter, and circuit breakers.

How does a CQRS read model work?

A CQRS read model inverts the timing of the join. API composition fetches and merges data when the request arrives. A read model does that work ahead of time, as data changes, so a query only has to read a precomputed result.

Each owning service publishes domain events when its state changes, such as OrderPlaced, PaymentCaptured or ShipmentDispatched. To avoid losing an event when the database commit succeeds but the publish fails, services usually write events through a transactional outbox or expose them through change data capture (CDC) on their own database. A projector consumes these events from the broker and writes a denormalized store shaped for one query, or for a small family of closely related queries. The query API reads only from that store and never calls the owning services.

 Orders svc     Payments svc    Shipping svc
     |               |               |
 [outbox/CDC]   [outbox/CDC]    [outbox/CDC]
     \_______________|_______________/
                     v
                  Broker
                     |
                     v
                 Projector
                     |
                     v
          Read store (order_summary)
                     |
                     v
                 Query API

The store is shaped for the query rather than for the domain, so the read becomes a single indexed lookup with no fan-out. Its latency and availability no longer depend on the slowest upstream service. The cost is that the store lags behind the source of truth and carries a second copy of the data.

CQRS does not require event sourcing. The owning services can keep ordinary mutable tables and still publish events through an outbox or CDC. Event sourcing makes rebuilding a projection easier because the full event history is available, but CQRS works without it.

+--------------+     +--------------+     +--------------+
|  Orders svc  |     | Payments svc |     | Shipping svc |
|  orders DB   |     | payments DB  |     | shipping DB  |
+------+-------+     +------+-------+     +------+-------+
       |                    |                    |
+------+-------+     +------+-------+     +------+-------+
|  outbox/CDC  |     |  outbox/CDC  |     |  outbox/CDC  |
+------+-------+     +------+-------+     +------+-------+
       |                    |                    |
       | OrderPlaced        | PaymentCaptured    | ShipmentDispatched
       |                    |                    |
       +--------------------+--------------------+
                            |
                            v
                 +---------------------+
                 |       Broker        |
                 +----------+----------+
                            |
                            v
                 +---------------------+
                 |      Projector      |
                 +----------+----------+
                            |  upsert denormalized rows
                            v
                 +---------------------+
                 |     Read store      |
                 |   (order_summary)   |
                 +----------+----------+
                            ^  single indexed lookup
                 +----------+----------+
                 |      Query API      |  (never calls owning services)
                 +---------------------+

Owning services publish domain events through an outbox or CDC to a broker, a projector builds a query-shaped order_summary store ahead of time, and the query API reads only from that store.

How do you implement a reliable CQRS read model?

The hard part of a CQRS read model is getting events out reliably. A service that commits to its database and then publishes to the broker has two failure windows. If the process crashes after the commit, the event is lost. If the publish succeeds but the commit rolls back, consumers see an event for a change that never happened. Retrying the publish fixes the first case but causes the second problem: duplicates.

The transactional outbox removes the dual write. The service inserts the event into an outbox table in the same transaction as the business change, so both commit or neither does. A relay process, or change data capture reading the database log, then publishes outbox rows. Delivery is at-least-once, and events can arrive out of order across partitions or retries.

The projector therefore has to be idempotent. Each event carries the entity's version, and the upsert writes only when the incoming version is higher than the stored one. Duplicates and stale events become no-ops.

Operating projections

  • Rebuild by replay. Create a new table, replay events into it from the beginning, then switch reads over to it.
  • Treat event schemas as a public contract. Add fields. Never repurpose existing ones.
  • Expose projection lag as a metric and alert on it.
  • Backfill new fields with a targeted replay.

Projection bugs need extra care because they rarely raise errors. A bad mapping or a skipped event type still writes plausible rows, so the read model returns wrong data with no visible failure. Reconciliation checks against the source services are the only reliable way to catch this.

-- Write side (PostgreSQL): business change and outbox row commit atomically.
-- A relay or CDC connector later publishes outbox rows to the broker.
BEGIN;

WITH updated AS (
  UPDATE orders
  SET status = 'shipped', version = version + 1
  WHERE id = 'ord_123'
  RETURNING id, status, version
)
INSERT INTO outbox (aggregate_type, aggregate_id, event_type, payload)
SELECT 'order', id, 'OrderShipped',
       jsonb_build_object(
         'schemaVersion', 1,      -- event contract version: add fields, never repurpose
         'orderId', id,
         'status', status,
         'version', version       -- entity version the projector keys on
       )
FROM updated;

COMMIT;  -- both rows persist, or neither does: no lost or phantom events

-- Read side: idempotent projector upsert, run once per delivered event.
-- $1 = orderId, $2 = status, $3 = version taken from the event payload.
INSERT INTO order_summary (order_id, status, version, updated_at)
VALUES ($1, $2, $3, now())
ON CONFLICT (order_id) DO UPDATE
SET status     = EXCLUDED.status,
    version    = EXCLUDED.version,
    updated_at = now()
-- Duplicates (same version) and stale out-of-order events (lower version)
-- match no row here, so they become no-ops instead of overwriting newer state.
WHERE order_summary.version < EXCLUDED.version;

A PostgreSQL outbox insert that commits in the same transaction as the business update, and a version-guarded upsert that makes the projector ignore duplicate and out-of-order events.

How do you handle eventual consistency for users?

The most visible symptom of a lagging read model is a user who saves something, lands on a list page, and doesn't see it. They assume the save failed and retry, which can create duplicates. Fixing this is mostly a read-your-own-writes problem, and you don't need the whole system to be strongly consistent to solve it.

The cheapest fix is to return the command result directly. The write service already knows the new state, so the response can carry it, and the client renders that instead of re-querying the read model. This covers the "I just edited this" screen but not a separate list or dashboard.

For those, version tokens help. The command returns a version or event sequence number. The client passes it on the next query, and the read side either waits briefly until its projection has caught up to that position or returns a "not yet current" signal the client can act on. Bound the wait. An unbounded wait turns projector lag into request timeouts.

A third option is to route reads to the source service for a short window after a write, or only for the record the user just changed. This works, but it puts load back on the service you were trying to protect.

Where staleness matters, such as balances, inventory, or approvals, show it. An "updated 40 seconds ago" label or a pending badge on unconfirmed items tells users what they're looking at.

Teams consistently underestimate this work. Deciding which screens need which guarantee, designing pending states, and writing copy for them are product decisions, not just backend ones. Plan them as such.

How can these patterns save engineering time?

Choosing deliberately between composition and a read model saves time mainly by avoiding work you don't need and by keeping the work you do need in one place.

Starting with composition avoids building infrastructure prematurely. A composer is ordinary application code calling existing APIs. There is no broker to provision, no outbox relay to run, no projection to rebuild, and no lag alert to tune. For queries that composition serves well, that infrastructure would be pure carrying cost: code to maintain, dashboards to watch, and incidents to handle.

Centralizing the join removes duplicated client logic. Without a composer, each client (web, mobile, partner integrations) tends to call the same set of services and stitch the results together itself. Every client then reimplements batching, timeouts, partial-failure handling, and the mapping between services. Putting that logic in one composer or one read model means a bug is fixed once and a new client gets the behavior for free.

Keeping service contracts stable reduces coordination. Because neither pattern lets consumers reach into another service's tables, owning teams can change their schemas without coordinating releases with every reader. The composer depends on public APIs, and the projector depends on published events treated as a contract, so internal refactors stay internal.

A read model, where justified, stops repeated firefighting. When a cross-service list or search endpoint keeps causing timeouts, cascading failures, or load spikes on owning services, engineers spend time on caching layers, retry tuning, and incident response that only treat symptoms. Moving that one query to a read model replaces recurring operational work with a one-time build plus ongoing, predictable maintenance.

Scoping read models to one use case limits blast radius. A narrow projection owned by one team can be rebuilt, changed, or deleted without negotiating with other consumers. A shared, general-purpose query store accumulates dependencies, and every change to it becomes a cross-team project.

Illustrative estimate, with assumed inputs: suppose three clients each implement the same order-view aggregation, and each implementation needs an assumed two engineer-days per quarter for fixes and changes in upstream APIs. That is an assumed six engineer-days per quarter. Moving the aggregation into one composer would bring that to roughly two engineer-days, plus whatever the composer itself costs to run. The real numbers depend entirely on your codebase. The point is that the saving scales with the number of clients that would otherwise duplicate the logic.

When should you use API composition vs a CQRS read model?

Default to API composition. It keeps each service the single owner of its data and adds no new infrastructure. When it gets slow, add response caching and batched lookups (one call per downstream service per request rather than one per item) before adding anything else. Build a CQRS read model only when you can name the failure it fixes:

  • Cross-service filtering or sorting. Example: "orders over $500 from customers in the EU, sorted by last shipment date." No single service can evaluate that predicate, and paging through every service to find matches does not scale.
  • Latency or availability budgets. The fan-out's slowest call, or the combined failure rate of all the calls, exceeds what the endpoint is allowed.
  • Query load. Read traffic would overwhelm the services that own the data.

Hybrids are common. You can project the fields you filter and sort on into the read model, then compose volatile or sensitive fields, such as live stock levels or payment details, from their owning services at request time.

Count the full cost of a read model before you commit. You take on a new store, a projector, a broker dependency, lag alerts, rebuild runbooks, and an owner for all of it. Copied data also creates compliance work. Erasure requests have to reach every copy, residency rules apply to every store, and access control has to be enforced again on the projected data. Finally, a shared query service that every team feeds becomes a new monolith. Scope each read model to a single use case and give it a single owning team.

API composition Composition + cache/batching CQRS read model
Consistency Current Stale up to cache TTL Eventual (projector lag)
Latency Slowest call in fan-out Lower on cache hits Single local query
Availability coupling High Medium Low at query time
Query flexibility Per-service only Per-service only Cross-service filter/sort
Operational cost Low Low–medium High
Ownership Owning services Owning services Dedicated team

How OneEnv can help

Disclosure: OneEnv is the product behind this blog.

OneEnv (oneenv.io) is a workspace-based governance platform for the things services share — environment variables, database structure, API routes and validation rules. A change to a shared value is staged on a branch, reviewed once, and then every affected service explicitly approves or rejects it before it goes live, so you can see who a change touches before it ships instead of finding out in production.

Applied to cross service queries microservices, this is where it helps:

Saves time

  • Every service that inherits a shared value is listed in one place, so nobody searches repos and chats to find out who is affected.
  • One review per branch replaces coordinating each team separately; each service still keeps its own approve/reject.
  • Changing a route's URL or method automatically notifies the services that consume it — no hunting through repos for callers.

Saves cost

  • Fewer production incidents and rollbacks caused by a shared value, route or column changing under a consumer unannounced.
  • Less duplicated configuration to maintain: one shared definition inherited by many services.
  • Consumers learn about a breaking change at review time rather than from production errors.
  • OneEnv has a free plan (1 workspace, unlimited services and members, full governance), so trying it costs nothing.

These are qualitative benefits, not measured results — how much you save depends on how many services share the value.

Want to try it? Start at oneenv.io.