Back to insights

Lightning Web Components, Data Flow & Cache Coherence

Lightning Data Service cache is not application state

JSBC Labs8 min read

Fast data is not owned state

A record page shows an Account in three components. One edits through updateRecord, one loads a summary through wired Apex and one copies the original record into local JavaScript for an inline form. After a save, two components update and the third displays yesterday’s value. The team adds a page refresh button, but the inconsistency returns whenever another automation changes the record.

The JSBC Labs position is that Lightning Data Service is a managed data-access and caching layer—not the owner of application state. It can share record data, respect platform security and keep participating components coherent. It cannot define who owns an unsaved edit, which derived values remain valid or how a custom Apex result relates to the LDS copy of the same record.

Choose one owner for each piece of state

Classify state before writing code. Server state is the Salesforce record or query result. View state includes the selected tab, expanded row and current filter. Draft state is the user’s uncommitted input. Derived state is calculated from other values. These categories have different lifecycles and should not be merged into one mutable object copied from a wire response.

Let the component closest to a UI concern own that concern, while an appropriate parent owns shared state and passes values downward. Salesforce’s LWC data-flow guidance treats public values as read-only and recommends events when a child needs the owner to change them. That one-way contract prevents sibling components from silently mutating shared references and creating state that no layer can reliably reconcile.

A wire is a stream, not a fetch promise

Salesforce documents the wire service as an immutable stream. A wire can emit from cache, emit newer data later and emit again without a component changing its configuration. The framework controls when and how provisioning occurs. Code that treats the first non-empty value as permanent initialization ignores the subscription model and leaves derived fields stale after later emissions.

Make wire handlers idempotent: derive display state from the latest value and separate genuine one-time side effects behind explicit guards. Never mutate wired objects directly; create a shallow copy for a draft that the user owns. Keep reactive configuration complete and stable, and do not update adapter configuration inside renderedCallback(), which Salesforce warns can produce an infinite loop.

Do not create competing truths

Lightning Data Service caches and shares supported record data across components. Data returned by Apex is not managed by LDS and uses a separate cache when the Apex method is cacheable. Salesforce’s data guidelines warn that fetching the same record through both Apex and LDS can produce inconsistent copies, including online and offline scenarios.

Pick one data source per record-shaped concern whenever practical. Prefer base components or LDS wire adapters for supported record operations, security-aware metadata and shared cache behaviour. Use GraphQL where it fits multi-object querying, and use Apex for unsupported objects, complex queries or genuinely transactional operations. If two mechanisms must coexist, document which result is authoritative and how each cache is invalidated after every mutation path.

Match invalidation to the data source

After an LDS-aware mutation such as updateRecord, LDS can update relevant subscribers. When imperative Apex changes records, LDS does not automatically know which cached records became stale. Salesforce provides notifyRecordUpdateAvailable() for that boundary; it accepts affected record IDs and resolves after LDS has processed the relevant wires.

For data provisioned by a wired Apex method, use refreshApex() with the value previously emitted by that wire. Do not use refreshApex() as a generic refresh button for LDS adapters—Salesforce now documents that usage as deprecated. Likewise, do not guess the cache lifetime or add dummy fields to force a miss. Invalidation should describe the real mutation and target the cache that owns the stale result.

Refresh scope is a UX decision

Reloading the whole page after every save discards filters, selection and draft context while hiding weak cache contracts. Salesforce’s RefreshView API offers a standard way to coordinate refresh across a component hierarchy, but participating custom components must still perform the appropriate refresh for their data—such as refreshApex(), refreshGraphQL() or LDS notification.

Define the smallest honest refresh scope. A single-record edit may need one LDS notification; a completed transaction that changes aggregate cards and related lists may justify a coordinated view refresh. Show progress, await completion and preserve valid view state. Avoid polling refresh functions with timers: Salesforce explicitly cautions that polling creates unnecessary server load and still cannot guarantee meaningful consistency.

Drafts need conflict semantics

A local copy is useful for an edit form, but it becomes a draft with its own lifecycle the moment the user changes it. Decide what happens when the server record changes while the draft is open: overwrite the draft, preserve it, warn the user or merge selected fields. Silent last-write-wins behaviour is easy to implement and expensive when users overwrite each other’s work.

The updateRecord function supports an ifUnmodifiedSince option based on LastModifiedDate, enabling a conflict check before update. Use that where lost updates matter, and surface a clear recovery path. Keep server snapshots, draft values and validation errors separate so a new wire emission does not accidentally erase user input—or cause an old draft to masquerade as freshly loaded data.

Test coherence across components

Test more than an isolated component receiving one mock response. Mount sibling components that read the same record, perform writes through each supported path and verify every subscriber’s final state. Exercise multiple wire emissions, delayed responses, rejected updates, out-of-band Apex changes, refresh failures, navigation away with dirty drafts and users with different field access.

Instrument unnecessary network requests and repeated refreshes as performance defects. Review new components for duplicated sources, mutable wire data and undocumented cache busting. The goal is not to force every value through LDS. It is to make server state, view state and draft state explicit enough that the page remains correct when data arrives from cache, changes elsewhere and is emitted again at any time.

Official references

Continue reading