Skip to content

Buffer

A buffer is a temporary memory region that smooths the flow of data between a producer and a consumer running at different speeds, absorbing bursts without blocking either side.

Definition

What it is

A buffer absorbs differences in throughput. A network card delivers packets at 10 Gbit/s; the application reads them at 500 Mbit/s. Without a buffer, most packets would be dropped. With a buffer, they queue in kernel memory, and the application drains them at its own pace.

Buffers exist in every layer: CPU caches, disk controllers, network sockets (SO_RCVBUF, SO_SNDBUF), keyboard input, audio output, video streaming, HTTP response bodies. A buffer is different from a cache: a cache holds data for reuse; a buffer holds data for transfer.

Why it matters

Buffers convert backpressure into latency. If a producer is faster than a consumer, either the producer waits (blocking) or the excess is stored (buffering). Buffers prefer latency: the data waits, not the producer. That trade-off is what makes networks and IO work at heterogeneous speeds.

Buffer vs cache vs queue

Aspect

Buffer

Cache

Queue

Purpose

Smooth flow

Store for reuse

Decouple producer/consumer

Who reads

One consumer

Many readers

Workers

Eviction

Drained

LRU/TTL

Ack-based

Lifetime

Milliseconds

Minutes/hours

Until processed

Example

Socket buffer

HTTP cache

SQS

Producer and consumer

graph LR
  P[Producer fast] -->|write| B[Buffer]
  B -->|read| C[Consumer slow]
  B -.->|full| Block[Block producer]
  B -.->|empty| Wait[Consumer waits]
  style B fill:#E8F5E9

Buffer strategies

Buffers that behave

  1. Size for the worst burst

    A 1 KB buffer for a 10 Gbit/s network drops packets. Size to the peak, not the average.

  2. Ring buffers for bounded memory

    A fixed-size circular buffer never grows: when full, oldest data is overwritten or the producer blocks.

  3. Monitor fill level

    A buffer at 90% is a buffer that will overflow soon. Alert before it does.

  4. Flush on shutdown

    A buffer that loses its data on shutdown is a data-loss bug waiting for a disk fill.

  5. Bounded is safer than unbounded

    An unbounded buffer is a memory leak that grows on backpressure.

Common failures

Origin

The word comes from Old French buffe, "a blow", and entered English in the 19th century as a mechanical device that absorbs shocks (railway buffers, buffer springs). Computing adopted it in the 1950s, when IBM mainframes used "buffers" to decouple tape reads from CPU speed. The metaphor is exact: a buffer absorbs the shock of speed mismatch.

A buffer is a region of memory used to temporarily hold data while it is being moved from one place to another.

— IEEE Std 610.12-1990, *Glossary of Software Engineering Terminology*
19th c. · "buffer" mecánico (ferrocarril)
1950s · buffer en mainframes IBM
1981 · SO_RCVBUF en BSD sockets
2010s · ring buffers en sistemas de mensajería

Evidence

FAQ

Buffer vs cache?

A cache stores for reuse; a buffer stores for transfer. A cache is read many times; a buffer is written and read once.

When does a buffer add latency?

Always. The question is whether the latency is worth the throughput. Small buffers = low latency, low throughput. Large buffers = high throughput, high latency.

How to pick a size?

Measure the burst. The buffer must absorb the worst spike without dropping, at the cost of the peak latency that the buffer introduces.

Bounded vs unbounded?

Bounded always, unless you like OOM kills. Bound it, and decide what to do when full: block, drop, or spill to disk.

Connections

Etymology

Origin: borrowed

  • buffer (noun): from Old French buffe, "a blow"; mechanical sense (railway buffer) from 19th century

Semantic network

BufferperformancioCacheQueueBackpressuring-buffeLatency

● broader ● narrower ● related

Broader

  • performance
  • io

Related

Used in the garden

Sources