Skip to content

Layer Memoization

Effect builds a layer value at most once within a single build graph, and VibeLang preserves that rule. Reuse the same layer reference when consumers should share initialization and resources, and opt out explicitly when each consumer needs an isolated instance.

MechanismScope of sharingWhen to use
Same layer variable in one graphShared across the build graphShared pools, caches, or single initialization
Layer.fresh(layer)Built fresh per referenceIsolated state or separate resource ownership
Sibling vs. nested provideSeparate per sibling; reused when nestedLocal overrides vs. one shared entry-point boundary
Layer.makeMemoMap + Layer.buildWithMemoMapShared across explicit builds in one ScopeCustom runtimes or multi-step manual assembly

Sharing acquisition across a graph

service Prefix {
  value: string
}
service Heading {
  value: string
}
service Footer {
  value: string
}
 
const PrefixLive = Layer.effect(Prefix, effect {
  try Console.log("Prefix initialized")
  return {
    value: "Monthly"
  }
})
const HeadingLive = Layer.effect(Heading, effect {
  const prefix = try Prefix
  return {
    value: `${prefix.value} report`
  }
})
const FooterLive = Layer.effect(Footer, effect {
  const prefix = try Prefix
  return {
    value: `${prefix.value} archive`
  }
})
 
const Shared = Layer.merge(
  Layer.provide(HeadingLive, PrefixLive),
  Layer.provide(FooterLive, PrefixLive)
)
 
effect function documentLabels() {
  provide Shared {
    return [(try Heading).value, (try Footer).value]
  }
}
// Run in an effect function:
//   {
//     const jsonValue = try documentLabels()
//     try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
//   }
// Output: Prefix initialized
// Output: ["Monthly report","Monthly archive"]

Both HeadingLive and FooterLive depend on the same PrefixLive reference, so initialization logs once per documentLabels() run. Memoization keys on layer object identity rather than service type or structural equality—call a layer factory once and reuse the returned constant whenever consumers must share it.

Sharing lasts for the graph's managed lifetime, not process-wide. Scoped resources stay alive while the graph runs and release when that owning lifetime closes.

Requesting fresh acquisition

const Separate = Layer.merge(
  Layer.provide(HeadingLive, Layer.fresh(PrefixLive)),
  Layer.provide(FooterLive, Layer.fresh(PrefixLive))
)
 
effect function isolatedLabels() {
  provide Separate {
    return [(try Heading).value, (try Footer).value]
  }
}
// Run in an effect function:
//   {
//     const jsonValue = try isolatedLabels()
//     try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
//   }
// Output: Prefix initialized
// Output: Prefix initialized
// Output: ["Monthly report","Monthly archive"]

Layer.fresh bypasses memoization for that branch, forcing an independent build. Use it whenever two consumers require separate state or distinct resource ownership.

Local provision boundaries

effect function readPrefix() {
  return (try Prefix).value
}
 
effect function twoLocalReads() {
  const reads: string[] = []
  provide PrefixLive { reads.push(try readPrefix()) }
  provide PrefixLive { reads.push(try readPrefix()) }
  return reads
}
 
effect function oneSharedReadBoundary() {
  provide PrefixLive {
    const first = try readPrefix()
    const second = try readPrefix()
    return [first, second]
  }
}
// Run in an effect function:
//   {
//     const jsonValue = try twoLocalReads()
//     try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
//   }
//   {
//     const jsonValue = try oneSharedReadBoundary()
//     try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
//   }
// Output: Prefix initialized
// Output: Prefix initialized
// Output: ["Monthly","Monthly"]
// Output: Prefix initialized
// Output: ["Monthly","Monthly"]

Each sibling provide creates an independent build boundary, whereas a nested provide reuses layers already built by its enclosing boundary. Provide your application layer once at the entry point, or use ManagedRuntime when multiple entry points must share a single runtime lifetime.

Controlling memoization explicitly

effect function manualSharing() {
  return try scoped(effect {
    const memoMap = try Layer.makeMemoMap
    const owner = try scope
    const first = try Layer.buildWithMemoMap(PrefixLive, memoMap, owner)
    const second = try Layer.buildWithMemoMap(PrefixLive, memoMap, owner)
    const reads: string[] = []
    provide first { reads.push(try readPrefix()) }
    provide second { reads.push(try readPrefix()) }
    return reads
  })
}
// Run in an effect function:
//   {
//     const jsonValue = try manualSharing()
//     try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
//   }
// Output: Prefix initialized
// Output: ["Monthly","Monthly"]

Layer.makeMemoMap is an Effect value; obtain it with try Layer.makeMemoMap. Building the same layer twice with one memo map reuses the acquisition, and providing the resulting contexts performs no rebuild.

The memo map governs sharing while the Scope governs resource release. Keep both aligned with the application or test boundary, and never retain resource-owning services past their scope's close.


The matching Effect page: effect.website/docs/v4/requirements-management/layer-memoization