SyntaxStudy
Sign Up
MySQL Event-Driven Queue Processing
MySQL Advanced 4 min read

Event-Driven Queue Processing

DB-backed Queue

A database queue table combined with a MySQL event can process background jobs without an external queue system — useful for small-scale needs.

Example
DELIMITER //
CREATE EVENT process_job_queue
ON SCHEDULE EVERY 1 MINUTE
DO
BEGIN
  DECLARE done INT DEFAULT 0;
  -- Pick and lock next pending job
  UPDATE job_queue SET status = "processing", started_at = NOW(), worker = CONNECTION_ID()
  WHERE status = "pending" ORDER BY created_at LIMIT 1;
  -- Log processing attempt
  IF ROW_COUNT() > 0 THEN
    INSERT INTO event_audit (event_name, run_at) VALUES ("queue_processor", NOW());
  END IF;
END//
DELIMITER ;
Pro Tip

For high-throughput queues, use Redis (Laravel), SQS, or RabbitMQ instead of a DB-backed queue.