12 Using the API Composition pattern to implement system queries
This chapter covers
- Why distributed queries are challenging in a microservice architecture
- Implementing distributed queries using the API composition pattern
- Designing resilient and performant API composition logic
- Understanding the benefits and drawbacks of API composition
The previous chapter described how to implement distributed system commands using the Saga pattern. This pattern, which is one of the four service collaboration patterns, is needed because classic distributed transactions are not a good fit for the microservice architecture. Distributed system queries - system operations that retrieve data from multiple services - face a similar constraint. Each service is an ACID transaction boundary and owns its data and persistence, so queries cannot rely on distributed transactions or database-level joins across services.
As a result, you must also implement distributed system queries in a microservice architecture using service collaboration patterns. The two service collaboration patterns for distributed queries are API Composition, which is covered in this chapter, and CQRS, which is discussed in the next chapter. Just like the Saga pattern, they shift complexity from the infrastructure to the application.