Rate Limiting Strategies for API Integrations in Landing

Introduction

When integrating with external APIs, it's crucial to respect their rate limits to ensure stable performance and avoid being throttled or blocked. This post will explore a practical approach to managing rate limits when syncing data to an external service within the landing project.

The Problem: Exceeding Rate Limits

The landing project integrates with an external service (Resend) to send emails. Without proper rate limiting, syncing a large number of contacts can easily exceed the service's rate limit (2 requests per second in this case), leading to errors and incomplete data synchronization. The goal is to implement a strategy that respects the rate limit while efficiently syncing data.

Solution: Chunking and Delayed Execution

To address this, the contact syncing process is divided into smaller chunks, with a short delay inserted between processing each chunk. This ensures that the API calls are spread out over time, staying within the rate limit.

Here's a PHP example demonstrating how this can be implemented:

<?php

function syncContactsInChunks(array $contacts, int $chunkSize, float $delay) {
    $chunks = array_chunk($contacts, $chunkSize);

    foreach ($chunks as $index => $chunk) {
        // Calculate the delay before processing this chunk
        $sleepTime = $index > 0 ? $delay : 0;
        sleep($sleepTime);

        // Process the chunk of contacts
        processContactChunk($chunk);
    }
}

function processContactChunk(array $contacts) {
    foreach ($contacts as $contact) {
        // Simulate sending data to the external API
        echo "Syncing contact: " . $contact['email'] . "\n";
        // In reality, this would be an API call to the external service
    }
}

// Example usage
$contacts = [];
for ($i = 1; $i <= 10; $i++) {
    $contacts[] = ['email' => "user{$i}@example.com"];
}

$chunkSize = 2; // Pairs of contacts
$delay = 1.1;   // 1.1 second delay between chunks

syncContactsInChunks($contacts, $chunkSize, $delay);

?>

This code divides the contacts into chunks of two. It then iterates through these chunks, pausing for 1.1 seconds before processing each one. This ensures that the rate limit of two requests per second is respected.

Adjusting Job Timeouts

When dealing with larger recipient lists, the overall synchronization process naturally takes longer. To accommodate this, it's essential to increase the job timeout to prevent premature termination. In this case, the job timeout was increased from 300 seconds to 900 seconds to handle larger contact lists.

Conclusion

When integrating with external APIs, proactively addressing rate limits is essential for maintaining system stability. By implementing chunking with delayed execution, you can effectively manage API call frequency and avoid being throttled. Remember to adjust job timeouts to accommodate longer processing times for larger datasets.

Actionable Takeaway: Review your API integrations and implement chunking and delayed execution strategies to respect external service rate limits. Monitor your job execution times and adjust timeouts accordingly.

Rate Limiting Strategies for API Integrations in Landing
GERARDO RUIZ

GERARDO RUIZ

Author

Share: