Memory cache
Factry Historian can cache time series data in memory. This speeds up queries but consumes more memory.
The cache has three parts. Each has its own setting under Cache in the general settings, and changes take effect without a restart:
- Cache points keeps recent points per measurement in memory, for the queries of calculations and event processing.
- Serve user queries from the cache also answers time series queries from users and Grafana from those points. It requires Cache points.
- Cache last values keeps the newest value of every measurement in memory, for latest-value queries.
Cache points and Cache last values work independently: turning one off does not affect the other.
From version 8.3, only measurements stored in an InfluxDB 1.x time series database are served from the cache. Queries on measurements in any other time series database always go to that database.
Cached measurements
A measurement is held in the cache when:
- it is used by a calculation or an event configuration (a consumer). It stays in the cache as long as it is used.
- from version 8.3, a user queries it while Serve user queries from the cache is on, and the cache could answer that query once it holds the data. The cache keeps at most User query cache measurement limit such measurements (1024 by default). When the limit is reached, a measurement that users have not queried recently is dropped. Measurements used by calculations and events do not count towards this limit and are never dropped for it.
Coverage
The cache does not load history from the time series database. It fills with the points written to Historian after the measurement entered the cache. A query is only answered from the cache when the cache covers its whole time range. The cache never serves a partially covered range, so a measurement that just entered the cache only answers queries over the period since then.
The cache keeps its coverage correct as follows:
- Points that arrive in timestamp order are added to the cache.
- When a batch arrives out of order, for example a collector sending buffered data after a disconnect, the cached points of that measurement are dropped. The cache fills again from the next in-order write.
- Deleting data removes the deleted range from the cache. Moving a measurement to another time series database, or changing its datatype, drops its cached points.
- Turning Cache points off drops all cached points.
The cache only sees points written through Historian. Data written to the time series database directly, from outside Historian, is not visible to the cache, and a cached range can then differ from the database.
Supported queries
Only queries with supported features are able to use the cache. The following aggregations are supported:
- Count
- First
- Last
- Sum
- Min
- Max
- Mean
- Median
- Spread
- Mode
Which means the following are not supported: Integral, Stddev, Twa. For array datatypes only First, Last and Count are supported, and for booleans Min and Max are not.
Aggregations are not the only requirement. A query is only served from the cache when it uses none of the following features:
- Grouping by all tags (*), or by more than 32 named tags. From version 8.3, grouping by named tags is supported and grouping by all tags is not.
- Fill modes other than none and null. previous, linear and 0 are not supported.
- Value filters.
- Descending timestamp order combined with a limit. From version 8.3, descending order without a limit is supported.
- A limit combined with an aggregation or with grouping.
- Query offsets.
- An open-ended time range combined with an aggregation and fill mode null.
- An aggregation that needs more than 100000 periods, counted over all measurements in the query.
A query that uses an unsupported feature is not answered by the cache but by the time series database directly, so it never returns wrong or partial data.
User queries
Available from version 8.3 and later.
With Serve user queries from the cache on, time series queries made over the REST API, the gRPC API and the Grafana datasource can be answered from the cache, under the same rules as above. Only queries that return JSON or Grafana data frames are served from the cache. Other response formats always go to the time series database.
Last values
Available from version 8.3 and later.
With Cache last values on, the cache keeps the newest point of every series (tag set) of each measurement. The memory cost is one point per series. It answers two query shapes without the time series database:
- the Last aggregation, with fill mode none or null. With a period, only when the query is descending, has a limit of 1 and uses fill mode none.
- a raw query in descending order with a limit of 1.
Such a query is only answered when it is not grouped, or when every grouped tag is fixed by a tag filter. Queries with offsets, value filters or only changes go to the time series database.
The last values do not depend on Serve user queries from the cache. They also fill the Last value column of the measurements table, and the last values the API returns on measurement and asset property lists.
The last values start empty after a restart. A measurement is only answered from them once a new point for it has been written.
Guidelines
Having the cache enabled will speed up queries, but will consume more memory. Be careful when increasing the cache size, keep an eye on the memory usage of the historian and adjust accordingly.
Changing the cache size will not have an immediate effect, the cache will slowly adjust to the new size. So if you increase the cache size it will take some time before the cache is fully populated.
Numeric and boolean data are quite consistent in their size but string data can vary based on the number of unique strings. The more unique strings the more memory is used.
Below is some data on how much memory is used by different data types when 16 million data points are cached:
Statistics
The following statistics are available in the _internal_factry database:
- cache_size: The current capacity of the cache in data points.
- cache_max_size: The maximum size of the cache in data points.
- cache_measurement_max_size: The maximum size of the cache per measurement in data points.
- cache_used: The current amount of data points in the cache.
- cache_consumer_count: The number of consumers of the cache. So the number of calculations or event workers that are using the cache.
- cache_measurement_count: The number of measurements that are in the cache.
- cache_measurement_hits: The number of measurements that were served from the cache. A query over several cached measurements increments this once per measurement, not once per query. Before version 8.3, this field was called cache_hits.
- cache_missed_by_error: The number of queries that were not served from the cache due to an error.
- cache_missed_by_feature: The number of queries that were not served from the cache due to a feature not being supported. For example an aggregation.
- cache_missed_by_range: The number of queries that were not served from the cache due to the data not being in the cache.
The following are available from version 8.2 and later:
- cache_missed_by_missing: The number of misses because the measurement was not in the cache at all.
- cache_queries_hit and cache_queries_miss: The query-level counters. A query counts as a hit only when every measurement in it was served from the cache.
The following are available from version 8.3 and later:
- cache_missed_by_database: The number of misses because the measurement is stored in a time series database the cache does not serve.
- cache_last_value_hits and cache_last_value_misses: The number of lookups that were and were not answered from the last values.
- cache_last_value_count: The number of series with a cached last value.
- cache_last_value_measurement_count: The number of measurements with a cached last value.