mateusjunges/laravel-kafka
Laravel Kafka makes it easy to produce and consume Kafka messages in Laravel with a clean, expressive API and improved testability. Build producers and consumers quickly, integrate with your app workflows, and avoid painful Kafka testing setups.
Kafka::publish()) from consumption (e.g., Kafka::consumer()), aligning with Laravel’s service container and dependency injection principles.FlixTech\AvroSerializer) enables structured event schemas, critical for scalable, versioned event systems (e.g., e-commerce order processing).Kafka::fake()) integrate seamlessly with Laravel’s testing tools (Pest/PHPUnit), reducing flakiness in CI/CD pipelines.rdkafka Extension: Requires native rdkafka PHP extension, adding a build/deployment dependency. Docker/Kubernetes environments can mitigate this via pre-built images.KafkaServiceProvider), but manual broker configuration (e.g., config/kafka.php) is required upfront.| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| Broker Connectivity | High | Implement circuit breakers (e.g., Laravel Horizon health checks) and exponential backoff for retries. |
| Schema Registry | Medium | Use cached schema registries (as shown in Avro docs) to avoid latency spikes. |
| Consumer Lag | High | Monitor offsets via Kafka tools (e.g., kafka-consumer-groups) and implement dead-letter queues for poison pills. |
| Testing Complexity | Low | Leverage Kafka::fake() for unit tests; use contract testing for consumer validation. |
| Performance | Medium | Benchmark async producers vs. sync for throughput needs; consider batch processing for high-volume topics. |
Broker Topology:
Consumer Scaling:
Error Handling:
dead-letter-topic?)Observability:
Migration Path:
php-rdkafka), what’s the data migration plan for topics/schemas?Event::dispatch() → Kafka) or commands (e.g., Artisan::command() → Kafka).Kafka::fake()) and Dusk (for UI-triggered Kafka events).rdkafka images) or Kubernetes (with StatefulSets for brokers).rdkafka: More control but requires boilerplate for Laravel integration.| Phase | Action Items | Tools/Libraries |
|---|---|---|
| Assessment | Audit existing Kafka usage (topics, schemas, consumers). | kafka-topics.sh, kafka-console-consumer |
| Setup | Install rdkafka extension and package (composer require mateusjunges/laravel-kafka). |
Docker, Laravel Sail |
| Configuration | Define config/kafka.php with broker URLs, topics, and Avro schemas. |
php artisan kafka:config (if available) |
| Producer Refactor | Replace direct rdkafka calls with Kafka::publish(). |
IDE refactoring tools |
| Consumer Refactor | Migrate consumers to use Kafka::consumer()->withHandler(). |
Laravel Horizon for worker management |
| Testing | Replace mocks with Kafka::fake() and add assertions. |
Pest/PHPUnit |
| Deployment | Roll out in stages (e.g., non-critical topics first). | Feature flags, canary releases |
| Observability | Add metrics/logging for producers/consumers. | Laravel Telescope, Prometheus |
Event::dispatch() → Kafka::publish() for event-driven workflows.KafkaJob::dispatch()->delay(5)).Phase 1: Producers
rdkafka calls with Kafka::publish().Kafka::fake() in tests.Phase 2: Consumers
Kafka::consumer()->withHandler() for new logic.Phase 3: Observability
Phase 4: Scaling
rdkafka updates may require PHP extension rebuilds.kafka-topics.sh for scaling partitions/replicas.rdkafka logs for connection errors.How can I help you explore Laravel packages today?