10.3 Large-Object Transfer and Chunking

IPCSTATUS: DEVELOPINGLEVEL: INTERMEDIATEREVIEWED: 2026-11-18
Before this page Serialization, Memory Mapping

10.3.1 Peak memory during transfer

Sending one 16 GB object is not the same as sending sixteen 1 GB chunks: peak memory, backpressure and fault recovery all differ.

10.3.2 The problem: peak memory during transfer

Consider a process that generates a large data frame and sends it to a worker.

# Main process
data <- data.frame(x = rnorm(1e9), y = rnorm(1e9))
# data is 16 GB in memory: two columns of 1e9 doubles

# Send it to a worker
send_to_worker(worker_id = 1, data = data)

What happens:

  1. The main process serializes data. The byte stream is 16 GB.
  2. The bytes wait somewhere on the way. They wait whole if the transport builds a complete message first (serialize(x, NULL), a message queue, a file), or a few kilobytes at a time if the sender writes to a socket the worker is reading.
  3. The worker deserializes. The worker allocates 16 GB for the new object.

Peak memory on the main process: at least 16 GB (the original) plus buffer overhead.

Peak memory on the worker: at least 16 GB (the new object) plus buffer overhead.

Total peak across both processes: 32 GB plus buffers.

Now chunk it into 16 pieces of 1 GB each:

  1. The main process serializes and sends chunk 1. 1 GB transferred.
  2. The worker receives and deserializes chunk 1. 1 GB allocated.
  3. The main process serializes and sends chunk 2 while the worker processes chunk 1.
  4. The worker receives chunk 2, processes it, and drops it.

Peak memory on the main process: 1 GB (one chunk) plus the original 16 GB (still in memory, cannot free until done).

Peak memory on the worker: 1 GB (one chunk) plus the next chunk in flight and overhead, as long as it processes each chunk and lets it go.

Peak on the main process is about the same (16 GB plus one chunk). Peak on the worker falls to 1 or 2 GB instead of 16 GB, but only for a worker that consumes chunks as they arrive. A worker that reassembles the whole data frame still needs 16 GB for it, and chunking then saves only the transient buffers. The two sides also overlap: the main process serializes chunk 2 while the worker deserializes chunk 1.

10.3.3 Mental model

Chunking trades a fixed cost per chunk for lower memory. The per-byte work of serializing, moving and deserializing every byte stays the same however the object is split. The fixed cost (a header, an acknowledgement round trip, a function call on each side) is paid once per chunk, and overlapping the two sides can win some of it back.

In return, chunking allows:

  1. Interleaved operations: Serialize chunk N while deserializing chunk N-1.
  2. Lower peak memory on the receiver: If the receiver consumes each chunk and lets it go, only one or two chunks are in memory at a time.
  3. Backpressure: The receiver can throttle the sender. If the receiver is slow, it does not pull the next chunk. The sender waits. Memory does not accumulate on the sender’s side.
  4. Fault recovery: If transfer fails midway, resend the failed chunk, not the entire object.

10.3.4 Chunk sizing

Too small (1 KB chunks):

  • Overhead per chunk: headers, sync, handshakes
  • For 16 GB = 16 million chunks, overhead dominates
  • Latency suffers

Too large (4 GB chunks):

  • Fewer chunks: 4 for 16 GB
  • Lower chunk overhead
  • But if a chunk fails, resending 4 GB is expensive
  • Peak memory on receiver: 4 GB per chunk

Goldilocks (100 MB chunks):

  • 160 chunks for 16 GB
  • 160 fixed per-chunk costs, small next to moving 16 GB
  • Peak memory on receiver: 100 MB per chunk (reasonable)
  • Resending one chunk costs little

Chunk size depends on:

  1. Network bandwidth: At 1 Gbps, transferring one chunk takes 0.8 seconds. The receiver needs 0.8 seconds of processing capacity per chunk. Chunk size and processing rate interact.
  2. Available memory: If the worker has only 2 GB total, chunks must be 1 GB or smaller.
  3. Fault tolerance: Large chunks mean large retry costs. Small chunks mean small retry costs.
  4. Serialization overhead: Large chunks amortize serialization header overhead.

For R workers, chunks in the tens to hundreds of megabytes would keep both the per-chunk overhead and the receiver’s memory small. The fixed cost per chunk fades as chunks grow, and a receiver holding one or two chunks at a time stays far below the memory of a typical R process. The right size for a given workload would come from profiling it.

10.3.5 Backpressure

Backpressure means the receiver can slow the sender.

Without backpressure:

sender():
  for each chunk:
    serialize(chunk)
    write(socket, chunk)  // Blocking or buffering

receiver():
  for each chunk:
    read(socket, chunk)
    deserialize(chunk)

The sender serializes and writes as fast as it can. If the receiver is slow, the socket buffers fill and the sender’s write() blocks. TCP flow control is therefore already backpressure, bounded by the buffer sizes (Linux grows a TCP send buffer up to about 4 MB by default). What it does not bound is memory above the socket: a sender that queues whole serialized messages in its own memory before writing them can run as far ahead as that queue allows.

