https://www.prisma.io/blog/improving-performance-with-apollo-query-batching-66455ea9d8b

Note: In the meantime, we released Prisma. It features an improved architecture, a more powerful GraphQL API and provides a flexible development setup. You can check it out here.

Apollo makes it easy to compose your application from individual components that manage their own data dependencies. This pattern enables you to grow your app and add new features without risk of breaking existing functionality. It does however come with a performance cost as each component will fire off uncoordinated GraphQL queries as they are being mounted by React.

In this article we will dive into transport-level query batching, an advanced Apollo feature that enables you to significantly improve performance of complex applications.

The Example App

We will use an extended version of the Learn Apollo Pokedex app to explore the performance gains query batching can provide. The original Pokedex app lists all Pokemon for a single trainer. We make the app multi-tenant by rendering the Pokedex component for each trainer. This is how the app looks like with 6 trainers

[](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/dcb72c9d-1cac-47a4-847d-5ff6d47a3f5b/0eqlu9eJJgj8vIqck.)

To really stress test Apollo, we’ll load each trainer twice!

Measuring Performance using Chrome DevTools

Chrome DevTools has a very detailed network traffic inspection feature. If you are serious about app performance take a look at the documentation. When you load the extended pokedex and filter for requests to the GraphQL backend it looks like this

[](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/2637748c-9c5e-4e40-aad3-5bbed0b4f285/0-h6XwUanLrNSYjJk.)

The first thing you notice is that Apollo is generating 12 requests. This makes sense as we are rendering 12 Pokedex components. Each request takes around 100 ms and the first 6 requests completes within 126 ms. But now something interesting happens. The following 6 requests are stalled for up to 126 ms while the first requests complete. All browsers have a limit on concurrent connections. For Chrome the limit is currently 6 concurrent requests to the same hostname, so 7 requests will take roughly double the amount of time to complete as 6 requests.

This is where Apollos Query Batching comes into play. If Query Batching is enabled, Apollo will not issue requests immediately. Instead it will wait for up to 10 ms to see if more requests come in from other components. After the 10 ms, Apollo will issue 1 request containing all the queries. This eliminates the issue with stalled connections and delivers significantly better performance

[](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/62492048-ee91-40b5-81c7-2a1ca9bf2c4c/0mv6z4N7_OSza7_2h.)

The performance of this combined query is almost as good as a single query from the first test.

Enabling Apollo Query Batching

Enabling Query Batching is super simple. The original Pokedex app looked like this:

To enable Query Batching, simply use the BatchingNetworkInterface: