Optimizing Laravel Performance with Docker and Queues

Introduction

As Laravel applications scale, performance optimization becomes crucial for handling high traffic, background processes, and efficient resource management. Two powerful tools that help streamline Laravel’s performance are Docker and Queues.

  • Docker enhances deployment by isolating environments, ensuring smooth scaling and reproducibility.
  • Queues offload intensive tasks, preventing long request times and improving responsiveness.

In this blog, we’ll explore how to:
✔ Set up Laravel with Docker for performance optimization
✔ Implement Queues for efficient background processing
✔ Apply best practices for scalable Laravel applications

Setting Up Laravel with Docker for Performance Boost

Docker containerizes Laravel applications, ensuring consistency across different environments.

Step 1: Install Docker & Docker Compose

Ensure Docker and Docker Compose are installed on your system:

docker --version
docker-compose --version

Step 2: Create a Docker-Compose File

Define Laravel’s services (PHP, MySQL, Nginx) in docker-compose.yml:

version: '3'
services:
  app:
    image: php:8.1-fpm
    container_name: laravel_app
    volumes:
      - .:/var/www
    depends_on:
      - db
  db:
    image: mysql:8
    container_name: laravel_db
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: laravel_db
  web:
    image: nginx:latest
    container_name: laravel_nginx
    volumes:
      - .:/var/www
      - ./nginx.conf:/etc/nginx/nginx.conf
    depends_on:
      - app
    ports:
      - "8000:80"

Step 3: Build & Run Containers

Run the Laravel application inside Docker:

docker-compose up -d

Step 4: Optimize Laravel Inside Docker

Enable OPcache, configure workers, and use caching for best performance:

Optimize PHP Execution

Modify php.ini inside the Docker container:

opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000

Apply changes:

docker restart laravel_app

Use Laravel Caching Inside Docker

Run cache optimizations:

php artisan config:cache
php artisan route:cache
php artisan view:cache

Using Queues to Optimize Laravel Performance

Queues allow Laravel to process tasks asynchronously, ensuring faster request times and better resource utilization.

Step 1: Configure Laravel Queue Driver

Modify .env to use Redis for queues:

QUEUE_CONNECTION=redis

Install Redis inside Docker:

services:
  redis:
    image: redis:alpine
    container_name: laravel_redis
    ports:
      - "6379:6379"

Step 2: Define Queue Jobs

Create a queueable task:

php artisan make:job SendEmailJob

php artisan make:job SendEmailJob

Update SendEmailJob.php:

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class SendEmailJob implements ShouldQueue {
    use InteractsWithQueue, Queueable;
    
    public function __construct(public $user) {}

    public function handle() {
        Mail::to($this->user->email)->send(new WelcomeEmail());
    }
}

Step 3: Dispatch Jobs to Queues

Run a queued job:

dispatch(new SendEmailJob($user));

Step 4: Process Queue Jobs Inside Docker

Start the Laravel queue worker inside the container:

docker exec -it laravel_app php artisan queue:work --daemon

Best Practices for Laravel Optimization

Use Redis Caching – Store database queries and sessions in Redis
Enable OPcache – Improve PHP performance significantly
Use Supervisor for Queue Workers – Ensure stable processing
Scale Horizontally with Docker – Run multiple app containers for load balancing

Conclusion

By containerizing Laravel with Docker and leveraging Queues, developers can build scalable, high-performance applications. Docker ensures consistent deployments, while Queues enable asynchronous task execution.

Leave a Reply

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