Persistent multi-node events in a stateless web application in PHP

I am creating an OO PHP application that will run through several nodes and be relatively inactive in nature, and I need to implement a proper publisher subscriber ( http://en.wikipedia.org/wiki/Observer_pattern / http://sourcemaking.com/ design_patterns / Observer / php ) style events.

My question is: how can I handle events?

In my application, we use technologies such as Cassandra, Redis, Mongo, and RabbitMQ.

I know that PHP has an EXTENSION event, but from what I can say, it is in a state - or if something like memcached is involved, it can be used in this node ... but my application will be distributed across several nodes .

So, let's look at an example: In Node 1, the metric is updated (metric identifier 37), and everything that subscribes to this indicator needs to be updated. This publishes the change and the change as it makes the update.

I have something that subscribed to the updated Metric ID 37, for example Metric 38, you may need to recount when the Metric 37 value changes.

Metric 38 is currently created and used in Node 2 in process ID 1011 ... How does Metric 37 tell Metric 38 on Node 2 (process ID 1011 in this case) to run a signed function?

Metric 39 subscribes to Metric 38, which is updated but not created anywhere ... How does Metric 39 update when Metric 38 completes the update?

I was thinking of something like using RabbitMQ as an event queue manager, and on every Node there is an event-consumer application like a daemon that reads events in an event queue (for load balancing / work distribution).

Then the consumer sees "Metric: 38: Updated", he checks something like Redis for everything that is subscribed to "Metric: 38: Updated" and gets the value ("What: Function: Values") and does something like call_user_func_array (array ($, that, function $), $ values); .... but it looks like it can cause excessive load and some synchronization problems ...

I use Doctrine MongoDB ODM to save my objects ... To deal with synchronization issues, I thought of something like this: Objects can have a version number ... (version = 1.0) And redis can be used to quickly link to the latest version object (ObjectVersion: ObjectType: ObjectId) = 1.1 And when the getter is called on the property of the object, which is marked as @critical (such as isDeleted, cash balances, etc.), It can check if the version identifier of version # is equal to redis and update its values ​​from mongo if necessary to ...

An alternative setup uses amphp/amp ( http://amphp.org/docs/amp/reactor-concepts.html ) and some form of RPC to synchronize nodes

Since I'm pretty new to web development (moving from C #) and stateless and spread. I thought it would be nice to ask the community if anyone has any better suggestions?

+5
source share
2 answers

My question is: how can I handle events?

If you want to use the event loop implementation, there are several options available:

You can use a PubSub system like Redis offers: http://redis.io/topics/pubsub . Amp offers a package for Redis , other event libraries may already have an available implementation.

Redis will send event notifications to all connected and listening clients. You may not need this because you want to synchronize your calculations and perform them only once.

You can transfer the actual data to the Redis list and use the event system only for polling in the case of a new task, so that employees can sleep at the same time. A better solution would be to use block list operations that block a Redis connection until new data appears in the Redis list. When this event occurs, you can recalculate the value and click update on the event.

This is basically creating a message queue with Redis, but essentially you just need to take a look at the functions of the various message queue implementations and see if they suit your needs. If you want to use any of the event loop libraries, you can also view the available clients and other functions that you need, because they are generally incompatible (for now).

+1
source

you may need middleware, for example http://redis.io/topics/pubsub or some other similar message queue might support your application

0
source

Source: https://habr.com/ru/post/1213433/


All Articles