Take the community feedback survey now.

Patrick Lam
May 5, 2025
  629
(2 votes)

Improving Query Performance in Optimizely Graph

As part of your onboarding with Optimizely Graph, we recommend adopting the following best practices to help improve performance and reduce query latency. Implementing these strategies can significantly enhance the efficiency of your Optimizely Graph implementation, leading to a better user experience and more reliable data retrieval.

Use Cached Templates

Cached templates allow you to store and reuse translated queries with variable placeholders. This minimizes processing overhead and helps deliver faster response times. Instead of translating the same query every time it's executed, the translated query is cached and reused. This is one of the key improvements you can make to your Graph implementation.

Example:

To enable cached templates, simply add the following to your request URL and request header.

  1. Query string parameter - Add stored=true to your request URL.
    https://cg.optimizely.com/content/v2?auth=123456789&stored=true
  2. Request header - Include the following header:
    • Key - cg-stored-query
    • Value - template

Code example showing a request using cached Templates:


-H "Content-Type: application/json" \
-H "cg-stored-query: template" \
-d '{
  "query": "query GetItem($id: ID!) { item(id: $id) { name, description } }",
  "variables": {
    "id": "12345"
  }
}'

Benefits:

  • Reduced Latency: By reusing translated queries, you avoid the overhead of repeated translation.
  • Improved Throughput: Caching allows your application to handle more requests with the same resources.
  • Lower CPU Usage: Reduced processing leads to lower CPU utilization on your servers.

Use Item Queries for Single Entities

When retrieving a single item, use the item query instead of items. This improves cache efficiency and reduces unnecessary cache invalidation. When you use the items query, Optimizely Graph may invalidate the entire cache for that content type, even if you're only retrieving one item. Using the item query ensures that only the cache for that specific item is invalidated when it's updated. This will lead to noticeable performance gains.

Example:

Code example showing an item query that retrieves a single item based on its RelativePath:


query GetItem($relativePath: String) {  
  Content(where: { RelativePath: { eq: $relativePath } }) {  
    item { Name RelativePath }  
  }  
}

Benefits:

  • Better Cache Efficiency: Only the cache for the specific item is invalidated, preserving other cached data.
  • Reduced Cache Invalidation: Minimizes unnecessary cache invalidation, leading to more stable cache performance.
  • Faster Data Retrieval: More efficient cache usage results in faster data retrieval times.

Conclusion

Following these practices will help ensure a smoother experience and more consistent performance. These improvements are essential for maintaining a high-performing Optimizely Graph implementation. If you have questions or would like support reviewing your implementation, our team is happy to assist.

Further Reading

May 05, 2025

Comments

Please login to comment.
Latest blogs
A day in the life of an Optimizely Developer - We Hacked the Future: Netcel's Opal Hackathon Adventure

Ever wondered what happens when you mix  AI ,  creativity , and a dash of competitive spirit? Welcome to the  Opal Hackathon 2025 —where we rolled ...

Graham Carr | Aug 31, 2025

Simple Feature Flags In Optimizely CMS

The Problem I was working with a CMS 11 client who wanted to introduce two new third party content sources. These would be synchronized into new...

Mark Stott | Aug 31, 2025

SQL addon for Optimizely CMS 12 updated with new features

First released in 2021, the SQL Studio addon for Optimizely CMS lets you query your database directly from the edit UI. The latest update adds...

Tomas Hensrud Gulla | Aug 29, 2025 |

🎓 New Certification Icons Are Now Live on World!

If you’ve passed an Optimizely Academy certification, you’ll now see something new beside your name on World —  fresh certification icons are live...

Satata Satez | Aug 29, 2025