advanced31 min studyLesson 692 in Engineering

Embedding serving moves indexed bytes across a placement and cache hierarchy

Account for row storage, request lookups, deduplication, cache misses, and sharding traffic before treating embedding serving as a dense compute problem.

On this page

What you’ll understand

  • Compute embedding-table and requested-row bytes under explicit representation assumptions.
  • Distinguish table-wise, row-wise, and column-wise placement tradeoffs.
  • Explain why a high aggregate hit rate can conceal costly request tails or hot shards.

Categorical features often become indices into embedding tables whose storage can dominate the dense layers of a recommendation model. A request may gather many rows and pool them into a smaller representation. Serving cost depends on which rows are read, where they live, whether they are cached, and how much traffic or synchronization their placement creates. Parameter count alone does not determine request latency. A useful byte calculation starts with explicit dimensions and representation, then adds the real system's omitted overheads.

Distinguish stored parameters from requested row traffic

For a plain table with N rows, dimension d, and b bytes per scalar, raw value storage is Ndb bytes. This omits row metadata, alignment, scales for quantization, replication, allocator overhead, and training optimizer state. A serving deployment need not carry the same extra state as training. A lookup of one full row moves db value bytes in an ideal accounting model, but hardware can fetch larger units or access compressed formats. Record whether the estimate counts logical values, memory reads, network payload, or observed physical traffic.

Count indices per request or batch before and after any valid deduplication. Repeated IDs may permit row reuse, while different feature tables with the same numeric ID remain distinct identities. An embedding bag's pooled output can be much smaller than all rows it reads. Cache hit rate must specify its denominator: lookup count, unique rows, bytes, or complete requests. A request touching many shards can still suffer a remote miss even with a high per-lookup hit rate, especially when hotness and row sizes vary.

Mvalues=Ndb,Bmiss=LmissdbM_{\rm values}=Ndb,\qquad B_{\rm miss}=L_{\rm miss}db
N is row count, d scalars per row, b bytes per scalar, and L_miss counts full-row misses for the stated request or batch. Both results are value bytes only. The formula assumes uniform uncompressed row width and excludes IDs, protocol, alignment, replication, metadata, and physical transaction amplification.↔ Scroll wide equations horizontally.

Placement changes routing and the longest required shard

Table-wise sharding places whole tables on selected workers and can minimize routing for a table that fits, but large or hot tables can dominate one placement. Row-wise sharding spreads IDs from a table across workers, increasing capacity flexibility while requiring routing and aggregation for multi-ID requests. Column-wise sharding splits each vector's dimensions, so one logical row may require several workers. Hybrid strategies trade memory balance, communication, and computation differently. An automated planner's recommendation still depends on its workload and topology assumptions rather than guaranteeing an optimal production tail.

Use observed frequency, bag lengths, row widths, topology, and the required aggregation path to evaluate a placement. Uniform row count does not imply balanced bytes or service demand. Replicating hot rows can reduce remote work but introduces memory and update-consistency costs. If a request waits for several shard responses, its critical path follows the last required response plus aggregation, not the mean shard latency. Version embedding values and cache keys consistently with the model and feature mapping so a fast stale row does not silently change the scoring function.

Tlookup=max⁡r∈STr+TaggregateT_{\rm lookup}=\max_{r\in\mathcal S}T_r+T_{\rm aggregate}
This conditional ideal assumes all required shards in S start together, execute concurrently, and have no other dependencies or contention beyond their measured T_r. Times share units. Delayed launches, serial routing, retries, and partial-result policies require a richer schedule; the formula is not a universal lookup latency prediction.↔ Scroll wide equations horizontally.

Test hotness, cold misses, and representation changes separately

Benchmark realistic ID and bag-length distributions, including cold starts, new items, and hot-key skew. Preserve model quality when compressing rows or changing pooling and distinguish storage savings from extra decode work. Measure complete-request latency alongside bytes and per-table hit rates. Rebuilds and refreshes can change cache temperature, so compare a stated warmup regime rather than one warm candidate with a cold baseline. Current TorchRec documentation describes useful sharding and collection primitives, but a serving cache, update policy, and failure behavior remain explicit system design choices.

