Inferensys

Glossary

Reservoir Sampling

Reservoir sampling is a probabilistic algorithm designed to select a uniformly random sample of fixed size from a data stream of unknown or infinite length, with each element having an equal probability of being included.
Data scientist building training data pipeline on laptop, data preprocessing visible, technical workspace.
ALGORITHM

What is Reservoir Sampling?

Reservoir Sampling is a family of randomized algorithms designed to select a simple random sample of a fixed size from a data stream of unknown or very large length, with each element having an equal probability of being included in the final sample.

Reservoir Sampling is a probabilistic algorithm for buffer management in continual learning. It maintains a uniformly random subset, or 'reservoir,' of fixed size k from a potentially infinite stream of data. The core algorithm, Algorithm R, processes items sequentially: the first k items fill the reservoir, and each subsequent i-th item replaces a randomly chosen reservoir entry with probability k/i. This guarantees every item in the stream has an equal probability of being in the final reservoir, enabling unbiased sampling from non-stationary distributions without prior knowledge of the stream's length.

In Edge-CL, reservoir sampling is critical for implementing experience replay within strict memory constraints. It provides a statistically sound method for maintaining a replay buffer that represents the overall data distribution seen so far. This allows models to rehearse past experiences efficiently, directly combating catastrophic forgetting. Variations like weighted reservoir sampling adapt the algorithm for non-uniform sampling, which is useful for prioritizing rare or informative examples in the buffer to improve learning efficiency and model robustness in sequential tasks.

ALGORITHM FUNDAMENTALS

Key Properties of Reservoir Sampling

Reservoir Sampling is a family of randomized algorithms designed to select a uniform random sample of k items from a data stream of unknown length n, where n can be very large or potentially infinite, using only O(k) memory.

01

Single-Pass Guarantee

The core property of reservoir sampling is its single-pass operation. The algorithm processes each element of the stream exactly once, without the need to store the entire stream or revisit previous elements. This makes it memory-efficient and suitable for online learning and streaming data scenarios where data arrives continuously and cannot be stored in full.

  • Mechanism: It maintains a 'reservoir' of size k. For the first k elements, it fills the reservoir. For each subsequent element i (where i > k), it includes the element with probability k/i, replacing a randomly chosen existing reservoir element.
02

Uniform Random Sampling

The defining statistical guarantee: after processing n elements, every subset of size k from the n elements has an equal probability of being the final reservoir. Each individual element has a probability of exactly k/n of being in the sample. This property holds regardless of the order of the stream.

  • Proof Sketch: For element i, the probability it enters the reservoir is k/i. The probability it survives all subsequent replacement attempts (for elements j from i+1 to n) is Π (1 - k/(j * k)) = i/n. Multiplying these gives k/n.
  • Implication for Continual Learning: This ensures the replay buffer is an unbiased statistical sample of the entire data stream, preventing distributional skew in rehearsal.
03

Algorithm R (The Classic Version)

Algorithm R is the foundational, simple reservoir sampling algorithm. It is straightforward to implement and provides the uniform sampling guarantee.

Pseudocode:

code
Initialize reservoir R with first k elements.
for i = k+1 to n:
    j = random integer in [1, i]
    if j <= k:
        R[j] = element i
  • Complexity: O(n) time, O(k) space.
  • Drawback: It requires generating a random number for every element in the stream after the first k, which can be a computational bottleneck for extremely high-speed streams.
04

Algorithm L (Optimized for Speed)

Algorithm L is an optimized version that reduces the number of random number generations. Instead of checking every element, it uses skip intervals calculated from a geometric distribution.

  • Mechanism: It calculates how many elements to skip before the next candidate for replacement, based on the current reservoir size and a random variable. This dramatically reduces the number of iterations in the main loop.
  • Advantage: While maintaining the uniform sampling property, it runs in O(k(1 + log(n/k))) expected time, which is significantly faster than Algorithm R for large n.
  • Use Case: Preferred in high-throughput streaming applications or when the sampling operation itself is a performance-critical path.
05

Application in Continual Learning Buffers

In Continual Learning, reservoir sampling is the standard algorithm for buffer management in rehearsal-based methods like Experience Replay.

  • Process: As new task data streams in, the algorithm decides which new samples to insert into the fixed-size replay buffer and which old ones to evict.
  • Why it works: It ensures the buffer always contains a uniform random sample of all data seen so far. This provides a statistically sound basis for interleaving old and new data during training, which is crucial for mitigating catastrophic forgetting.
  • Contrast with Heuristics: Unlike FIFO (First-In-First-Out) or other deterministic strategies, reservoir sampling prevents bias towards recent data, promoting better long-term knowledge retention.
06

Weighted Reservoir Sampling

A powerful generalization where each stream element i has an associated weight w_i. The algorithm selects a sample where the probability of an element being chosen is proportional to its weight.

  • Use Case in ML: Extremely relevant for importance sampling and non-uniform buffer management. For example, samples with higher loss or gradient magnitude can be given higher weight to be retained in the buffer, as they may be more informative for rehearsal.
  • Algorithm: Variants like Algorithm A-Chao or Algorithm A-Res extend the core idea to handle weights, maintaining O(k) memory but with more complex per-element operations.
  • Connection: This bridges simple random sampling to more sophisticated, curriculum-based or hard example mining strategies within continual learning frameworks.
RESERVOIR SAMPLING

Frequently Asked Questions

Reservoir Sampling is a foundational probabilistic algorithm for managing data streams in continual learning. These questions address its core mechanics, applications, and trade-offs for edge AI systems.

Reservoir Sampling is a family of randomized algorithms designed to select a simple random sample of k items from a data stream of unknown, potentially infinite length n, with each item having an equal probability of being included in the final sample. The classic Algorithm R works by initially filling a reservoir (a buffer of size k) with the first k stream elements. For each subsequent element i (where i > k), the algorithm generates a random integer j uniformly from [1, i]. If j ≤ k, the element at reservoir index j is replaced with the new stream element i. This process guarantees that after processing n elements, every possible subset of size k is equally likely to be the reservoir's contents, providing a uniform random sample without requiring prior knowledge of n.

Key Property: The probability any given element is in the final reservoir is exactly k / n.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.