Sizing it: numbers a senior gives

Part of the free LogicWiz Data Engineering course, Module 17: Pipeline design. The SQL and Python run in the app, on real Postgres and real Python in the browser.

It is Wednesday, 3 p.m., and the clickstream design from Lesson 17.3 is on the screen. One box says "Kafka: 134 GB of disk". Nora taps it. "With replicas? And how many partitions, how big a cluster, what does it cost a month?" "It depends" will not do here. She wants the arithmetic, one line at a time.

You'll learn to

  • Turn events a second into bytes, disk, files and partitions
  • Size Kafka partitions, Spark tasks, waves and memory per task
  • Estimate storage, compute and warehouse cost from a few prices
  • Split an end-to-end target into a latency budget with slack

Learn

How big? Five conversions

Exact numbers are not the point. The way you reach them is. Every sizing answer is the same chain of five conversions, each a formula you can say out loud:

  1. Rate: events a day / 86,400 seconds = events a second. Multiply by a peak factor (measured, often 3 to 10): you size for the peak, not the average.
  2. Bytes: events x bytes per event = bytes a day. Times retention and replicas = disk.
  3. Files: bytes / target file size = files.
  4. Compute: bytes / split size = tasks; tasks / cores = waves.
  5. Money: storage, compute and queries, each a quantity times a price.

Write the assumption next to every number. A wrong assumption can be fixed; a missing one looks like a guess.

Sizing the clickstream, one line at a time

The clickstream estimate built up in six steps: events a second, bytes a day, storage with replicas, Kafka partitions and cores, the monthly cost, and the two assumptions to measure.

  1. Events a second. Start from the business, not the servers: 2 million visitors a day, 60 events each, so 120 million events. Divide by 86,400 seconds, then size for the peak, measured at 5 times the average.

  2. Bytes a day. A measured event is 160 bytes of JSON. 19.2 GB a day sounds big, but at the peak it is only 1.1 MB/s: small for Kafka.

  3. Storage, with every copy. Kafka keeps each byte for 7 days on 3 replicas: 403 GB, three times the number on the slide. As Parquet (a measured 6 times smaller), bronze keeps 30 days and silver 13 months.

  4. Partitions and cores. One stream task handles about 1,000 events a second, so the peak needs 7 partitions; doubled for headroom, 14. The stream runs one task per partition: 14 cores, say 4 nodes of 4 cores.

  5. Cost a month. The lake is 1,360 GB at $0.023 a GB-month: $31. Compute is the real bill: 4 nodes running all 720 hours of a month, at an assumed $1.00 a node-hour, is $2,880.

  6. Say what you trust least. Two guesses carry everything else: the peak factor of 5 and the 1,000 events a second per task. The last line of the estimate says how you will measure them in the first week.

Read it top to bottom: each box uses only the numbers above it, so one changed assumption flows down to every line beneath it.

The storage estimate in code

events_per_day = 2_000_000 * 60
bytes_per_event = 160
peak_factor = 5
compression = 6                 # JSON to Parquet, measured on a sample

raw_gb = events_per_day * bytes_per_event / 1e9
peak_mb_s = events_per_day / 86_400 * peak_factor * bytes_per_event / 1e6
kafka_gb = raw_gb * 7 * 3       # 7 days, 3 replicas
bronze_gb = raw_gb / compression * 30
silver_gb = raw_gb / compression * 395
lake_usd = (bronze_gb + silver_gb) * 0.023

print(f'raw {raw_gb:.1f} GB/day, peak {peak_mb_s:.2f} MB/s')
print(f'kafka {kafka_gb:.0f} GB, bronze {bronze_gb:.0f} GB, silver {silver_gb:,.0f} GB')
print(f'lake storage ${lake_usd:.0f} a month')

Change one assumption at a time (1 KB events, 30 days of Kafka retention) and watch which number moves. That conversation is what a reviewer is after.

Partitions: the largest of three limits

Back to the 14 partitions. A partition is Kafka's unit of parallelism: one consumer in a group reads it, and its order is kept. So the count is the largest of three limits:

  • Producer side: peak MB/s / what one partition accepts on your brokers (measure it).
  • Consumer side: peak MB/s / what one consumer processes. Usually the binding limit, because consumers do real work: parse, look up, write.
  • Parallelism you want: at least as many partitions as consumers you plan to run.

Then add headroom. Adding partitions later changes hash(key) % partitions, so keys move to other partitions and per-key order breaks for anything in flight. A bigger stream shows it: 50 MB/s, 10 MB/s per partition, 5 MB/s per consumer, 8 workers planned.

Partitions each limit needs, for a 50 MB/s stream

Bars compare the partitions needed by the producer limit, the consumer limit and the planned workers, and the 20 chosen after doubling the largest for headroom.

The tallest of the first three bars sets the count, max(5, 10, 8) = 10, and the green bar doubles it for headroom: 20.

Files and folders have sizes too

Every task writes at least one file, so the file count is a design output, not an accident. The clickstream writes a micro-batch every minute with 14 tasks: up to 20,160 files a day. Compacting into 256 MB files turns the day's 3.2 GB into 13.

