skip to content

A job folds one small value per group, yet runs out of room — which two quantities multiply to set that memory, and what inflates each?

level: middleimportance: should knowfreq 46%

answer

  1. memory is a product, not a length
  2. per-group size times open groups
  3. overlap factor multiplies the group count
  4. key cardinality is usually the dominant term

basics

~20 s

Memory is what one open group holds multiplied by how many groups are open at once. A tiny accumulator still costs plenty when the grouping key has millions of values, or when intervals restarted before they end put each record into several groups.

solid answer

~50 s

The product has two factors and candidates usually quote only the first. **What one open group holds** is a constant when the aggregate folds and grows with records when it does not. **How many groups are open at once** is set by the number of distinct grouping keys active in the period, multiplied by an overlap factor when an interval of length L is restarted every step S — each record then belongs to `L / S` groups simultaneously — and increased again by how long a group stays resident after its span ends: it waits for the running assertion that no older record will still arrive, and then possibly for a stated extra span during which it still accepts records and re-emits. Two million keys, an overlap factor of fifteen and a 48-byte accumulator is thirty million open groups and roughly 1.4 gigabytes, with no record retained anywhere.

code

python · 11 lines
python
keys_active = 2_000_000
interval_seconds = 900
step_seconds = 60
bytes_per_group = 48

overlap_factor = interval_seconds // step_seconds
open_groups = keys_active * overlap_factor
retained_bytes = open_groups * bytes_per_group

print(overlap_factor, open_groups, retained_bytes)
# 15 30000000 1440000000

go deeper

for a junior

Remember that the interval's length alone does not set the cost. Memory is what each open group holds multiplied by how many groups are open at the same time.

for a middle

Do the arithmetic out loud, including the overlap factor when an interval is restarted before it ends, and the number of distinct grouping keys active in the period.

for a senior

Use the product as a sizing argument before the job is written, and know which lever moves which factor: key coarseness, interval length, restart step, and how long a finished group stays resident.

for a principal

Decide what the platform will promise. A grouping whose open-group count is a function of user-supplied key cardinality is an unbounded promise unless something is defined to drop those groups.

## The memory of a grouping is a product The number that decides whether a grouping job runs is not the interval's length and not the arrival rate on its own. It is: > retained memory is approximately **what one open group holds** multiplied by **how many groups are open at the same time** Candidates reliably reason about the first factor and forget the second, which is how a job with a four-byte counter per group runs out of room. Both factors have to be estimated before the job is written, because only one of them is easy to change afterwards. ## Factor one: what one open group holds - A **foldable accumulator** — a per-group value that any two of which merge into one and that does not grow with records folded in — is a constant: one number, or a small tuple such as a total and a count. - A **retained record set** grows with the records the group has seen, so this factor becomes arrival-rate-dependent and the product becomes a triple. - The grouping key itself is usually stored per open group alongside the value, which matters when the key is a long string rather than an integer. - How a carried value is laid out in bytes is a separate subject from this one; estimate it as a plausible per-group figure and be explicit that it is an estimate. ## Factor two: how many groups are open at once - **Distinct grouping keys active in the period.** This is usually the dominant term and it is usually supplied by the data, not by the author: user identifiers, device identifiers, session identifiers. - **The overlap factor.** An interval of length L restarted every step S shorter than L means any instant is covered by L divided by S intervals, so one key has that many groups open simultaneously and each record is folded into all of them. - **Groupings with no fixed length.** A grouping that stays open for one key while records keep arriving, and closes after a stated quiet period with none, keeps one group open per key that has been active recently — and its length is a property of the data. - **Residency after the span ends.** A group is not droppable the moment its span is over. It waits for the job's running assertion that no record older than a stated moment will still arrive, and it may then wait through a further stated span during which it still accepts records and re-emits a corrected value. Both extend how many groups coexist. - **Anything that never expires.** A grouping over a key space that keeps producing new keys accumulates groups for as long as nothing drops them. ## Worked arithmetic 1. Distinct keys active in an interval: two million. 2. Interval length fifteen minutes, restarted every minute, so the overlap factor is fifteen. 3. Open groups: two million times fifteen, which is thirty million. 4. Carried value estimated at 48 bytes: thirty million times 48 bytes is about 1.4 gigabytes — with not one record retained. The instructive part is which step you can move. Dropping the overlap factor from fifteen to three, by restarting the interval every five minutes instead of every minute, divides the answer by five and changes the product's output resolution, which is a requirements conversation. Coarsening the grouping key divides it too, and changes what the result means. Shrinking the carried value by a few bytes changes almost nothing, which is why tuning it is the wrong instinct. ## What the number is for, and where this subject stops This arithmetic is a design-time argument: it tells you whether the grouping as specified is viable, and which of the two factors to negotiate if it is not. What the runtime does when the total exceeds what a worker can hold — writing part of it to local disk, failing, or degrading — and where those bytes physically sit are separate subjects with their own owners. So is how one worker's memory is divided between competing uses. Compute the product, compare it against what the job has to work with in total, and hand the overflow question to the people who own it. ## Where engines of this class differ - **How the open groups are maintained.** Where each record advances individually through long-lived operators, the open groups live continuously for as long as they are open. Where arrivals are instead collected for a short span and one finite job runs over the collected set, the carried values are handed from one such job to the next, so the same product applies but residency is counted in whole spans. In a finite pass that materialises between phases there are no groups open between runs at all, and the product applies only within one pass. - **Whether the open-group count is under the author's control.** Sometimes the width of the job and the key space can be changed while it runs; in other designs the width is fixed for the life of the job, so the only lever left is the grouping key or the boundary. - **What bounds the count.** Some runtimes will drop a group's carried value once a stated span has passed with no write to it; others leave that entirely to the author. Never assume a ceiling exists because one runtime you used provided one.

  • Which factor does shortening the interval actually reduce?
    Mostly residency. A shorter span means each group is droppable sooner, which lowers how many coexist when a key is only occasionally active. Where every key is active in every interval, the count is already one group per key and shortening changes how long each lives rather than how many exist — unless the interval overlaps, in which case a shorter length with the same step also cuts the overlap factor.
  • Two jobs hold the same number of open groups but one uses far more memory. What differs?
    The other factor: what each open group holds. One may fold to a constant such as a total and a count, while the other keeps every value for an exact median or every identifier for an exact distinct count. Same open-group count, per-group size differing by orders of magnitude.

saying these in an interview costs you the question

  • Estimates memory from the interval's length alone.
  • Ignores that overlapping intervals put one record in several open groups.
  • Assumes a folded value means memory can never become a problem.
  • Forgets that grouping key cardinality sets how many groups exist.
  • Believes memory falls the moment a group emits its value.