The cache lab provides a tiny deterministic reuse example. Reset, set both C=4 and stride s=4, predict four misses and twelve hits in the initial sixteen requests, then inspect or Play. Reset and set both C=2 and s=4 for sixteen initial misses. It begins empty and accesses (i×s) mod 16 with fully associative LRU, adds two requests per playback second, and caps at4096. Each entry has no embedding width or version. The model demonstrates reuse and eviction, not a distributed embedding benchmark, production hit distribution, or byte estimate.

Logical row bytes expose the difference between capacity and miss traffic

A hypothetical serving table has ten million rows, 128 values per row, and two bytes per value. A batch makes 1280 full-row lookups with exactly 960 served from a valid cache. Assume no lookup deduplication and exclude all metadata, protocol, and physical-read overhead.

  1. Raw value capacity is 10000000×128×2=2560000000 bytes, or 2.56 decimal GB. Replicas and any representation metadata would increase that deployment total.

  2. Each row contains 128×2=256 value bytes. All logical lookups represent 1280×256=327680 bytes, or 320 KiB using 1024 bytes per KiB.

  3. Miss count is 1280−960=320, so uncached value payload is 320×256=81920 bytes=80 KiB. The 75% lookup hit rate does not directly specify complete-request hit probability, network traffic, or latency.

The result

This is a useful lower-level value-byte account. Measure the actual placement, protocol, hardware transactions, cache validity, and request tails before turning it into a serving prediction.

Explore the model

Interactive lab

Interactive lab · cache

Reuse and capacity have an exact meaning only within the stated stream

Reset, set both C=4 and s=4, predict four misses and twelve hits at initial N=16, then inspect or Play. Reset and re-enter both C=2 and s=4 for sixteen misses. The empty fully associative LRU model accesses (i×s) mod 16, with N=16+floor(2t) capped at4096. Entries have no byte width, embedding version, network placement, or concurrent shard behavior.

Reuse and capacity have an exact meaning only within the stated streamA fully associative LRU cache starts empty and requests address (i·s) mod 16. Hits move an address to the newest position; misses evict the oldest when full. Playback starts with 16 requests and adds two per second, capped at 4096 explicitly simulated requests. This toy stream is not a processor-cache benchmark. Current readings: Hits / requests: 0 / 16; Hit rate: 0%; Address-stream period: 16 addresses. All parameters are adjustable with the labeled controls below.Fully associative LRU cache after 16 deterministic requests: 0 hits and 16 misses.Most recent 16 requestsaddress = i × 3 mod 160M3M6M9M12M15M2M5M8M11M14M1M4M7M10M13MCurrent cache order · oldest → newest471013Latest address 13 · miss: inserted as newestToy address stream · period 16 · starts empty
Cached addressMiss / evictionMost recent request
Hits / requests
0 / 16
Hit rate
0%
Address-stream period
16 addresses
t = 0 s

A fully associative LRU cache starts empty and requests address (i·s) mod 16. Hits move an address to the newest position; misses evict the oldest when full. Playback starts with 16 requests and adds two per second, capped at 4096 explicitly simulated requests. This toy stream is not a processor-cache benchmark. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forTranslate misses to bytes only after defining row representation and the real lookup workload.

Check your understanding

A table's logical row count is evenly divided across workers. What still needs measurement before claiming balanced serving cost?

Try it yourself

A table has two million rows of dimension 64 with four-byte scalars. A request has forty distinct full-row lookups, ten of which miss its valid cache. Compute raw table bytes and missed value bytes under the uniform-row assumptions.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

First find bytes per row, then multiply separately by stored rows and missed rows.

Compare with the solution

Each row has 64×4=256 bytes. Raw table values use 2000000×256=512000000 bytes, or512 decimal MB. Ten misses represent 2560 value bytes=2.5 KiB. Metadata, IDs, alignment, network framing, replication, and physical-read amplification remain excluded.

Take it with you

  • Table capacity and lookup traffic require separate byte counts and representation assumptions.
  • Sharding balances capacity, hotness, routing, and aggregation through different tradeoffs.
  • Measure complete-request tails, cache validity, and real workload skew alongside aggregate hit rate.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.