Design a thread-safe resource loader with a size-bounded LRU cache

Quick Overview

Design a thread-safe resource loader that registers assets, loads each one from disk or a CDN exactly once, and keeps them in a size-bounded LRU cache. It tests the choice of LRU data structures, eviction with variable item sizes, status reporting, and concurrency control for simultaneous requests.

Design a thread-safe resource loader with a size-bounded LRU cache

Company: Roblox

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a thread-safe resource loader that simulates loading assets from disk or a CDN and caches them in memory. The loader should handle multiple concurrent client requests efficiently by ensuring each unique resource is loaded exactly once, even when multiple requests for the same resource arrive simultaneously. The loader maintains a bounded cache with a given total capacity (in size units). When the cache does not have enough room for a new resource, it evicts the least recently used (LRU) cached resources one at a time until sufficient space is available. If a single resource's size exceeds the total cache capacity, it cannot be loaded. Resources must first be registered (with a name, source, and size) before they can be loaded. Accessing a cached resource marks it as the most recently used. Loading is simulated: the content stored for a resource is the string `"content_of_<name>"`. Implement a class `ResourceLoader(capacity: int)` with the following methods: - `register(name: str, source: str, size: int) -> bool` registers a resource with its name, source (`"disk"` or `"cdn"`), and size. Returns `True` if newly registered, `False` if the name already exists. - `load(name: str) -> str` loads a registered resource into the cache, evicting LRU entries as needed. Returns `"cache_hit"` if it is already cached (this counts as an access), `"not_found"` if it is not registered, `"too_large"` if its size exceeds the capacity, or `"loaded_from_disk"` / `"loaded_from_cdn"` on success. - `getResource(name: str) -> str` returns the cached content string of a resource, or `"not_cached"` if it is not in the cache. Marks the resource as most recently used. - `evict(name: str) -> bool` manually evicts a specific resource from the cache. Returns `True` if evicted, `False` if it was not in the cache. - `getStatus(name: str) -> str` returns `"cached"`, `"registered"` (registered but not cached), or `"unknown"`. - `getCacheUsage() -> List[int]` returns `[current_usage, capacity]`: how much of the cache is in use and how much it can hold. - `getCachedResources() -> List[str]` returns the cached resource names ordered from least recently used to most recently used. For example, with capacity 10: ```text loader = ResourceLoader(10) loader.register("texture_a", "disk", 4) -> True loader.register("model_b", "cdn", 5) -> True loader.register("texture_a", "cdn", 2) -> False loader.load("texture_a") -> "loaded_from_disk" loader.load("model_b") -> "loaded_from_cdn" loader.getCacheUsage() -> [9, 10] loader.load("texture_a") -> "cache_hit" loader.getCachedResources() -> ["model_b", "texture_a"] loader.getResource("model_b") -> "content_of_model_b" loader.getCachedResources() -> ["texture_a", "model_b"] ``` The second `register` for `texture_a` fails and leaves its source as `"disk"`. The cache hit on `texture_a` and the read of `model_b` each move that resource to the most recently used end. Eviction, a resource that can never fit, and manual eviction, again with capacity 10: ```text loader = ResourceLoader(10) loader.register("a", "disk", 4) -> True loader.register("b", "disk", 3) -> True loader.register("c", "cdn", 6) -> True loader.register("huge", "cdn", 11) -> True loader.load("a") -> "loaded_from_disk" loader.load("b") -> "loaded_from_disk" loader.getResource("a") -> "content_of_a" loader.load("c") -> "loaded_from_cdn" loader.getCachedResources() -> ["a", "c"] loader.getStatus("b") -> "registered" loader.load("huge") -> "too_large" loader.getCacheUsage() -> [10, 10] loader.load("missing") -> "not_found" loader.getStatus("missing") -> "unknown" loader.evict("a") -> True loader.evict("a") -> False loader.getCacheUsage() -> [6, 10] loader.getResource("a") -> "not_cached" ``` Loading `c` needs 6 units while 7 of 10 are used. Reading `a` made `b` the least recently used entry, so `b` is evicted (usage drops to 4) and `c` then fits exactly. `huge` is rejected without evicting anything. ```hint Order and size together You need lookup by name and quick access to the least recently used entry, both in constant time. Pick a structure that gives you both, and keep a running total of cached sizes so you never have to sum them. ``` ```hint Exactly once Checking the cache, evicting and inserting are separate steps. Think about what a second thread asking for the same resource sees while the first thread is partway through them. ``` ### Constraints & Assumptions - `1 <= capacity <= 10000` - Resource names are non-empty strings containing only lowercase letters, digits, and underscores. - Resource sizes are positive integers. - `source` is either `"disk"` or `"cdn"`. - At most 10,000 calls in total across all methods. ### Clarifying Questions to Ask - When a second request for a resource arrives while the first load is still running, should it wait for that load, and what status should it get back? - Is the load slow real I/O, or simulated so that holding a lock while it runs is acceptable? - Which calls change recency order: only `getResource`, or also a `load` that hits the cache? - Can a registration ever be changed or removed after it is created? ### What a Strong Answer Covers - The data structures for the registry and for the LRU cache, and the cost of each method with them. - How eviction decides when to stop, and how the usage total stays correct under both automatic and manual eviction. - The order of checks in `load`, and what a failed load does to the cache. - Which methods change recency order and which only read state. - How the design loads each resource once under concurrent requests, and what it costs to hold a lock during a slow load. ### Follow-up Questions - Real loads take hundreds of milliseconds. How would you avoid holding a global lock during I/O while still loading each resource only once? - How should a load that fails or times out be handled, and what should callers waiting on it see? - How would you support pinning resources that are in use so eviction skips them? - Reloading from the CDN costs more than reloading from disk. How could eviction take reload cost and size into account?