A table partition (a folder per day, say) should hold enough data to deserve its folder. Databricks advises not partitioning tables under about 1 TB, and keeping each partition at 1 GB or more. Below that, cluster the data instead, with liquid clustering or Z-order.

Apply it. Silver clicks are 3.2 GB a day and 1.26 TB in all: daily partitions are fine. The daily batch's facts are 25 MB a day, 27 GB in three years: do not partition, cluster on order_date.

Is one day big enough for a folder?

Bars compare one day of silver clicks at 3.2 GB and one day of the daily batch facts at 25 MB with the 1 GB a partition that Databricks advises.

Compare each bar with the grey 1 GB bar: only the clicks clear it, so only the clicks get a folder per day.

Cores and waves from data volume

Now a batch job. A nightly Spark job reads 2 TB of Parquet and shuffles 600 GB, on 50 executors of 4 cores: 200 cores.

  • Input tasks: 2,000,000 MB / 128 MB = 15,625 tasks.
  • Waves: each core runs one task at a time, so 15,625 / 200 = 78.1, which is 79 waves. The last one holds only 25 tasks. At 3 s a task the scan takes about 4 minutes.
  • Shuffle partitions: the default 200 would make each one 3 GB. At about 200 MB each you want 3,000, a multiple of the 200 cores. Or start high and let AQE coalesce towards 64 MB.

Waves: 10 tasks on 4 cores

Four cores run ten 30-second tasks in three waves; in the third wave only two cores have work and the other two sit idle.

Read it left to right: three waves of 30 s, and in the last one two cores sit idle, so 10 tasks cost as much as 12. A task count that is a multiple of the cores (or many times larger) keeps the last wave full.

Last, memory per task. Each executor has a 16 GB heap, and its unified memory is 0.6 x (16,384 - 300) = 9,650 MB. With nothing cached and 4 tasks running, each task may use between 1/(2N) and 1/N of it: 1,206 to 2,413 MB.

So a 200 MB shuffle partition fits easily. A 3 GB one, from the default 200 partitions, spills to disk. That is the real reason for the 3,000.

Size the nightly job

import math

input_gb, shuffle_gb = 2_000, 600
executors, cores_per_exec, heap_mb = 50, 4, 16_384
split_mb, target_shuffle_mb = 128, 200

cores = executors * cores_per_exec
tasks = math.ceil(input_gb * 1000 / split_mb)
waves = math.ceil(tasks / cores)
shuffle_parts = math.ceil(shuffle_gb * 1000 / target_shuffle_mb / cores) * cores
unified = 0.6 * (heap_mb - 300)

print(f'{tasks:,} input tasks in {waves} waves on {cores} cores')
print(f'{shuffle_parts:,} shuffle partitions of {shuffle_gb * 1000 / shuffle_parts:.0f} MB')
print(f'per task: {unified / (2 * cores_per_exec):,.0f} to {unified / cores_per_exec:,.0f} MB of execution memory')

Estimates use decimal units (1 GB = 1,000 MB). Spark's 128 MB is really 128 MiB; the 5% difference never changes a decision.

Compute is the bill

Back to the clickstream. Storage came to $31 a month. Compute grows with hours running, not with data size. Use your own price per node-hour; the arithmetic does not change. Take the same 4-node cluster at an assumed $1.00 per node-hour, everything included:

  • Always-on stream: 4 x 24 h x 30 days = 2,880 node-hours: $2,880 a month.
  • Hourly micro-batch (availableNow, 12 minutes a run with start-up): 4 x 4.8 h x 30 = $576.
  • Nightly batch of 1 hour: $120.

Going from a nightly batch to an always-on stream multiplies the cost by 24 here. So the first question is "how fresh, and what decision does it drive?". A 15-minute answer that saves $2,000 a month is a senior answer.

A month of compute for the same pipeline

Bars compare the monthly compute cost of one pipeline run as an always-on stream, an hourly micro-batch and a nightly batch on the same four-node cluster.

Same cluster, same data, three schedules: the bars differ only in the hours the cluster runs.

Warehouses bill bytes or seconds

Warehouses bill differently, and a dashboard is the classic trap.

  • BigQuery on-demand bills bytes scanned: $6.25 per TiB, the first TiB a month free, at least 10 MB for each table a query touches. A tile that scans 50 GB every 5 minutes scans 288 x 50 GB = 14.4 TB a day, 13.1 TiB: about $82 a day, $2,450 a month. Pointed at a 50 MB gold table, it fits in the free tier.
  • Snowflake bills credits for each hour a warehouse runs (X-Small 1, each size up doubles), per second, with a 60-second minimum every time it resumes. 1,000 five-second queries that each wake a suspended warehouse bill 1,000 minutes, not 83.

The fix is the same in both: pre-aggregate in gold, and batch the small queries.

One tile, a month of BigQuery

Bars compare a month of one dashboard tile on BigQuery on-demand: about 2,449 dollars on raw clicks against nothing on a small gold table inside the free tier.

Same tile, same refresh rate: the only change is how many bytes each refresh scans.

Ten minutes is a sum

