GraphQL is a popular query language for reading and mutating data in APIs and is a key technology in the trend toward headless websites. It allows web builders to make their API layer flexible, powerful, and efficient. Developers face challenges, however, when caching GraphQL APIs at the network edge. For this reason, Layer0 by Limelight has developed a solution that makes edge caching easy for GraphQL, and also lets teams host the GraphQL server in a serverless environment. GraphQL caching fills a gap in the market for headless and API-first web apps and proves the value of Layer0 and Limelight joining forces.
What is GraphQL
The GraphQL query language improves on traditional REST APIs and has had tremendous growth and adoption among developers. Originally incubated at Facebook and later open sourced, GraphQL provides declarative, efficient data fetching which significantly improves performance. On top of that, the strongly typed schema between client and server enables teams to iterate their APIs faster and independently, without incurring versioning headaches.
GraphQL can be used with any backend framework or programming language. This benefit, paired with its powerful tooling (ie. strong typing and built-in support for introspection) makes GraphQL especially popular with developers from enterprise organizations using headless or API-first architectures, such as Twitter and Airbnb. In the annual state of JS survey, GraphQL had the highest scores for awareness (98%), satisfaction (94%), and interest (87%) among developers than any other data layer solution.
Caching problems with GraphQL at the Edge
Traditional CDNs and caching software has been built around REST APIs, in which only HTTP GET requests are cacheable and the cache key can be derived from the REST URL. Unfortunately, GraphQL which uses the more complex HTTP POST format and the cachibility and cache keys must be determined from the HTTP body. While it would be great if HTTP POST could be cached by a mere flip of a switch, it’s not that simple. In order to cache GraphQL, the CDN must be able to parse and understand the GraphQL format — which they cannot. As a result, GraphQL performance is a challenge for developers.
It’s common for development teams to spend time building workarounds from scratch to cache GraphQL — and even then caching isn’t optimal. Mismanaging GraphQL can lead to low cache hit rates. However if done right, caching GraphQL at the edge leads to faster, more reliable and scalable APIs. Layer0 makes this easy.
Layer0 solves caching challenges at the network edge
With Layer0’s EdgeJS, a developer can cache information such as a product ID in an eCommerce API from a GraphQL response. It can also cache attributes such as language, currency, and personalization preferences that are embedded in the request headers. These may be defined by other non-GraphQL components in the stack that haven’t been migrated to GraphQL yet.
Once cached, Layer0 gives you the ability to purge individual or collections of queries from the edge by assigning surrogate keys to each cached response. For example, in an eCommerce application developers could use EdgeJS to give teams the ability to selectively purge the cache by product ID, product category, or any other product attribute in the query by using the Layer0 console or APIs.
To understand this in more detail, watch the demo below that walks through how to convert a REST API to GraphQL and add caching at the edge, using the popular Next.js framework, Apollo Server, and Layer0.
Achieve 100% cache hit rate
No other CDN works as hard as Layer0 to enable teams to maximize their cache hit rate. EdgeJS makes it easy for developers to configure caching from within their application, enabling developers to cache the previously uncacheable.
EdgeJS is flexible and powerful, with support for modern caching features like stale-while-revalidate and surrogate keys. Our customers have already used surrogate keys to connect Layer0 to their order and content management systems so that individual product pages are cleared from the cache when inventory, price, or content changes. This lets them maintain high cache hit ratios for all the other pages and APIs that have not changed when an individual item changes.
In addition, the platform also includes automatic cache warming of the highest traffic content after a deploy. The Layer0 network intelligently coordinates routing and caching among POPs to maximize the likelihood of a cache hit. If a POP doesn’t have a piece of data, it will performantly request it from another POP before the request is forwarded to the origin server. To easily monitor website performance and cache hit rate, developers have access to observability and key metrics. For granularity, the platform proactively highlights application routes that need improvement.
Layer0’s GraphQL caching is powered by Limelight's private, global backbone. Limelight Networks is trusted by the biggest streaming companies in the world, including the Premier League Football Clubs and Disney BYJU. Limelight has over 135 Points of Presence (POP) around the globe that go beyond the average functions of a CDN and coordinate data amongst each other to warm the cache.
Serverless Hosting of GraphQL
Layer0 also offers serverless services including GraphQL hosting. Teams can manage and submit runtime calls to the GraphQL server hosted in a serverless environment on Layer0, eliminating worries about infrastructure and scale.
Boost productivity, performance, and security on Layer0
Layer0 helps web builder teams increase developer productivity, boost site performance, and improve security. Layer0 makes it easy to develop and maintain edge logic throughout the development lifecycle and deliver sub-second websites via predictive prefetching of dynamic content, making it the CDN that development teams love to use.
Layer0 includes everything development and DevOps teams need to implement a headless or Jamstack architecture including hosting, serverless, observability, RUM and CI/CD integration. Teams can be flexible in how they leverage Layer0. They can use it to cache their GraphQL APIs, run their GraphQL server in a serverless environment, or host their entire headless application. Layer0 also has powerful routing options that enable teams to gradually migrate legacy applications to headless or API-first architectures in a piecemeal fashion.
The combination of GraphQL support and compatibility with legacy systems makes Layer0 ideal for large enterprises migrating to GraphQL with a mix of traditional and modern components.