Run Multiple Django Apps With Celery On One Server With Rabbitmq VHosts

29 Apr · by Tim Kamanin · 0 min read

Sometimes, we can have multiple Django apps, each running celery, on a single server. If celery workers use Rabbitmq as a broker, we need to isolate them so that each worker uses a separate rabbitmq instance.

Luckily, Rabbitmq has a concept of virtual hosts, so we can create a host per celery app and call it a day. Here's how to do this:

  1. Create rabbitmq server vhost at first:

    bash
    sudo rabbitmqctl add_vhost our_app_vhost
    
  2. Add user / password pair

    bash
    sudo rabbitmqctl add_user username password
    
  3. Grant permissions to our user on our vhost:

    bash
    sudo rabbitmqctl set_permissions -p our_app_vhost username ".*" ".*" ".*"
    
  4. If you want to give our user access to management backend execute this command:

    bash
    sudo rabbitmqctl set_user_tags username management
    
  5. Now you can add BROKER_URL to your Django app settings:

    python
    BROKER_URL = 'amqp://username:password@localhost:5672/our_app_vhost'
    

We have just created a virtual host for our Django app. Using this technique, create a virtual host for every Django/Celery app you have and enjoy the peace of mind.

Want to get more 🔥 tips like this one?

Subscribe to get notified about new dev tutorials