A distributed query retrieves information owned by multiple services. Because each service controls its data and transaction boundary, the query cannot use cross-service database joins or one distributed ACID transaction; services must collaborate through APIs or another service pattern. section 12.0 section 12.1.5
Finding an order
A findOrder() query may combine order details and status from the Order Service, preparation status from Kitchen, delivery information from Delivery, and payment status from Accounting. Each service supplies its part through its API. section 12.1.5
Figure 12.4 Implementing a system query, such as findOrder(), in a microservice architecture can be challenging because the needed information is scattered across multiple services, yet neither distributed joins nor distributed transactions can be used.
Shows why scattered data, unavailable cross-service joins, and unavailable distributed transactions make the query challenging. section 12.1.5
Choose a collaboration pattern according to query cost and runtime requirements:
- Use API Composition as the simplest viable option: an API composer synchronously calls provider services and combines their results. It can reside in a client, gateway, Backend-for-frontend, or standalone service. section 12.4
- Prefer CQRS when composition would require too many network round trips, large data transfers, or expensive in-memory joins. A queryable view can pre-join data and avoid direct runtime dependence on provider services. section 12.4 section 13.2.2
Composition runs independent local transactions, so results may be inconsistent across providers. Use circuit breakers, time limits, and fallbacks to improve availability, and invoke providers concurrently where possible to reduce latency. Distributed queries do not provide a strongly consistent cross-service view. section 12.4 section 7.5.1
