Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is redis pub/sub realistic in php?

I'd like to use redis pub/sub in PHP, but I'm afraid PHP can't be the only tool: a subscriber need to be always callable, since php isn't built for running as a daemon, I can't trust it to reliably be always "on".

So what is the solution for the PHP world?

  1. don't use pub/sub, use other redis' storages with a crontask launching php every x minutes
  2. use a broker which will call php?
  3. other?

With the "2." I mean : use a nodejs/java/fooBar server which is the daemonized subscriber and call back the php (using http/cli or whatever).

I can't find a better idea than the "2." , but it seem so ineffective at the same way...

What is your opinion?


EDIT : How would you do this using a cloud platform like platform.sh which do not give the opportunity to have a supervisor.d alike?

like image 613
Bruno Avatar asked Nov 09 '22 04:11

Bruno


1 Answers

Thanks to the comments, I found a satisfying way to go : use of supervisor.d which will relaunch a symfony Command script with :

  • set_time_limit(0)
  • an infinite loop
  • a blocking call to redis (a BRPOP with a max way of 1 sec. lower than the read_write_timeout)
    • it is important to do a blocking command, in order to not consume all the CPU time
    • I would event go to a real pub/sub, but for now, I only have one listener so it don't matter

what I can tell from an early point of view :

  • supervisor.d is really easy to install/configure, the doc is complete, I didn't run in any problem, it's very rare + satisfying!
  • it seems to works well
  • logs are written, so it may be more easy to understand futur crashes
  • in case of X successive and near crashes, the service is stopped, I didn't find a way to be notified of this, it is really a problem, I think I'll go to this solution(doc)
  • like @Mike Miller said : "Only thing to remember of you go that way is you need to restart after a code change or it will keep running your old code"
like image 75
Bruno Avatar answered Nov 15 '22 12:11

Bruno