Tomek Poniatowicz
1/9/2019
As you all know, a cache is a hardware or software component that stores data (usually as a result of an earlier computation) so that future requests could be served faster. Caching is considered by many developers as one of the GraphQL weaknesses. Is that true? And why caching is so important?
We implement caching to increase the performance of our applications and decrease loads on our servers. Google observed that 53% of page visitors leave the page if it takes more than three seconds to load. A neural network trained to predict user behavior with 90% accuracy revealed that as page load time goes from one second to five, the probability of bounce increases by 90%.
Caching is an integral part of the HTTP specification that REST APIs can use. GET vs. POST caching semantics is clearly defined so that browser caches, intermediary proxies, and server frameworks can follow. Because of the way GraphQL operates, it does not follow the HTTP specification for caching and instead uses a single endpoint, so it is up to the developer to make sure that caching is implemented properly for non-mutable queries. The GraphQL page even frankly suggests that the client should take care of the caching on its end. Fortunately, there are some libraries that can help GraphQL solve this problem with the help of developers.
Choosing one of them will help you develop more performance-focused applications and reduce the number of sessions being bounced:
FlacheQL - FlacheQL offers partial retrieval of cached data based on search parameters — a feature that no other GraphQL library offers. Larger implementations like Apollo and Relay can only cache data based on GraphQL query fields.
Apollo GraphQL - Apollo includes a default cache implementation for Apollo Client 2.0. It's called InMemoryCache and it is a normalized data storage that supports all of Apollo Client 1.0’s features without the dependency on Redux. You can read more about its use-cases here.
Relay Library - Relay faciliate creation of Relay-compliant servers using the GraphQL.js reference implementation of a GraphQL server.
GraphQL definitely needs a better caching system to win the GraphQL vs REST war.