Unlock the Power of Bottleneck Comprehensive Guide with APIs Examples

Master Bottleneck in Node.js: A Comprehensive Guide with API Examples

Bottleneck is a powerful rate-limiting library for Node.js designed to control the maximum number of concurrent and delay of tasks in your application. Whether you’re handling API requests, database operations, or resource-intensive jobs, Bottleneck ensures you can control congestion and optimize performance seamlessly. In this blog post, we will dive into the concept of Bottleneck, explore its powerful APIs with a plethora of examples, and demonstrate how you can use it in a real-world application.

What is Bottleneck?

Bottleneck is an efficient Node.js library designed to throttle operations and limit the number of concurrent jobs being carried out. By using Bottleneck, developers can streamline application workflows, manage API request limits, and prevent accidental flooding of resources.

Installation

To install Bottleneck in your project, simply run:

  npm install bottleneck

Core Features and APIs

Below are the key features and APIs of Bottleneck with usage examples:

1. Creating a Bottleneck Instance

Create an instance to manage the rate limits:

  const Bottleneck = require("bottleneck");

  const limiter = new Bottleneck({
    maxConcurrent: 5,
    minTime: 100
  });

Here, maxConcurrent limits the number of concurrent tasks, and minTime ensures a minimum delay between task executions.

2. Scheduling Functions

Use schedule to add tasks and control their execution:

  limiter.schedule(() => fetch("https://api.example.com/data"))
    .then(console.log)
    .catch(console.error);

3. Using wrap for Wrapping Functions

Wrap a function with the limiter to automatically rate-limit its execution:

  const limitedFetch = limiter.wrap(fetch);

  limitedFetch("https://api.example.com/data")
    .then(console.log)
    .catch(console.error);

4. Event Management

Use events to track task execution and performance:

  limiter.on("error", (error) => {
    console.error("Error occurred:", error);
  });

  limiter.on("done", (jobInfo) => {
    console.log("Task completed:", jobInfo);
  });

5. Chaining Requests

Chain tasks with Bottleneck:

  const taskOne = () => Promise.resolve("Task 1 done");
  const taskTwo = () => Promise.resolve("Task 2 done");

  limiter.schedule(taskOne)
    .then(() => limiter.schedule(taskTwo))
    .then(() => console.log("All tasks completed!"));

6. Dynamic Updates

Adjust rate-limiting properties dynamically:

  limiter.updateSettings({
    maxConcurrent: 10,
    minTime: 50,
  });

7. Stopping the Limiter

Stop the limiter after completing all pending tasks:

  limiter.stop({
    dropWaitingJobs: false, 
  });

8. Advanced Grouping with Bottleneck

Handle multiple limiters using groups:

  const limiterGroup = new Bottleneck.Group({
    maxConcurrent: 2,
  });

  const limiterOne = limiterGroup.key("task1");
  const limiterTwo = limiterGroup.key("task2");

  limiterOne.schedule(() => taskOne());
  limiterTwo.schedule(() => taskTwo());

Real-World Application Example

Let’s create an example application that uses Bottleneck to throttle API requests while fetching data from a mock JSON placeholder API:

  const Bottleneck = require("bottleneck");
  const fetch = require("node-fetch");

  const limiter = new Bottleneck({
    maxConcurrent: 3,
    minTime: 200
  });

  async function fetchData(endpoint) {
    console.log(`Fetching ${endpoint}...`);
    const response = await limiter.schedule(() => fetch(endpoint));
    const data = await response.json();
    console.log(`Fetched data:`, data);
  }

  async function main() {
    const apiEndpoints = [
      "https://jsonplaceholder.typicode.com/posts/1",
      "https://jsonplaceholder.typicode.com/posts/2",
      "https://jsonplaceholder.typicode.com/posts/3",
    ];

    const promises = apiEndpoints.map((endpoint) => fetchData(endpoint));
    await Promise.all(promises);
  }

  main().catch(console.error);

Conclusion

Bottleneck is a versatile and highly efficient library for managing rate limits and controlling concurrency in your Node.js application. It provides a wide range of features like task scheduling, event management, dynamic updates, and more. Whether you’re making multiple API calls or managing complex workflows, Bottleneck ensures you remain in control. Try it out today and optimize your application logic with ease!

Leave a Reply

Your email address will not be published. Required fields are marked *