PAI-Rec feature cache
Describes how to choose and configure the two local feature caches of PAI-Rec: FeatureViewCache caches an entire feature view so that state filtering can read it directly, whereas FeatureCache is an on-demand shared cache that the feature configurations of multiple scenarios can reuse.
PAI-Rec feature cache
The PAI-Rec engine provides two local feature caches that reduce the number of online feature read requests and the latency of filtering and feature retrieval. The two caches work in different ways and suit different scenarios, and you can use them at the same time.
-
FeatureViewCache (full view cache): loads a feature view from FeatureStore in full into the local memory of the engine and keeps it synchronized. It is currently consumed by state filtering (ItemStateFilter).
-
FeatureCache (on-demand shared cache): for the keys of a request, fetches data on demand from FeatureStore and caches it. Each cache is defined globally by name and can be reused by the feature configurations of multiple scenarios.
How to choose
|
Dimension |
FeatureViewCache |
FeatureCache |
|
Cached content |
All data rows of the specified view |
The data rows of the keys that a request actually uses |
|
Loading method |
Full scan when the engine starts or the configuration is hot reloaded, followed by continuous synchronization |
Fetches data from the source by key on first access; no further fetch after a hit |
|
Memory usage |
Proportional to the total number of rows in the view; resident and never evicted |
Bounded by |
|
Data freshness |
Stream views are synchronized incrementally within seconds; Batch views are fully refreshed based on |
Remains unchanged for |
|
Data perspective |
Read only by the join id of the view; the actual consumer is the item dimension |
user or item; the data source can be a view or a "model + entity" pair |
|
Consumer |
Currently state filtering (ItemStateFilter) |
Feature configurations |
|
Typical use cases |
Large candidate sets, state changes that must take effect within seconds, and a manageable number of view rows |
Heavy feature reads with scattered keys, where staleness within the TTL window is acceptable |
|
Not suitable for |
The number of view rows exceeds what the memory of a single machine can hold |
You need to localize an entire table, or you must always read the latest values |
Recommendations:
-
If your goal is to stop querying each item online, state changes must take effect within seconds, and the view rows fit in the memory of a single machine, use FeatureViewCache.
-
If your goal is only to reduce repeated point queries, the keys are scattered, and each request hits only a small proportion of items, use FeatureCache and control memory usage and the staleness window with
CacheSize+CacheTime. -
When you have both requirements, the two caches can coexist: state filtering uses FeatureViewCache, and feature loading for
FeatureConfs/UserFeatureConfsuses FeatureCache.
FeatureViewCache: full feature view cache
Overview
FeatureViewCache loads an entire feature view from FeatureStore into the local memory of the recommendation engine and keeps it synchronized with the source view, so that modules such as state filtering (ItemStateFilter) can read it directly. After you enable it, state filtering no longer queries each item online and hits the local cache instead.
Scenarios
We recommend that you enable it in the following scenarios:
-
State filtering depends on a real-time table (a Stream feature view), such as filtering by takedown, expiration, inventory, or moderation status that must take effect within seconds.
-
The candidate set to filter is large and the state fields are relatively stable, so a full local cache lets the engine evaluate states locally and avoids large-scale online point queries.
-
You want to reduce the online query pressure on FeatureStore or FeatureDB by consolidating frequent state queries into local reads plus background synchronization.
Use caution or choose another approach in the following scenarios:
-
The view holds a very large amount of data, far beyond what the memory of a single machine can hold.
-
The state requires only offline-level freshness, so you can use an offline view that is fully refreshed on a schedule.
Key capabilities
|
Capability |
Description |
|
Full local cache |
Loads the entire feature view into local memory at startup |
|
Real-time incremental synchronization |
Synchronizes data changes of Stream views to the local cache within seconds |
|
Scheduled full calibration |
Periodic full refresh that corrects deletions and data drift as a fallback |
|
Reuse by name |
Define a cache once globally and reference it by name in multiple places, which avoids duplicate loading |
|
Configuration hot reload |
Configuration changes take effect automatically, and the update does not block online requests |
|
High-availability degradation |
State filtering automatically passes items through when the cache is not ready, which prevents recommendation results from being emptied |
Data freshness
-
Stream (real-time) views: after the source data changes, incremental updates reach the local cache within seconds and state filtering takes effect immediately. This is the recommended choice for state filtering that requires high freshness.
-
Batch (offline) views: fully refreshed on a schedule based on
RefreshIntervalMinutes. -
The minimum full refresh interval is 60 minutes: a full scan has a cost. To avoid frequent full scans of FeatureDB, a configured value smaller than 60, including no configuration, is treated as 60. This lower bound applies only to the scheduled full calibration and does not affect the second-level incremental synchronization of Stream views.
Configure the cache definition
Define caches by name in FeatureViewCacheConfs. A single definition can be referenced in multiple places:
{
"FeatureViewCacheConfs": {
"item_status_cache": {
"FeatureStoreName": "pairec_fs",
"FeatureStoreViewName": "item_status_view",
"SelectFields": "status,is_valid,category",
"RefreshIntervalMinutes": 60
}
}
}
|
Field |
Type |
Required |
Description |
|
FeatureStoreName |
string |
Yes |
The name of a FeatureStore that is already configured in the data source configuration ( |
|
FeatureStoreViewName |
string |
Yes |
The name of the feature view in the FeatureStore project. We recommend that you use a Stream view for real-time state filtering |
|
SelectFields |
string |
No |
The fields to cache, separated by commas. Default value: |
|
RefreshIntervalMinutes |
int |
No |
The interval of the scheduled full refresh, in minutes. Both the default value and the lower bound are 60 |
Configuration validation:
-
If
FeatureStoreNamedoes not exist inFeatureStoreConfs,FeatureStoreViewNamedoes not exist in the project, orSelectFieldscontains a field that the view does not define, the cache is not created and the engine writes amodule=FeatureViewCache ... error=init failedlog entry. The state filtering that references the cache then fails during the build phase. Locate and fix the configuration based on that log entry first. -
SelectFieldsmust cover all fields used by the filter condition (FilterParams). Otherwise, the filter reads empty values.
Reference the cache from state filtering
In the ItemStateFilter section, in ItemStateDaoConf, reference the cache by setting FeatureViewCacheName:
{
"FilterConfs": [
{
"Name": "item_status_filter",
"FilterType": "ItemStateFilter",
"ItemStateDaoConf": {
"FeatureViewCacheName": "item_status_cache",
"DefaultFieldValues": { "status": 1 }
},
"FilterParams": [
{ "Name": "status", "Domain": "item", "Operator": "equal", "Type": "int", "Value": 1 }
]
}
]
}
|
Field |
Type |
Required |
Description |
|
FeatureViewCacheName |
string |
Required when you use a cache |
References a cache name in |
|
DefaultFieldValues |
map |
No |
The fallback attribute value used for evaluation when the item does not exist in the view |
|
FilterParams |
[ ]object |
No |
The state filter condition. The semantics are the same as state filtering that reads directly from FeatureStore |
Note:
-
After you configure
FeatureViewCacheName, the engine prefers the local cache and you no longer need to configureAdapterType,ItemFieldName,WhereClause, orSelectFields. -
The cache is read at the item dimension, based on the join id of the feature entity that corresponds to the view. The view that you configure must therefore use the item entity. If other modules need to read the same view, they must be integrated separately.
Availability and degradation
-
The cache is not ready because the initial load has not finished or FeatureDB has failed: state filtering automatically passes items through instead of blocking them, which prevents the recommendation results from being emptied while the cache is unavailable. The engine retries automatically every 30 seconds until the initial full load succeeds.
-
The cache configuration is removed and the reference becomes invalid: state filtering also passes items through and logs that the cache is unavailable.
-
After the cache is ready: items that truly do not exist in the view are evaluated based on
DefaultFieldValuesor empty values, which is the same behavior as state filtering that reads directly from FeatureStore. In this case, ifSelectFieldsdoes not cover the fields used byFilterParams, all items may be filtered out. -
During a configuration hot reload: reads of online requests are not blocked. The new configuration is built and warmed up first, and the engine switches to the new instance atomically only after the build succeeds. If the build fails, the engine continues to use the old instance.
Best practices
-
SelectFieldsshould include only the required fields to reduce memory usage and network overhead during loading. -
Estimate the memory of a single machine: usage is approximately the number of entries multiplied by the field size per entry. For about 100,000 entries with a few fields, usage is typically tens of MB.
-
Use semantic cache names, such as
item_status_cache, and define only one cache for the same view. -
For scenarios that require high freshness, prefer Stream views. Real-time performance is guaranteed by incremental synchronization, and the scheduled full refresh serves only as a fallback calibration, for which 60 minutes is usually enough.
FAQ
Q1: The log reports feature view cache not found, name:xxx?
The consumer cannot find the cache. Check the following items in order:
-
FeatureViewCacheConfscontains the name. The name is case-sensitive. -
The
FeatureStoreNameof the cache can be found inFeatureStoreConfs. -
FeatureStoreViewNameis a view name that exists in the project. -
Search earlier in the log for
module=FeatureViewCache ... initial load failedto find the specific cause. That log entry is the root cause.
Q2: Why is the minimum refresh interval 60 minutes, and does it affect real-time performance?
No. A full scan has a cost, and the lower bound prevents frequent full scans of FeatureDB. The second-level incremental synchronization of Stream views and the scheduled full refresh are two independent mechanisms.
Q3: If the cache has not finished loading or FeatureDB has a temporary failure, are all items filtered out?
No. When the cache is not ready, state filtering automatically passes items through, and the engine retries in the background until it recovers.
Q4: If a full scan returns an empty result, is it treated as an empty view and does the cache get cleared?
Yes. An empty scan that returns successfully is treated as a genuinely empty view and handled as a valid result: the cache clears the expired entries accordingly and is marked as ready. If the upstream data has not been produced when you go live, make sure that the view already contains data, or configure DefaultFieldValues for the filter.
FeatureCache: on-demand shared feature cache
Overview
FeatureCache is a cache that is defined centrally in FeatureCacheConfs. It is an on-demand cache: each cache definition holds both the capacity and TTL and the data retrieval source, including the data source, the view or model, the fields to read, and the SDK parameters. The feature configuration on the scenario side only references the cache name and provides the lookup key at request time, so scenarios no longer duplicate the data source and cache parameter configuration.
This feature mainly solves two problems:
-
The same features are loaded repeatedly by multiple scenarios, and each scenario maintains a private cache, so the data source and field definitions are scattered and easily inconsistent.
-
You want to control the cache capacity and expiration time of a type of feature in one place.
Scenarios
-
Multiple scenarios, or multiple
FeatureLoadConfsentries, read the same user or item features. -
Feature reads are heavy but the keys are scattered, individual point queries are expensive, and staleness within the
CacheTimewindow is acceptable. -
You want to consolidate the cache policy of a type of feature, including the capacity, TTL, and fields to read.
Not suitable when you need to localize an entire table or must always read the latest values. In those cases, use FeatureViewCache or read directly from FeatureStore.
Configure the cache definition
{
"FeatureCacheConfs": {
"user_profile_cache": {
"FeatureStoreName": "pairec_fs",
"FeatureStore": "user",
"FeatureStoreViewName": "user_profile_view",
"SelectFields": "age,city,level",
"CacheSize": 100000,
"CacheTime": 600
},
"item_profile_cache": {
"FeatureStoreName": "pairec_fs",
"FeatureStore": "item",
"FeatureStoreModelName": "rank_v1",
"FeatureStoreEntityName": "item",
"CacheSize": 50000,
"CacheTime": 3600
}
}
}
|
Field |
Type |
Required |
Description |
|
FeatureStoreName |
string |
Yes |
References a FeatureStore that is already configured in |
|
FeatureStore |
string |
Yes |
The enumeration value |
|
FeatureStoreViewName |
string |
One of the two |
The name of the feature view. If you configure this field, data is fetched by view |
|
FeatureStoreModelName |
string |
One of the two |
The name of the model. Used together with |
|
FeatureStoreEntityName |
string |
One of the two |
The name of the feature entity. Used only when you configure a model |
|
SelectFields |
string |
No |
The projection fields of the view, separated by commas. Default value: |
|
FeatureStoreOptions.DlrmHSTU |
bool |
No |
Specifies whether to enable DlrmHSTU data retrieval. Supported only from the user perspective |
|
CacheSize |
int |
Yes |
The maximum number of cache entries. The value must be greater than 0. When the cache is full, entries are evicted by LRU |
|
CacheTime |
int |
No |
The expiration time after a write, in seconds. Default value: 3600 |
Configuration validation rules. If a rule is not met, the engine reports an error at startup or when the configuration is loaded:
-
FeatureStoreViewNameis mutually exclusive withFeatureStoreModelName/FeatureStoreEntityName: in view mode you cannot configure both a model and an entity, and in model mode you must configure both a model and an entity and cannot configureSelectFields. -
In model mode,
FeatureStoreEntityNamemust be an entity that the model actually uses. A model from the user perspective must have a label table. -
If the view, model, or entity does not exist in FeatureStore, an error is reported when the configuration is loaded.
Reference the cache from a scenario
On the scenario side, in FeatureDaoConf, reference the cache by using FeatureCacheName, and keep the lookup key and destination configuration:
{
"FeatureConfs": {
"scene_name": {
"FeatureLoadConfs": [
{
"FeatureDaoConf": {
"FeatureCacheName": "user_profile_cache",
"FeatureKey": "user:uid",
"FeatureStore": "user"
}
}
]
}
}
}
|
Field |
Type |
Required |
Description |
|
FeatureCacheName |
string |
Yes |
References a cache name in |
|
FeatureKey |
string |
Yes |
The source of the lookup key, such as |
|
FeatureStore |
string |
Yes |
The destination to which features are written: |
Constraints when you reference a cache. If a constraint is not met, an error is reported when the feature DAO is built:
-
FeatureCacheNamecurrently supports only thefeaturestoreadapter: the scenario side must keep"AdapterType": "featurestore". If you specify another value such ashologres,tablestore, orredis, or if you omit the field, an error is reported when the feature DAO is built:FeatureCacheName is only supported by the featurestore adapter. On this path the field does not participate in routing (routing is determined byFeatureCacheName). The field is only validated, but it is still required.
If the features are stored in another data source such as Hologres, you cannot use the shared cache. If you need a cache, you can use only the private DAO cache of the legacy path, which you configure onFeatureDaoConfwithCacheSize/CacheTime). -
Cannot be used together with
FeatureAppendKey. -
On the scenario side, you cannot duplicate the configuration of the data source, the fields to read, and the SDK parameters (
FeatureStoreName,FeatureStoreViewName,FeatureStoreModelName,FeatureStoreEntityName,UserSelectFields,ItemSelectFields, orFeatureStoreOptions). These are provided centrally byFeatureCacheConfs. Among them,FeatureStoreNameis ignored and a warning is printed. -
FeatureStoremust be the same as the perspective in the cache definition. Otherwise, an error is reported. -
LoadFromCacheFeaturesNameon this path does not take effect (only the empty implementation for an unrecognized adapter reads back from cacheFeatures). If two feature configurations must reuse the same data, let them reference the sameFeatureCacheName. Do not use cacheFeatures as an intermediary. -
Loading sequence features from the user perspective does not support the shared cache yet. Use the view or model path.
Use it in UserFeatureConfs
UserFeatureConfs (user feature prefetch) also supports FeatureCacheName. The syntax is the same as in FeatureConfs:
{
"UserFeatureConfs": {
"scene_name": {
"AsynLoadFeature": true,
"FeatureLoadConfs": [
{
"FeatureDaoConf": {
"FeatureCacheName": "user_profile_cache",
"FeatureKey": "user:uid",
"FeatureStore": "user"
}
}
]
}
}
}
Note the following when you use it in this configuration block:
-
UserFeatureConfsrequiresFeatureStoreto beuser. Otherwise, the configuration fails to load. You can therefore reference only the cache definitions whoseFeatureStoreisuser. Caches from the item perspective cannot be reused here. -
On the scenario side, do not keep
FeatureStoreName. The shared path does not read it and prints anignoredConfigwarning. If you keep fields such asFeatureStoreViewNameorUserSelectFields, an error is reported immediately. -
The user perspective does not write back empty values: if no data is fetched from the source, the load fails for that request and no empty result is written to the cache. This differs from the item perspective.
-
Hot reload compares the signature of the entire scenario (all fields in
FeatureLoadConfsparticipate). ChangingFeatureCacheNametriggers a rebuild of that scenario.
Read and cache behavior
-
Cache hit: the cached value is used directly and FeatureStore is not accessed. From the item perspective, you can see the hit flag in the debug output.
-
Cache miss: data is fetched from the source based on
FeatureKeyand then written to the cache. Multiple items in the same request are merged into a single batch query. -
Empty results from the item perspective: items that the batch query does not return are written to the cache as empty results and are not fetched from the source again within
CacheTime. After the view data is completed, you must wait for the TTL to expire before you can read the new values, so do not setCacheTimetoo large. -
Empty results from the user perspective: if no data is fetched from the source, the load fails for that request and no empty value is written.
-
Expiration and eviction: entries expire after
CacheTimeand a hit does not extend the TTL. When the number of entries reachesCacheSize, the least recently used entries are evicted. -
Data copies: attributes read from the cache are copied only at the top level. Nested maps and slices are shared as read-only, so consumers must not modify nested values in place.
Configuration hot reload
-
If the definition is exactly the same, the original instance is reused and the existing cached data is kept.
-
If any field in the definition changes, including cache policies such as
CacheSize/CacheTime, as well as the data source, the view or model, the fields to read, and the SDK parameters, the cache instance is rebuilt and the existing cached data is cleared and warmed up again. Increasing the capacity or adjusting the TTL also causes a cold start, so we recommend that you make such changes during off-peak hours. -
If a cache definition is invalid, this hot reload fails as a whole and the original configuration is kept. Locate the issue based on the log entry
reload config error. -
A cache with the same name cannot switch between the user and item perspectives. You need a new cache name.
-
After a cache name is removed from the configuration, the feature DAOs that are already built query FeatureStore directly based on the definition used at construction time and no longer use the cache. A newly built feature DAO that references a removed cache name reports an error.
-
Online requests are not blocked during a hot reload. If the new configuration fails to build, the existing cache instance is kept.
Best practices
-
Name a cache after the business meaning of the data, such as
user_profile_cache, and use one cache for each view or model. -
SelectFieldsshould include only the fields that are actually used. In model mode, the fields are determined by the model definition and do not need to be configured. -
CacheSizeis estimated based on resident memory:number of entries × field size per entry. SetCacheTimebased on how much staleness your business can tolerate. -
On the scenario side, specify only
FeatureCacheName,FeatureKey,FeatureStore, and the destination-related configuration. Do not duplicate the data source and fields in the scenario. -
Do not use this path for features that require second-level freshness. Use FeatureViewCache or query FeatureStore directly.
FAQ
Q1: Startup reports feature cache not found, name:xxx?
The consumer cannot find the cache. Check whether FeatureCacheConfs contains the name. The name is case-sensitive. Also check whether the cache definition passes validation.
Q2: Why does the FeatureStoreName configured in a scenario not take effect?
The data retrieval source of a shared cache is held by the cache definition. On the scenario side, FeatureStoreName is ignored and a warning is printed. Configure the data source in FeatureCacheConfs.
Q3: If an item feature cannot be found, do subsequent requests keep failing to find it?
From the item perspective, keys that are not returned are cached as empty results and are not fetched from the source again within CacheTime. If the view data is completed later, reduce CacheTime or check whether the upstream writes are working properly.
Q4: Can multiple scenarios share one cache?
Yes. Caches are defined globally by name. Multiple scenarios can share the same data by referencing the same FeatureCacheName. The perspective in FeatureStore must be the same as in the cache definition.