Tutorials Courses Interview Prep (100 Q&A) AI Tools (50) Developer Tools (50) Tracks Pricing Plans Blog & Articles Contact Us
Laravel Advanced & Architecture 5-8 Years (Senior) Medium Production Scenario

How do you monitor and handle Slow Query alerts in real-time in Laravel production?

Real-World Scenario / Incident Description

You need an automated Slack/Sentry alert whenever any database query takes longer than 500ms in production.

287 views • Updated 1 month ago

Using DB::whenQueryingForLongerThan

In AppServiceProvider::boot():

DB::whenQueryingForLongerThan(500, function (Connection $connection) {
    Log::warning("Database query exceeded 500ms on [{$connection->getName()}]");
    // Send alert to Sentry or Slack webhook
});
Related Concepts: #Monitoring #Database #Performance #DB Listeners