With explicit backpressure (via flow control):

sender():
  for each chunk:
    serialize(chunk)
    write(socket, chunk)
    wait_for_ack(receiver)  // Block until receiver confirms

receiver():
  for each chunk:
    read(socket, chunk)
    deserialize(chunk)
    send_ack(sender)  // Tell the sender to go on

The receiver confirms each chunk. The sender waits for the ack before serializing the next chunk, so a slow receiver slows the sender down with it, and memory does not accumulate.

10.3.6 Streaming deserialization

Naive deserialization materializes the entire object:

bytes = read_all(socket)  // Read 16 GB
object = deserialize(bytes)  // Allocate 16 GB

Streaming deserialization reads and builds piece by piece:

while not end_of_stream:
  chunk_bytes = read_chunk(socket, 100MB)  // Read one chunk
  partial_object = deserialize_chunk(chunk_bytes)  // Build incrementally
  add_to_result(partial_object)

For a data frame with rows, streaming can deserialize row-by-row or column-by-column. For nested structures, streaming is harder but possible.

R’s unserialize() reads a connection as the bytes arrive, but it returns nothing until the whole object is rebuilt, so the receiver holds all of it; there is no way to take part of a serialized object. Apache Arrow’s IPC stream format is a sequence of record batches, and a reader can take one batch of rows at a time.

10.3.7 Example: sending a large matrix via chunks

// Pseudocode: sender
matrix = 10000x10000 numeric matrix (800 MB)
chunk_size = 100MB  // 8 chunks

for i in 1 to 8:
  chunk = extract_rows(matrix, (i-1)*1250, i*1250)
  send_with_ack(worker, chunk)

// Receiver
results = []
for i in 1 to 8:
  chunk = receive(sender)
  partial_matrix = deserialize(chunk)
  add_rows(results, partial_matrix)
  send_ack(sender)  // Backpressure

complete_matrix = combine(results)

The sender extracts 1,250 rows at a time (100 MB each) and waits for an ack after each chunk. The receiver’s peak is not one chunk here, though. results ends up holding all eight chunks, and combine() then builds a second 800 MB copy, so the receiver peaks near 1.6 GB. Writing each chunk straight into a preallocated 800 MB matrix keeps the peak near 800 MB plus one chunk.

10.3.8 When chunking is essential

Large objects: Objects that take a large share of the receiver’s free memory. A single transfer can exhaust it or cause garbage collection pressure.

Slow networks: High latency between sender and receiver. Chunking allows interleaved work. The sender serializes while the receiver deserializes.

Memory-constrained environments: Embedded systems or containers with limited RAM. Chunking keeps peak memory low.

Fault recovery: If the network connection drops, resending one chunk is faster than resending the entire object.

10.3.9 When chunking is not necessary

Small objects: Objects far smaller than the receiver’s free memory. The extra code and the per-chunk cost buy nothing.

Fast, local networks: Transport is quick next to serialization, so chunking gains little speed. It can still be needed for memory.

Immutable data structures: Apache Arrow columnar format. The data is already chunked (batch layout). Streaming deserialization is built in.

10.3.10 Comparison

Strategy Receiver peak memory Latency Backpressure Best for
Full transfer Object size plus buffers serialize + transfer + deserialize Implicit (socket buffer) Small objects, fast network
Chunked streaming 1 to 2 chunks, if consumed as they arrive Same per-byte work plus a cost per chunk Explicit (per-chunk acks) Large objects, slow network
Columnar (Arrow) 1 batch, if consumed as read deserialize as read Built-in Bulk numeric data

10.3.11 Serialization format impact

Not all serialization formats support chunking equally.

R’s format (serialize()): Binary, self-describing, requires full materialization. Not chunk-friendly.

Apache Arrow: Columnar layout, chunked batches, streaming deserialization. Chunk-friendly.

Protocol Buffers: Not self-delimiting. A stream of messages needs a length prefix before each one, which the Protocol Buffers documentation describes, and the same page advises against single messages much over a megabyte. Chunk-friendly if each chunk is its own message.

MessagePack: Compact binary. Each value encodes its own length, so values written one after another can be read back one at a time, but splitting one large value into chunks is up to the program.

sequenceDiagram
  participant S as sender
  participant W as worker
  S->>W: chunk 1 of 16 (1 GB)
  W->>W: deserialize, keep the rows
  W-->>S: ack
  S->>W: chunk 2 of 16 (1 GB)
  W->>W: deserialize, keep the rows
  W-->>S: ack
  Note over S,W: repeat until chunk 16

Chunked transfer with explicit backpressure: the sender waits for each ack before it sends the next chunk, so only one chunk of bytes is in flight at a time.

Further reading

Where this appears

  • sunlight Data that hatch(setup = ) creates in the template reaches every forked worker without a transfer. Everything else a task needs is serialized into the chunk that carries it.
  • r-in-rust (rrs) rrs could use streaming deserialization for large objects, building them chunk by chunk instead of materializing each one up front.

Where to go next

Back to top