RabbitMQ PHP Consumer Database Connection

I am considering using RabbitMQ for some async processing in my new app.

One of the challenges I am facing is how best to handle Database connections. I will be using Eloquent and in effect PDO.

One resource I found online here https://www.teqneers.de/2013/10/simple-spawn-rabbitmq-consumers-with-php/ suggests spawning a new process using proc_open but I am not really comfortable with this approach and wonder if there is a better approach.

I also tried looking best practices for handling a database connection in a PHP daemon but didn’t find anything.

Some things I have thought about.

  1. Creating a new connection and closing it per message

    • Cons: App is about delivering messages to bulk messages and consumers will be numerous and run often. Might be resource heavy.
    • Pros: However, traditional php scripts work in a similar way so this might not be such a bad idea
  2. Keeping the connection throughout the lifetime of the consumer and killing them often

    • Cons: Obviously wasteful. Need to constantly check if the connection is still active.
  3. Running a worker on the command line

    • Cons: Too many moving parts. Need to parse the response for message ACKS hence the consumer needs to wait for the worker to end. Means creating and closing connections per message, might as well stick with option 1 as it has less parts.

I also thought of persistent connections, but after reading more about them, it seems they are not what I thought they were. Also, many posts say it is a bad idea anyway.

I understand PHP might not be the best job for this, but I am rapidly prototyping and PHP is the language I am most comfortable in for this task.

I would really appreciate it if I could get some advice on how to handle this.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s