Advanced 18 min read

Minimizing Server Calls and Caching Strategies

By the end of this lesson, you'll be able to:

  • Leverage Lightning Data Service's shared cache to avoid redundant server calls
  • Recognize how inconsistent parameters can defeat wire adapter caching
  • Apply manual caching for expensive client-side computations

Prerequisites: Module 12: "Secure Apex/LWC Communication and Safe DOM Manipulation"

LDS's Shared Cache, Revisited

Module 6 introduced this as a benefit; it's worth restating as a performance principle: multiple components on the same page wiring the same record (matching recordId/fields) share a single cache entry — only one server call happens, regardless of how many components asked for it. This is a real, meaningful performance win that requires zero extra code.

Avoiding Redundant Apex Calls

// These two calls will NOT share a cache entry, despite meaning the same thing:
@wire(search, { searchTerm: 'acme corp' })
@wire(search, { searchTerm: 'Acme Corp ' })

@wire caching for Apex methods (Module 7) matches on exact parameter values. Two calls with the same underlying intent but inconsistently cased or whitespace-padded parameters are treated as genuinely different requests, defeating the cache. Normalizing input (e.g. trimming and lowercasing a search term) before it reaches @wire keeps identical intents actually identical to the cache.

Manual Caching Strategies

_cachedTotal;
_cachedForData;

get expensiveTotal() {
    if (this._cachedForData === this.records) {
        return this._cachedTotal;
    }
    this._cachedTotal = this.records.reduce((sum, r) => sum + r.amount, 0);
    this._cachedForData = this.records;
    return this._cachedTotal;
}

For an expensive computation with no server involved at all, a simple memoization pattern — only recompute when the underlying input has actually changed — avoids redoing real work unnecessarily. Lesson 2 covers this exact pattern for getters specifically, in more depth.

Exercise

Explain, as a comment, why passing searchTerm.trim().toLowerCase() into a cacheable Apex wire is better practice than passing the raw, unmodified user input.

Show hint

Think about what counts as "the same request" to the wire cache.

JAVASCRIPT

Exercise

Challenge: two unrelated components on the same page both wire getRecord for the same Account Id and fields. How many server calls happen, and why?

Show hint

Recall Module 6's shared-cache behavior.

JAVASCRIPT

Minimizing Server Calls and Caching Strategies Quiz

1. How many server calls happen when two components wire the same record with identical parameters?

2. Why can inconsistent casing in a search term defeat wire caching?

3. What is a reasonable fix for the caching issue caused by inconsistent search term formatting?

4. What is manual memoization used for in this lesson's example?

5. What is often the single biggest lever for improving performance, according to this lesson?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

The fastest server call is the one that never happens — understanding how LDS and @wire caching actually work, and how to avoid accidentally defeating it, is often the single biggest performance lever available.