← Back to Module

Events & Queues Demonstrator

See the dramatic difference between synchronous and asynchronous processing

Real-World Scenario: User Registration

When a user registers on your platform, you typically need to:

Total time if synchronous: ~2500ms
The user must wait while staring at a loading spinner. If any external service fails, the entire registration fails!

❌ Synchronous Approach

Everything runs during the HTTP request

Expected Issues:

  • Slow response (~2500ms)
  • User waits for everything
  • Failures break registration

✅ Asynchronous with Queues

Tasks run in background workers

Expected Benefits:

  • Fast response (~50ms)
  • Instant user feedback
  • Failures don't break flow

Processing registration...

Performance Metrics

Response Time
Database Queries
Memory Used
Status

Created User

ID:

Name:

Email:

Created:

Task Execution Timeline

✓ Immediate Tasks (Completed)

⏳ Background Jobs (Processing Asynchronously)

Note: These tasks are running in background queue workers. The user received an instant response and doesn't have to wait for these to complete!

Registration Failed!

How It Works

❌ Synchronous (Bad)

// All tasks run during request
$user = User::create($data);
Mail::send($welcomeEmail);     // 800ms
CRM::updateContact($user);     // 1200ms
Analytics::track($event);      // 300ms

// User waits ~2500ms total

✅ Asynchronous (Good)

// Only create user
$user = User::create($data);   // 50ms

// Fire event (instant)
UserRegistered::dispatch($user);

// Response returns immediately!
// Queued listeners run in background

Key Takeaways

When to Use Queues:

  • ✓ Sending emails
  • ✓ Processing images/videos
  • ✓ Calling third-party APIs
  • ✓ Generating reports
  • ✓ Any task > 100ms

Implementation:

  • 1. Create Event: UserRegistered
  • 2. Create Listeners implementing ShouldQueue
  • 3. Fire event: UserRegistered::dispatch()
  • 4. Run worker: php artisan queue:work