liveBook · Manning

Lazy initialization

As explained in Vibe Engineering

Lazy loading is a technique that obtains or constructs data only when it is first requested rather than during initialization.

A lazy cache starts empty and builds as requests arrive, avoiding startup work and memory use for data that may never be needed. section 7.4

Choose the tradeoff against the workload:

  • Lazy loading can reduce startup time and unnecessary memory, but the first request for an uncached item pays the loading cost. section 7.4 section 7.4.2
  • For larger datasets, use request patterns to inform expiration; percentile-based intervals can help balance cache reuse against memory use. section 7.4
  • Set explicit data-size assumptions before choosing: eager loading may be reasonable for a small dictionary, while a very large one changes the risk. section 7.4.2
Listing 7.18 Constructing a word-exists cache
LoadingCache<String, Boolean> wordExistsCache = 
    CacheBuilder.newBuilder()
    .ticker(ticker)
    .expireAfterAccess(DEFAULT_EVICTION_TIME).recordStats() #A
    .build(
         new CacheLoader<String, Boolean>() {
           @Override
           public Boolean load(@Nullable String word) throws Exception {
       if (word == null)
             return false; #B
             }
        return checkIfWordExists(word); #C
           }
          });

A word-existence cache loads a result when requested and configures expiration after access. section 7.4

How Vibe Engineering covers it

How deep it goes
Explains cache population, traffic-based expiration, and latency and memory tradeoffs, while leaving eviction choices workload-dependent.
Examples
Uses a word-existence cache over dictionary files and compares lazy caching with eager loading.
What you can do after
Evaluate eager or lazy caching using data size, request patterns, memory use, and latency constraints.
How it connects
Connects caching with eager initialization, startup time, memory usage, request latency, and cache eviction.

Also explained in

Compare how 12 books explain lazy initialization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vibe Engineering. The section links point to the parts of the book it draws on.