Nora has one last question: "And the 10 minutes you promised for the database feed?" An end-to-end target is a sum. Give each stage its worst case, not its average, and keep 10 to 20% slack. Lesson 17.4 promised 600 s from commit to silver:

  • capture and Kafka: about 2 s;
  • the bronze trigger, every minute: up to 60 s, plus 20 s to run;
  • silver's MERGE every 5 minutes: up to 300 s of waiting, plus 90 s to run;
  • the gold refresh: 30 s.

That is 502 s, with 98 s (16%) of slack. The 5-minute schedule is 60% of it: that is the lever. Every 2 minutes would cut 180 s, at the price of more, smaller MERGEs and more files to compact.

Commit to silver: the 600-second budget

A bar of 600 seconds split into capture, the bronze trigger and run, the wait for the five-minute silver schedule, the silver merge, the gold refresh and 98 seconds of slack.

Read it left to right as the life of one change: the two amber blocks are time spent waiting for a schedule, 360 of the 502 seconds.

The four classic sizing mistakes. Sizing for the average instead of the peak. Forgetting replicas and retention (Kafka stores every byte 3 times for 7 days). Mixing bits and bytes (a 10 Gbit/s link moves 1.25 GB/s at best). And quoting a number without its formula: "about 400 GB" can be checked only as "19.2 GB x 7 x 3".

Remember: say every number with its formula and its assumption, in one breath: "120 million events a day is about 1,400 a second, 7,000 at peak and 1 MB/s: small for Kafka." Round boldly, then say what you would measure first.

Numbers to remember

QuantityValueUse it for
Seconds in a day86,400 (round to 100,000)per day to per second
1 million events a dayabout 12 a seconda quick rate check
1 MB/s sustained86.4 GB a daystream to storage
A 10 Gbit/s link1.25 GB/s at bestbits are not bytes
spark.sql.files.maxPartitionBytes128 MBinput split per task
spark.sql.shuffle.partitions200shuffle tasks before AQE
AQE advisoryPartitionSizeInBytes64 MBtarget size when coalescing
Kafka log.retention.hours168 (7 days)broker disk
Databricks partitioning advicenone under about 1 TB; 1 GB or more per partitionpartition or cluster
S3 Standard, first 50 TB, US East$0.023 per GB-monthlake storage
BigQuery on-demand$6.25 per TiB scanned, first TiB a month freequery cost
Snowflake warehouseX-Small 1 credit an hour, doubling per size; per second, 60 s minimumwarehouse cost

Prices are list prices for one region at the time of writing: say "about" and check your own. Measure the rest (event size, compression, throughput per core) on a sample before you trust it.

What changed at SwiftKart

The slide changes. "Kafka: 134 GB" becomes "19.2 GB x 7 days x 3 replicas = 403 GB", and every other box gets its line of arithmetic: 14 partitions, $31 of storage, $2,880 of compute for the always-on stream, 502 of 600 seconds for the database feed. Nora reads the two lines you trust least and asks you to measure both in the first week.

The estimate goes into a capacity sheet beside the design: one row per quantity, its formula, its assumption, and later the measured value. Each quarter it is checked against Kafka's bytes in, Spark's task times and the cloud bill.

That was the last design review. Module 18 is the interview itself, one round at a time, starting with the SQL round.

In one line

Size a pipeline with five conversions said out loud (rate, bytes, files, compute, money), write the assumption next to every number, and split the latency target into a budget with slack.

Interview corner

Estimate the infrastructure for 100,000 events a second.

"At 1 KB an event that is 100 MB/s, 8.64 TB a day. Kafka with 7 days and 3 replicas needs about 181 TB of broker disk, so I would also compress at the producer and think about shorter retention plus a bronze copy in object storage. If one consumer handles 5 MB/s, the consumer limit is 20 partitions, 40 with headroom. In the lake, at a 6x compression, it is about 1.4 TB a day, so daily partitions are well over 1 GB and clustering by event type helps. Then I would measure the event size and per-consumer throughput on real traffic before buying anything."

How many shuffle partitions would you set for a 600 GB shuffle?

"I aim for roughly 100 to 200 MB per partition, so about 3,000, rounded to a multiple of the cores so the last wave is full; the default 200 would give 3 GB partitions that spill. With AQE on, I would rather set a high initial number and let it coalesce towards the advisory size, 64 MB by default. I would check that parallelismFirst is set the way I expect, because by default it favours parallelism over the advisory size."

The business wants a streaming pipeline instead of the nightly batch. How do you argue the cost?

"Compute cost follows hours running. On the same four nodes at about $1 a node-hour, an always-on stream is about $2,880 a month, an hourly micro-batch about $576 and the nightly batch $120. So I ask what decision needs fresher data, and what being an hour late costs. If the answer is a dashboard read twice a day, the hourly batch meets it for a fifth of the price; if it is fraud or stock-outs, the stream pays for itself."

How do you set a latency budget for a pipeline?

"I list every stage from the event to the reader, give each its worst case from real measurements, and add them up against the target with 10 to 20% slack. In our CDC design that is 502 of 600 seconds, and 300 of them are the wait for the 5-minute silver schedule, so that is the lever. The budget also tells me what to alert on: any stage that eats the slack."