Skip to content

blackrock/micro-batcher

Micro Batcher

Micro Batcher is a lightweight, zero-dependency, and experimental interval-based micro-batching library for TypeScript/JavaScript.

Table of Contents

Installation

npm install @blackrock-oss/micro-batcher
yarn add @blackrock-oss/micro-batcher
pnpm add @blackrock-oss/micro-batcher

Examples

The Examples Directory is a great resource for learning how to setup Micro Batcher.

Web Application is a playground application which provides examples on how to configure and integrate Micro Batcher, including error handling scenarios and batch resilience demonstrations.

Usage

Micro Batcher in a nutshell

Micro Batcher is a decorator utility that enhances the input function with additional functionalities such as batching, throttling, and more.

By decorating the original function with Micro Batcher, it produces an enhanced function with the same function signature, allowing developers to seamlessly replace the original function with the enhanced version.

If a batching function is provided to Micro Batcher, calls to the decorated function within a short interval are intercepted, their payloads are accumulated, then forwarded to the batching or original function for processing, and the results are distributed back to the respective callers.

micro batcher demo

API Examples

Below code snippets are taken from the Example Web Application. Micro Batcher is used to generate a decorated function that is being used by data fetching workflow.

export const fetchSingleSecurity = async (cusip: string): Promise<Security> => {
  // Data Fetching Implementation
};

const batchFetchSecurities = async (cusips: string[]): Promise<Security[]> => {
  // Data Fetching Implementation
};

export const decoratedFetchSecurity = MicroBatcher(fetchSingleSecurity)
  .batchResolver(batchFetchSecurities, {
    payloadWindowSizeLimit: 4,
    batchingIntervalInMs: 50
  })
  .build();

The decorated function has the same signature as the original, so it can be used as a drop-in replacement:

// With Micro Batcher enabled
const result = await decoratedFetchSecurity('AAPL');

// Without Micro Batcher — same signature, same result
const result = await fetchSingleSecurity('AAPL');

Other Examples

Example 1: Single Parameter Function
// Original function
const multiplyByTwo = (input: number): Promise<number> => {...};

// Batch resolver function for "multiplyByTwo" function
const batchMultiplyByTwo = (inputs: number[]): Promise<number[]> => {...};

const multiplyByTwoBatcher: (input: number) => Promise<number> =
  MicroBatcher<number, number>(multiplyByTwo)
    .batchResolver(batchMultiplyByTwo)
    .build();
Example 2: Multiple Parameters Function
// Original function
const multiply = (input1: number, input2: number): Promise<number> => {...};

// Batch resolver function for "multiply" function
const batchMultiply = (inputs: [number, number][]): Promise<number[]> => {...};

const multiplyBatcher: (input1: number, input2: number) => Promise<number> =
  MicroBatcher<[number, number], number>(multiply)
    .batchResolver(batchMultiply)
    .build();
Example 3: Override Default Batching Interval

The default batching interval is 50ms, which can be overridden using batchingIntervalInMs in the batch options.

const multiplyBatcher: (input1: number, input2: number) => Promise<number> = MicroBatcher<
  [number, number],
  number
>(multiply)
  .batchResolver(batchMultiply, {
    batchingIntervalInMs: 100
  })
  .build();
Example 4: Specify Payload Window Size Limit

By default, Micro Batcher accumulates all caller payloads based on the batching interval.

However, an optional batch option payloadWindowSizeLimit can specify the upper limit of the accumulation size.

Upon reaching the limit, the payloads are immediately delegated to the batch resolver.

const multiplyBatcher: (input1: number, input2: number) => Promise<number> = MicroBatcher<
  [number, number],
  number
>(multiply)
  .batchResolver(batchMultiply, {
    payloadWindowSizeLimit: 5
  })
  .build();
Example 5: Error Strategy

By default, Micro Batcher uses the broadcast error strategy — if the batch resolver throws, all callers in the batch receive the same error.

The isolate error strategy enables per-item error handling. The batch resolver returns Promise<TReturnType>[] (an array of independent promises), allowing each caller to settle independently. A failed or cancelled caller bails immediately without waiting for or affecting the rest of the batch.

// Broadcast (default) — batch resolver error rejects all callers
const decoratedFn = MicroBatcher(fetchSingle)
  .batchResolver(batchFetch) // errorStrategy defaults to { type: 'broadcast' }
  .build();

// Isolate — per-item promises, each caller settles independently
const batchFetchIsolated = (cusips: string[]): Promise<Security>[] => {
  return cusips.map((cusip) => fetchSingle(cusip)); // each item is an independent promise
};

const decoratedFn = MicroBatcher(fetchSingle)
  .batchResolver(batchFetchIsolated, {
    errorStrategy: { type: 'isolate' }
  })
  .build();

In isolate mode, if a caller's underlying API call is cancelled or fails, that caller receives the error immediately while other callers in the same batch continue processing. If the batch resolver itself throws synchronously, all callers are rejected (broadcast fallback).

Development

Local Development

pnpm install

Build

pnpm run build

Test

# Run tests
pnpm run test

# Run tests with coverage report
pnpm run test:coverage

# Watch mode
pnpm run test:dev

Roadmap

Features

  • Concurrent Batcher Limit Support
  • Rate Limiting and Throttling Policies Support

Contributing

License

Micro Batcher is Apache Licensed.

Contact

GitHub Issues: https://github.com/blackrock/micro-batcher/issues

About

Interval-based micro batching library for TypeScript/JavaScript

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors