Light entities
Light entities share storage and are queried in memory. Use them for small lists of rarely changed choices.
For the team workflow, see Reuse a list of choices.
Some entity types are small lists that change rarely and are read everywhere: the choices of a PIM's controlled vocabularies, the topics and seasons of a CMS, the return reasons and service levels of an OMS. They need ids, routes, references and editors like any entity type, but not drafts, history or indexes of their own. A light entity type is such an entity type: it's stored with the other light entity types in shared storage, kept whole in memory, and queried there.
[VoxLightEntity]
[VoxChoiceSet]
[VoxTitle(nameof(Label))]
public partial class GoogleProductCategory : IVoxEntity, IVoxSelectableChoice
{
// The ordinary IVoxEntity built-in properties are omitted here.
[VoxFilterable, VoxSortable, VoxSearchable]
public virtual string? Label { get; set; }
public virtual bool Deactivated { get; set; }
// A deactivated category isn't offered to editors, see Choice sets in Editor metadata
public virtual VoxSelectability GetSelectability() => Deactivated ? VoxSelectability.Hidden : VoxSelectability.Selectable;
}
An unused light entity type costs nothing in storage: there's no table or index per light entity type, only the shared ones, which stay empty until there are entities.
What stays the same
A light entity type keeps the contract of an entity type:
- its name, ids, route, JSON, generated actions and Content API shape;
- references to it, from properties (
VoxReference<T>) and from the fields of field sets, and its own relations, embedded or index-backed, in the relation storage of its provider; - validation, actions, imports, synchronization, permissions, operational logs, change events and subscriptions;
- rich text, whose embedded entities are kept in the entity embed index like for any entity type;
- filters, sorts, paging and text search over its filterable, sortable and searchable fields.
Moving an entity type between light and full storage, by adding or removing [VoxLightEntity], keeps all of that.
Enabling something light entity types don't have is a separate change, see below.
What a light entity type can't do
A light entity type's changes take effect immediately, so it has no authoring data: it can't be authored
(IVoxAuthoredEntity), receive the drafts and releases of a source, or be scheduled. It also can't keep history
([VoxHistory]), have unique constraints ([VoxUnique]), calculated fields or calculations rooted at it, projections
rooted at it, be a context type or a member of contexts, a singleton or an asset, and its expected count can't be
Large. A model with such a light entity type fails to build with an error that names the entity type and the
capability, also when a kind of an entity family declares it.
Calculations and projections of other entity types read light entities normally, and are recalculated when a light entity they read changes. Light entities don't need a draft to be safe to change, but a change still reaches what depends on it: relabeling or deactivating a choice that millions of products refer to is processed like any other change, in bounded work that doesn't hold up other work.
Light entity types are stored in Entity Framework storage, in one LightEntities table keyed by entity type and id.
Their values can vary by context, eg a label per language, and their relations are kept in the ordinary relation
tables. They aren't stored in Redis, which stores only transient entity types: Build()
rejects a light entity type stored there. A light type is small and read from memory, while a transient type is large
and read by index, so an entity type is never both.
Size
Light storage suits lists up to about 50,000 entities or 64 MiB of JSON per entity type. That's guidance, not a limit:
writes, imports and migrations succeed beyond it, and an error is logged once so that it doesn't go unnoticed. Configure
the guidance in VoxLightEntityOptions:
options.LightEntities.AdvisoryMaxEntities = 100_000;
options.LightEntities.AdvisoryMaxBytes = null; // no error about bytes
A light entity type is VoxEntityCount.Medium unless it's declared Small, so editors search it rather than listing
every entry as an option. A small row count doesn't make frequent writes cheap: every change is processed and fanned
out to what depends on it.
Kept whole in memory
Light entity types, and the entity types whose expected count is Small, eg markets and languages, are kept whole in
memory, in a cache that every application instance has. Turn it on or off per entity type:
options.EntityStorage.UseWholeTypeCache<Market>(); // the default for a small entity type
options.EntityStorage.UseWholeTypeCache<Brand>(); // a full entity type that's read often
options.EntityStorage.UseWholeTypeCache<Language>(false); // read from storage instead
The cache is canonical data, the same for every caller. Permissions, contexts and the read scope of a caller are applied to what it reads from it, and every read gets copies that it may change. Reads in a draft, a release or contexts, and reads in a transaction, read the storage.
Every write of an entity type that's kept whole records its changes in a change inventory in its storage, in the
transaction of the write: a sequence per entity type and the latest change of each entity. When the write commits, its
host is told the new sequence at once, so its own reads see its writes, and the other hosts are told with an instance
event. An interactive read, eg of the API or the admin, uses the cache as it is unless the host was told of a later
sequence, and reads nothing to check it. A host that missed an event catches up at a periodic check of the inventory
(VoxLightEntityOptions.RefreshInterval, every minute by default). A batch of durable processing, eg a recalculation,
still reads the sequences once before its reads, since what it computes is stored: when an editor relabels a choice on
one host and a recalculation reads it on another that hasn't had the event yet, the recalculation catches up first.
When the cache is behind, it catches up on the entities that changed, in bounded pages, without reading the rest. An entity type is read whole the first time it's read, after it's evicted, after
its model changes, or when an instance has been idle for longer than the inventory keeps deletions
(VoxLightEntityOptions.ChangeRetention, a day by default), whose older deletions one processing host prunes every
ChangeRetentionInterval, an hour by default. Changing whether an entity type is kept whole needs no data
migration, only a new model revision, so every instance that writes it records its changes.
The cache holds at most VoxLightEntityOptions.CacheCapacityBytes of estimated memory, 512 MiB by default, which
includes the JSON, the materialized entities and their lookups. The least recently used entity types are evicted to make
room. An entity type that doesn't fit by itself isn't kept, which logs an error with its estimated size and the
capacity, and is read from its storage instead: its reads by id still work, and its queries read it from storage, up to
MaxTemporaryLoadEntities, or fail rather than returning part of it. It's an estimate, not a guarantee of the memory of
the process.
A full entity type that's kept whole keeps its storage, history, drafts, unique constraints, calculations and indexes, and its queries are answered by its storage, never in memory. Only its published reads in no contexts are served from the cache. The context entities that contexts are selected and resolved with are derived from the cache too, so a new language or a changed fallback is seen by the host that wrote it as soon as it's committed, and by the others when its instance event arrives.
Light entity types are also the shared vocabularies of attributes: an attribute of a reference kind refers to a light entity type, eg the IP ratings that several specification attributes use.
Queries
The queries of a light entity type are evaluated in memory:
- strings are equal in any case, so
Bluematchesblue, and a text search matches every term in any case; - strings are sorted in
VoxLightEntityOptions.ComparisonCulture, the invariant culture by default. Set it to egsv-SEto sortZbeforeÅ,ÄandÖ. The same culture compares, sorts and continues a query, and a continuation from another culture isn't accepted; - accents count, so
cafedoesn't matchcafé, and so do trailing spaces. A database's collation may compare them differently, which is a difference between the layouts rather than something to rely on; - a missing value is null: it's equal to null, in no range, and sorted first. A negated equality matches it;
- a varying field is compared by its value in the contexts of the query, as full storage compares it;
- relation filters, the facets of forward relations and the filter by references are answered by the relation index of the storage, from the side of their ids, so a category with millions of products isn't read to filter categories.
Filters by draft status or assessments, and the facets of inverse relations, fail with a VoxQueryCapabilityException
rather than being ignored. A full entity type filtered by a reference to a light one, eg products by their category,
is filtered by its own storage, through its relation index.
Tracked queries of calculations record the culture they were evaluated with, so a change of a light entity is known to change their results or not without reading them again.
Moving between light and full
Adding [VoxLightEntity] to an entity type, or removing it, is planned by model evolution as a
move of its storage, which keeps its ids, versions, previous versions, cells, relations and the revisions of its links:
- The new layout is created, and from then on every write maintains both layouts.
- The entities are copied a batch at a time, without overwriting what a write copied meanwhile, and verified.
- The entity type is routed to the new layout with a new model revision, which fences the instances with the previous model, and the old layout is removed under the destructive migration rule.
Moving to full storage builds its query storage, the columns and value tables of its filterable and sortable fields, before it's activated, and checks it once more under a short block of its writes. Copying isn't a change: no events or deliveries are written.
A light entity type has no history or unique constraints, so moving to full storage can't recreate past history, and a unique constraint added with it is validated against the duplicates that light storage allowed. Moving to light storage fails on authoring, history and unique constraints in the new model. An entity type that was authored stops being authored with the move: its drafts and releases are neither copied nor deleted, so the move can be undone.
Monitoring
Every instance logs an error when an entity type is larger than the guidance, or doesn't fit in the cache, once until it fits again. The cache counts its hits, misses, whole reads, caught-up changes and evictions, and the estimated size of each entity type, which the diagnostics of an application can report.