The internals round

Part of the free LogicWiz Data Engineering course, Module 18: Interview rounds. The SQL and Python run in the app, on real Postgres and real Python in the browser.

It is the internals round. The interviewer turns the laptop towards you: "Your executors have 16 GB and 4 cores. Write me a function that says how much memory one task gets before it spills, and what the container really asks for." Then: "Now Delta. Two writers commit at once. Model the conflict check." This is the round where you show that the numbers you quote are numbers you can work out.

You'll learn to

  • Answer with the mechanism, the numbers, the failure and the lever
  • Work out executor memory, join choice and shuffle files from real defaults
  • Model skipping, commit conflicts, acknowledgements and visibility in a few lines

Learn

How the round runs

One system, ten minutes, follow-up after follow-up. "How much can one task use before it spills? And the container?" "Two writers commit to the same Delta table: who wins, and what does the loser see?" Sometimes it is a whiteboard. More and more often it is a laptop and "model it in Python".

The answer has the same four parts every time:

  1. Mechanism: the steps and the data structure.
  2. Numbers: the defaults, and one formula worked through.
  3. Failure: the exact error or symptom when it goes wrong.
  4. Lever: the config or design change, and what it costs.

The ten problems below are the models interviewers ask for. Each prompt states its rules, as an interviewer would.

Build one answer: one task's memory

Take the question from the scene: executors with 16 GB and 4 cores. How much memory does one task get, and what does the container ask for?

Do not quote a number from memory. Build it, one line at a time, and draw the bar as you go. Each step below cuts the bar once more.

A 16 GB, 4-core executor, cut up step by step

A bar for one executor is cut step by step: a 16,384 MB heap, 300 MB reserved, 6,434 MB user memory and 9,650 MB unified memory, the unified part split into 4,825 MB storage and 4,825 MB execution, and finally 1,638 MB overhead outside the heap, making an 18,022 MB container.

  1. The heap. spark.executor.memory=16g gives the JVM a heap of 16 x 1,024 = 16,384 MB.

  2. Spark keeps 300 MB. A fixed reserve for Spark itself. Usable = 16,384 - 300 = 16,084 MB.

  3. Split by spark.memory.fraction, 0.6. Unified memory = 0.6 x 16,084 = 9,650 MB, shared by execution and cache. The other 0.4, 6,434 MB, is for your own objects.

  4. Split unified in two. storageFraction 0.5 protects 4,825 MB of cache from eviction. With nothing cached, execution may use all 9,650 MB. Four tasks run: each gets 9,650 / 8 = 1,206 MB for certain, and up to 9,650 / 4 = 2,413 MB.

  5. Add what lives outside the heap. Overhead = max(384, 0.10 x 16,384) = 1,638 MB. So the container asks for 16,384 + 1,638 = 18,022 MB. Go past that and the cluster manager kills the container: that is not a Java OutOfMemoryError.

Read each bar left to right. The container figure in the last step is what Kubernetes or YARN must grant. This is the "numbers" part of the answer, worked out loud.

The rules behind the ten problems

AreaRuleDefaults
Executor memoryusable = heap - 300 MB; unified = fraction x usable; protected storage = storageFraction x unified; overhead = max(384 MB, 0.10 x heap)spark.memory.fraction 0.6, storageFraction 0.5
Per-task sharebetween unified / (2N) and unified / N with N tasks running and nothing cachedN = cores per executor
Join strategybroadcast a side the join type allows when it is under the threshold; else sort-merge; full outer never broadcast-hashesautoBroadcastJoinThreshold 10 MB
Shuffle filessort-based: one data and one index file per map task; reducers fetch one block from every map taskbypass writer at 200 reducers or fewer
AQE coalescemerge neighbouring partitions while the group stays within the targetadvisory 64 MB, minimum 1 MB
AQE skewskewed if over factor x median and over the threshold; split into pieces near the targetfactor 5, threshold 256 MB
Parquet skippingskip a row group whose min/max cannot satisfy the filterrow group 128 MB in parquet-java
Delta conflictsmetadata first, then files added where you read, then files you read that were removed, then files both removedWriteSerializable on Databricks
Kafka acksan acknowledged write is on every ISR member; with acks=all writes are refused when the ISR is below min.insync.replicasacks=all, min.insync.replicas 1
MVCC visibilitya version is visible if its creator committed before the snapshot and its deleter did notsnapshot = xmin, xmax, in-progress list
Delta log replaythe newest checkpoint at or before the version, then every JSON commit after ita checkpoint every 10 commits (open source)

