> For the complete documentation index, see [llms.txt](https://gia-duong-duc-minh.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gia-duong-duc-minh.gitbook.io/notes/computer/microservices/api-composition.md).

# API Composition

## Context

You have applied the \[\[Microservices architecture pattern]] and the \[\[Database per service]] pattern. As a result, it is no longer straightforward to implement queries that join data from multiple services.

## Problem

How to implement queries in a microservice architecture?

## Solution

Implement a query by defining an *API Composer*, which invoking the services that own the data and performs an in-memory join of the results.

![](https://502244816-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXPmru3MV7ngRA9hdKlzd%2Fuploads%2Fgit-blob-a11b4330c1c63cffd93eb00207ebcc2653fb0dc6%2FApiBasedQueryBigPicture.png?alt=media)

## Example

An API Gateway often does API composition.

## Resulting context

This pattern has the following benefits:

* It a simple way to query data in a microservice architecture

This pattern has the following drawbacks:

* Some queries would result in inefficient, in-memory joins of large datasets.

## Related patterns

* The \[\[Database per Service]] pattern creates the need for this pattern
* The \[\[CQRS]] pattern is an alternative solution