Overview: Design a thread-safe resource loader that registers assets, loads each one from disk or a CDN exactly once, and keeps them in a size-bounded LRU cache. It tests the choice of LRU data structures, eviction with variable item sizes, status reporting, and concurrency control for simultaneous requests.

Roblox logo
Roblox
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a thread-safe resource loader that simulates loading assets from disk or a CDN and caches them in memory. The loader should handle multiple concurrent client requests efficiently by ensuring each unique resource is loaded exactly once, even when multiple requests for the same resource arrive simultaneously.

The loader maintains a bounded cache with a given total capacity (in size units). When the cache does not have enough room for a new resource, it evicts the least recently used (LRU) cached resources one at a time until sufficient space is available. If a single resource's size exceeds the total cache capacity, it cannot be loaded.

Resources must first be registered (with a name, source, and size) before they can be loaded. Accessing a cached resource marks it as the most recently used. Loading is simulated: the content stored for a resource is the string "content_of_<name>".

Implement a class ResourceLoader(capacity: int) with the following methods:

  • register(name: str, source: str, size: int) -> bool registers a resource with its name, source ( "disk" or "cdn" ), and size. Returns True if newly registered, False if the name already exists.
  • load(name: str) -> str loads a registered resource into the cache, evicting LRU entries as needed. Returns "cache_hit" if it is already cached (this counts as an access), "not_found" if it is not registered, "too_large" if its size exceeds the capacity, or "loaded_from_disk" / "loaded_from_cdn" on success.
  • getResource(name: str) -> str returns the cached content string of a resource, or "not_cached" if it is not in the cache. Marks the resource as most recently used.
  • evict(name: str) -> bool manually evicts a specific resource from the cache. Returns True if evicted, False if it was not in the cache.
  • getStatus(name: str) -> str returns "cached" , "registered" (registered but not cached), or "unknown" .
  • getCacheUsage() -> List[int] returns [current_usage, capacity] : how much of the cache is in use and how much it can hold.
  • getCachedResources() -> List[str] returns the cached resource names ordered from least recently used to most recently used.

For example, with capacity 10:

loader = ResourceLoader(10)
loader.register("texture_a", "disk", 4)   -> True
loader.register("model_b", "cdn", 5)      -> True
loader.register("texture_a", "cdn", 2)    -> False
loader.load("texture_a")                  -> "loaded_from_disk"
loader.load("model_b")                    -> "loaded_from_cdn"
loader.getCacheUsage()                    -> [9, 10]
loader.load("texture_a")                  -> "cache_hit"
loader.getCachedResources()               -> ["model_b", "texture_a"]
loader.getResource("model_b")             -> "content_of_model_b"
loader.getCachedResources()               -> ["texture_a", "model_b"]

The second register for texture_a fails and leaves its source as "disk". The cache hit on texture_a and the read of model_b each move that resource to the most recently used end.

Eviction, a resource that can never fit, and manual eviction, again with capacity 10:

loader = ResourceLoader(10)
loader.register("a", "disk", 4)    -> True
loader.register("b", "disk", 3)    -> True
loader.register("c", "cdn", 6)     -> True
loader.register("huge", "cdn", 11) -> True
loader.load("a")                   -> "loaded_from_disk"
loader.load("b")                   -> "loaded_from_disk"
loader.getResource("a")            -> "content_of_a"
loader.load("c")                   -> "loaded_from_cdn"
loader.getCachedResources()        -> ["a", "c"]
loader.getStatus("b")              -> "registered"
loader.load("huge")                -> "too_large"
loader.getCacheUsage()             -> [10, 10]
loader.load("missing")             -> "not_found"
loader.getStatus("missing")        -> "unknown"
loader.evict("a")                  -> True
loader.evict("a")                  -> False
loader.getCacheUsage()             -> [6, 10]
loader.getResource("a")            -> "not_cached"

Loading c needs 6 units while 7 of 10 are used. Reading a made b the least recently used entry, so b is evicted (usage drops to 4) and c then fits exactly. huge is rejected without evicting anything.

Constraints & Assumptions

  • 1 <= capacity <= 10000
  • Resource names are non-empty strings containing only lowercase letters, digits, and underscores.
  • Resource sizes are positive integers.
  • source is either "disk" or "cdn" .
  • At most 10,000 calls in total across all methods.

Clarifying Questions to Ask Guidance

  • When a second request for a resource arrives while the first load is still running, should it wait for that load, and what status should it get back?
  • Is the load slow real I/O, or simulated so that holding a lock while it runs is acceptable?
  • Which calls change recency order: only getResource , or also a load that hits the cache?
  • Can a registration ever be changed or removed after it is created?

What a Strong Answer Covers Guidance

  • The data structures for the registry and for the LRU cache, and the cost of each method with them.
  • How eviction decides when to stop, and how the usage total stays correct under both automatic and manual eviction.
  • The order of checks in load , and what a failed load does to the cache.
  • Which methods change recency order and which only read state.
  • How the design loads each resource once under concurrent requests, and what it costs to hold a lock during a slow load.

Follow-up Questions Guidance

  • Real loads take hundreds of milliseconds. How would you avoid holding a global lock during I/O while still loading each resource only once?
  • How should a load that fails or times out be handled, and what should callers waiting on it see?
  • How would you support pinning resources that are in use so eviction skips them?
  • Reloading from the CDN costs more than reloading from disk. How could eviction take reload cost and size into account?