One row per problem below: the rule, then its real default. Say the rule, then the default, then the consequence. Interviewers often change one number ("and with 8 cores?") to see whether you know the formula or only a remembered answer.

`acks=all` with `min.insync.replicas=2`, replication factor 3

A sequence between a producer, the leader and two followers: a write is acknowledged once all three in-sync replicas have it; after one follower drops out of the ISR writes are still acknowledged by two copies; after the second drops out the leader refuses writes with NotEnoughReplicasException.

Read it top to bottom. Each dashed line across is a broker dropping out. Every acknowledged write sits on at least two brokers, so one broker can be lost for good without losing acknowledged data, and with one broker down the topic still takes writes. With the default min.insync.replicas=1, the last write would succeed on the leader alone.

What a reader opens to rebuild Delta version 37

A folder tree of _delta_log: the _last_checkpoint file points at version 30, the reader opens the version 30 checkpoint Parquet file and the JSON commits 31 to 37, and skips commits 0 to 29.

Teal files are opened, grey ones are skipped. Open-source Delta writes a checkpoint every 10 commits by default (delta.checkpointInterval), so a reader never replays more than 9 JSON files; Databricks tunes the interval itself. Without checkpoints, version 50,000 would mean 50,001 files.

Draw first, then number: a bar for the executor, a line per broker, a file per commit. Work one number out loud ("9,650 MB unified, 2,413 MB at most for one of four tasks"), then name the config you would change and its cost ("more partitions, so each task needs less; or spark.memory.fraction up, at the expense of user memory"). Follow-ups stop when the numbers are right.

The trap: numbers people get wrong in this round. The heap is not the container: 16 GB of spark.executor.memory asks for about 18 GB. A task is guaranteed only 1/(2N) of the execution pool, not 1/N. The 10 MB broadcast threshold is compared with an estimate, which can be far off after a filter, which is why AQE's runtime switch matters. acks=all waits for the ISR, not for every replica. And under WriteSerializable, a blind append does not conflict with your MERGE; under Serializable it does.

Remember: draw it, then work one number out loud. Mechanism, numbers, failure, lever.

Next is the debugging round: no rules to recite, only evidence to read.

In one line

Answer an internals question as mechanism, numbers, failure and lever, and be ready to write the rule as a few lines of code.

Interview corner

Walk me through how much memory one Spark task can use on a 16 GB, 4-core executor.

Spark reserves 300 MB, and 60% of the remaining 16,084 MB, about 9,650 MB, is the unified region shared by execution and caching. With nothing cached, execution can use all of it, and each of the 4 running tasks is guaranteed 1/(2N) = 1,206 MB and may grow to 1/N = 2,413 MB; past that, its sort or hash buffers spill to disk. If a quarter of the region is held by cached blocks inside the protected storage half, the pool shrinks and so does each share. The container itself is the heap plus overhead, max(384 MB, 10%), so about 18 GB.

How does Spark choose a join strategy, and when does it choose wrong?

For an equi-join the planner broadcasts a side whose estimated size is under autoBroadcastJoinThreshold, 10 MB by default, if the join type allows that side to be the hash table: the right side for inner, left, semi and anti joins, the left for inner and right joins, neither for full outer. Otherwise it uses a sort-merge join. It goes wrong when the estimate is wrong, typically after a selective filter or with missing statistics: it shuffles a table that turned out tiny, or broadcasts one that is huge and runs the driver or executors out of memory. AQE fixes the first case at runtime; hints and fresh statistics fix the second.

Two jobs write the same Delta table at the same time. What happens?

Both write their Parquet files, then each tries to create the next JSON file in _delta_log; object storage guarantees only one put-if-absent wins. The loser reads the winner's commit and checks it in order: a metadata change fails it at once; files added in partitions it read give ConcurrentAppendException, unless the winner was a blind append and the isolation is WriteSerializable; files it read that the winner removed give ConcurrentDeleteReadException. If nothing conflicts it simply commits as the following version. To avoid conflicts, give concurrent MERGEs disjoint partition predicates in their conditions.

What exactly does acks=all with min.insync.replicas=2 and replication factor 3 guarantee?

An acknowledged write is on every in-sync replica at that moment, and the leader refuses writes when the ISR is smaller than 2, so every acknowledged write is on at least 2 brokers. That survives the permanent loss of any one broker without losing acknowledged data, and with one broker down the topic still accepts writes. With two brokers down, producers get NotEnoughReplicasException: availability is traded for durability. With the default min.insync.replicas=1, the ISR can shrink to the leader and acks=all quietly means one copy.