Caching

Caching stores query results temporarily so repeated calls don't re-run the query. This is different from materialization, which pre-computes and stores results on a schedule.

How caching works

When you execute an endpoint:

  1. PostHog checks if there's a cached result that's still within the data freshness window
  2. If yes, it returns the cached result immediately
  3. If no, it runs the query and caches the result

Data freshness

Data freshness controls how long cached query results are considered fresh. Instead of configuring cache duration and materialization sync frequency separately, you set a single data freshness value and PostHog handles the rest.

For non-materialized endpoints, the data freshness value determines how long query results are cached. Once the freshness window expires, the next request re-runs the query.

For materialized endpoints, data freshness controls the materialization schedule (how often PostHog re-runs the query and updates stored results). See materialization for details.

Configuring data freshness

Set your data freshness in the Configuration tab under Data freshness. The available options are:

ValueDuration
90015 minutes
1,80030 minutes
3,6001 hour
21,6006 hours
43,20012 hours
86,4001 day (default)
604,8007 days

Shorter durations serve fresher data but consume more compute. Longer durations are faster but may serve stale results.

See endpoint execution for more detail on how this can affect your results.

Caching vs materialization

FeatureCachingMaterialization
StorageTemporaryPersistent (S3)
RefreshOn demandScheduled
Best forModerate traffic, real-time needsHigh traffic, expensive queries

Use caching when you need relatively fresh data. Use materialization when query speed matters more than freshness.

Community questions

Was this page useful?

Questions about this page? or post a community question.