World is now on Opti ID! Learn more

Patrick Lam
May 5, 2025
  0
(0 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
Make Global Assets Site- and Language-Aware at Indexing Time

I had a support case the other day with a question around search on global assets on a multisite. This is the result of that investigation. This co...

dada | Jun 26, 2025

The remote server returned an error: (400) Bad Request – when configuring Azure Storage for an older Optimizely CMS site

How to fix a strange issue that occurred when I moved editor-uploaded files for some old Optimizely CMS 11 solutions to Azure Storage.

Tomas Hensrud Gulla | Jun 26, 2025 |

Enable Opal AI for your Optimizely products

Learn how to enable Opal AI, and meet your infinite workforce.

Tomas Hensrud Gulla | Jun 25, 2025 |

Deploying to Optimizely Frontend Hosting: A Practical Guide

Optimizely Frontend Hosting is a cloud-based solution for deploying headless frontend applications - currently supporting only Next.js projects. It...

Szymon Uryga | Jun 25, 2025

World on Opti ID

We're excited to announce that world.optimizely.com is now integrated with Opti ID! What does this mean for you? New Users:  You can now log in wit...

Patrick Lam | Jun 22, 2025

Avoid Scandinavian Letters in File Names in Optimizely CMS

Discover how Scandinavian letters in file names can break media in Optimizely CMS—and learn a simple code fix to automatically sanitize uploads for...

Henning Sjørbotten | Jun 19, 2025 |