Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 18, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 18, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 18, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 18, 2025

      I need to see more from Lenovo’s most affordable gaming desktop, because this isn’t good enough

      May 18, 2025

      Gears of War: Reloaded — Release date, price, and everything you need to know

      May 18, 2025

      I’ve been using the Logitech MX Master 3S’ gaming-influenced alternative, and it could be your next mouse

      May 18, 2025

      Your Android devices are getting several upgrades for free – including a big one for Auto

      May 18, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      YTConverter™ lets you download YouTube videos/audio cleanly via terminal — especially great for Termux users.

      May 18, 2025
      Recent

      YTConverter™ lets you download YouTube videos/audio cleanly via terminal — especially great for Termux users.

      May 18, 2025

      NodeSource N|Solid Runtime Release – May 2025: Performance, Stability & the Final Update for v18

      May 17, 2025

      Big Changes at Meteor Software: Our Next Chapter

      May 17, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      I need to see more from Lenovo’s most affordable gaming desktop, because this isn’t good enough

      May 18, 2025
      Recent

      I need to see more from Lenovo’s most affordable gaming desktop, because this isn’t good enough

      May 18, 2025

      Gears of War: Reloaded — Release date, price, and everything you need to know

      May 18, 2025

      I’ve been using the Logitech MX Master 3S’ gaming-influenced alternative, and it could be your next mouse

      May 18, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Managing API Rate Limits in Laravel Through Job Throttling

    Managing API Rate Limits in Laravel Through Job Throttling

    November 29, 2024

    Managing API Rate Limits in Laravel Through Job Throttling

    When working with external services like AWS SES for email delivery, preventing API flooding is crucial. Laravel provides an elegant solution through Redis::throttle to manage your rate limits effectively. Let’s explore how to implement this to maintain optimal API usage.

    Understanding Redis::throttle

    The Redis::throttle method in Laravel offers a powerful way to control the flow of your queued jobs. This ensures you stay within API rate limits and avoid temporary or permanent service blocks.

    Redis::throttle('key-name')
        ->allow(10)
        ->every(5)
        ->then(function () {
            // Your job logic here
        });
    

    Real-World Implementation

    Let’s build a practical example for managing email sending rates with AWS SES. We’ll create a complete middleware system to handle rate limiting:

    <?php
    
    namespace AppHttpMiddleware;
    
    use Closure;
    use IlluminateSupportFacadesRedis;
    
    class EmailRateLimit
    {
        public function handle($job, Closure $next)
        {
            Redis::throttle('email-throttle')
                ->block(2)
                ->allow(10)
                ->every(2)
                ->then(
                    function () use ($job, $next) {
                        $next($job);
                    },
                    function () use ($job) {
                        $job->release(30);
                    }
                );
        }
    }
    

    To apply this in your notification system:

    <?php
    
    namespace AppNotifications;
    
    use IlluminateBusQueueable;
    use IlluminateContractsQueueShouldQueue;
    use IlluminateNotificationsNotification;
    use IlluminateNotificationsMessagesMailMessage;
    use AppHttpMiddlewareEmailRateLimit;
    
    class EmailDispatch extends Notification implements ShouldQueue
    {
        use Queueable;
    
        protected $content;
    
        public function __construct($content)
        {
            $this->content = $content;
        }
    
        public function via($notifiable)
        {
            return ['mail'];
        }
    
        public function toMail($notifiable)
        {
            return (new MailMessage)
                ->subject($this->content['subject'])
                ->line($this->content['body']);
        }
    
        public function middleware(): array
        {
            return [new EmailRateLimit];
        }
    }
    

    And finally, implementing it in your controller:

    <?php
    
    namespace AppHttpControllers;
    
    use AppModelsUser;
    use AppNotificationsEmailDispatch;
    use IlluminateHttpRequest;
    
    class MailController extends Controller
    {
        public function dispatch(Request $request)
        {
            $content = [
                'subject' => 'Important Update',
                'body' => 'Your account has been updated successfully.'
            ];
    
            $user = User::find($request->user_id);
            $user->notify(new EmailDispatch($content));
    
            return response()->json(['message' => 'Email queued for delivery']);
        }
    }
    

    This implementation ensures your application respects API rate limits while maintaining efficient email delivery through AWS SES or any other email service provider.


    The post Managing API Rate Limits in Laravel Through Job Throttling appeared first on Laravel News.

    Join the Laravel Newsletter to get all the latest
    Laravel articles like this directly in your inbox.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleNovember 2024: People on the Move
    Next Article The Unexpected Powerhouse: Building Static Sites with Drupal

    Related Posts

    Development

    February 2025 Baseline monthly digest

    May 18, 2025
    Artificial Intelligence

    Markus Buehler receives 2025 Washington Award

    May 18, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Exploring JavaScript (ES2024 Edition)

    Development

    Ashford Creative

    Development

    Enabling Commerce Innovation with the Power of MongoDB and Google Cloud

    Databases

    Ransomware Extortion Drops to $813.5M in 2024, Down from $1.25B in 2023

    Development

    Highlights

    CVE-2025-3841 – Wix Incubator Jam Jinja2 Template Handler Template Injection Vulnerability

    April 21, 2025

    CVE ID : CVE-2025-3841

    Published : April 21, 2025, 8:15 p.m. | 1 hour, 13 minutes ago

    Description : A vulnerability, which was classified as problematic, was found in wix-incubator jam up to e87a6fd85cf8fb5ff37b62b2d68f917219d07ae9. This affects an unknown part of the file jam.py of the component Jinja2 Template Handler. The manipulation of the argument config[‘template’] leads to improper neutralization of special elements used in a template engine. It is possible to launch the attack on the local host. The exploit has been disclosed to the public and may be used. This product takes the approach of rolling releases to provide continious delivery. Therefore, version details for affected and updated releases are not available.

    Severity: 3.3 | LOW

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    Google Drive is now available for Arm64 Windows 11 PCs

    March 26, 2025

    How can NLP Chatbots Improve Business Growth in 2024?

    May 20, 2024

    PoC Tool Released for Max Severity Apache Parquet Vulnerability to Detect Affected Servers

    May 7, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.