About Cached Queries

Query caching improves a site's performance by automatically storing frequently requested data results. When visitors trigger queries whose results are already cached, the system serves the cached data instead of querying the database again, significantly reducing response times.

The Data API automatically caches certain query results to a Content Delivery Network (CDN). Understanding when caching occurs and how to work with it helps you optimize site performance.

Cached queries

Not all queries are cached. The Data API only caches query results when specific conditions are met, ensuring that cached data remains accurate and secure.

Query results are cached when all of the following conditions are true:

  • Read-only operations: The query doesn't modify the collection or any items.
  • Native collections: The queried collection is a native Wix Data collection and not an external or app collection.
  • Show content collections: The collection permissions don't allow anonymous visitors to add, update, or delete items.
  • No hooks: The query doesn't trigger any data hooks.
  • Frontend requests: The request comes from frontend code, not backend code.
  • Site visitors: The request is made on behalf of a site visitor, not a logged-in member.
  • No PII: The request doesn't contain Personally Identifiable Information.
  • Default consistency: Eventual consistency isn't overridden with consistentRead: true.
  • No item visibility: The collection doesn't have item visibility rules enabled.

Cache invalidation

Cache invalidation ensures that visitors receive up-to-date data by automatically clearing cached results when the underlying data changes.

The cache is automatically cleared in the following scenarios:

  • Data changes: Any request modifies the collection or its items.
  • Related collections: Changes occur to collections referenced by cached queries.
  • Site publishing: The site is published with new code or configuration.
  • Time expiration: Results have been cached for a week or longer.

To optimize caching performance, consider separating collections into ones with stable data that doesn't change often and others with dynamic data that does change often. This allows the system to cache stable data for longer periods while invalidating dynamic data more frequently.

Eventual consistency

Cache invalidation can take a few seconds to complete, which may make eventual consistency effects more noticeable. If you need the most up-to-date data immediately after a change, override eventual consistency using the consistentRead parameter.

Site monitoring

The monitoring dashboard provides insights into a site's performance, including the effects of query caching.

Cached query requests are tracked in Wix's monitoring system but aren't currently displayed in the monitoring dashboard. This means:

  • Performance benefits: You'll see improved response times from caching, but the dashboard won't explicitly show which requests were served from cache.
  • Request counts: Cached requests are still counted in the site's request metrics.
  • Error tracking: Any errors in cached queries are still monitored and reported.

See also

Did this help?