7

I have a Procfile like so:

web:     bundle exec rails server -p $PORT
em:      script/eventmachine

The em process fires up an eventmachine with start_server (port ENV['PORT']) and my web process occasionally needs to communicate with it.

My question is how does the web process know what port to communicate with it on? If I understand heroku correctly it assigns you a random port when the process starts up (and it can change if the ps is killed or restarted). Thanks!

Brian Armstrong
  • 19,707
  • 17
  • 115
  • 144

3 Answers3

5

According to Heroku documentation,

Two processes running on the same dyno can communicate over TCP/IP using whatever ports they want.

Two processes running on different dynos cannot communicate over TCP/IP at all. They need to use memcached, or the database, or one of the Heroku plugins, to communicate.

ChrisPhoenix
  • 1,040
  • 1
  • 11
  • 16
  • 2
    Current version of the documentation of this is [here](https://devcenter.heroku.com/articles/dynos#networking) – weibeld Dec 11 '17 at 22:10
3

Processes are isolated and cannot communicate directly with each other.

http://www.12factor.net/processes

There are, however, a few other ways. One is to use a backing service such as Redis, or Postgres to act as an intermediary - another is to use FIFO to communicate.

http://en.wikipedia.org/wiki/FIFO

It is a good thing that your processes are isolated and share-nothing, but you do need to architecture your application slightly differently to accommodate this.

Neil Middleton
  • 22,105
  • 18
  • 80
  • 134
  • 2
    Yep I could do a fifo queue in redis or something, but are you sure that's about processes being unable to communicate with each other? Eventmachine servers/clients can talk to each other over standard tcpip just like the web process accepts incoming tcpip requests. I just don't know what port to connect on. Thanks for the answer though - that 12factor page by Adam Wiggins is really interesting. – Brian Armstrong Mar 05 '12 at 19:06
  • 1
    Yes. Your web process is assigned a port to receive requests on from the outside world - there is no other inter process communication. See this similar question here: http://stackoverflow.com/questions/9322599/tcp-socket-communication-between-processes-on-heroku-worker-dyno/9453432#9453432 – Neil Middleton Mar 05 '12 at 22:34
  • 1
    Ok cool thanks, this was helpful. I ended up using a pub/sub model with Redis which worked. Although it's not durable, so I may try iron_mq after this and poll it from eventmachine instead. Thanks for the help! I really appreciate it. – Brian Armstrong Mar 16 '12 at 07:48
2

I'm reading this while on my commute to work. So I haven't tried anything with it (sorry) but this looks relevant and potentially awesome.

https://blog.heroku.com/archives/2013/5/2/new_dyno_networking_model

davnicwil
  • 28,487
  • 16
  • 107
  • 123
Nathan Lilienthal
  • 864
  • 1
  • 10
  • 16