Query hashing to reduce GraphQL Payload Size

Varun Sharma
Feb 15 · 3 min read

The Challenge

We at Tokopedia use GraphQL for interacting with services from our front-end clients Android, iOS and Web. Using GraphQL we send POST requests to the server and get back JSON response. But as the feature grows, size of the query POST body also grows. We wanted to reduce the payload size for our GraphQL queries due to following reasons :

  • We can save client bandwidth by sending less data to the server.

Proposed Solutions

  • Using utilities like gunzip Drawback here was we needed to check system wide compatibility.

Adopted Solution

  • Using Query Hashing to provide client an option to reduce the payload that is being sent as POST body. We adopted this solution because it allowed modular control. We can use Firebase configs to control which queries to compress. This also prevented overall system testing overhead that would have come with gunzip implementation.

Implementation Challenges

  • We needed to come up with hashing algorithm that is performant. So that we do not incur server overhead.

How it works - Client Perspective

  • For the very first request client will send the GQL POST body with a header queryhash set to true.
Client GQL interaction

Sample Payload POST body Without Query Hash

{
“meta_data_1”: “meta_data”,
“meta_data_2”: “”,
“query”: “query sampleQuery($queryParams:ParamType){query body}”,
“variables”: {
“variable_1”: “”
}
}

Sample Payload POST body With Query Hash

{
“meta_data_1”: “meta_data”,
“meta_data_2”: “”,
“query”: “”,
“variables”: {
“variable_1”: “”
}
}

Benefits

For the network speeds < 512kbps and GQL query of size > 2KB, we will save between 20–40 ms at for each network request. Also payload size reduced by > 70 percent for each such request.

Tokopedia Engineering

Story from people who build Tokopedia