Architectural comparison of queries and monitors
In some ways, an Apama query is similar to an Apama monitor. Each operates as a self-contained event processing agent that communicates with other monitors and queries by sending and receiving events.
Note:
While Apama queries and Apama stream queries use similar terminology, they are different constructs. Apama queries can communicate with monitors, but Apama queries are not contained in monitors. Whereas Apama stream queries are defined and operate inside monitors.
One difference between a monitor and a query is the programming model for scaling. With monitors, the approach is procedural. A spawn statement is used to create new monitor instances. Typically, for each real-world entity, a separate monitor instance is used to handle the events relating to that entity. The developer has full control over what data is held where as well as the design of the solution architecture. With queries, the approach is declarative. A key is defined which is used to identify how the events are partitioned such that events from each real-world entity are handled separately. Also, queries can use a distributed Apama MemoryStore to share historical data between correlators. This allows query deployments to scale across several hosts, make the same data available to multiple correlators and provide availability should a correlator fail or be taken down for maintenance.
Another difference between monitors and queries is the way in which they handle the state, or event history. With monitors, each monitor instance holds the state, or event history, needed for its continuing processing. This state is held in memory, which allows high-performance processing over complex state. With queries, the only state is the event history, which is held separately from the query. The query is effectively stateless, which allows queries to easily scale across correlators.
Typically, a monitor instance operates on events that relate to a particular real-world entity. To operate on events related to another entity in the same set, the monitor typically spawns another instance. In contrast, the definition of a query specifies how to partition incoming events so that each set of events that relates to a particular real-world entity is in its own partition. The query operates on the events in each partition independently of every other partition.
The following table compares monitor variables with query parameters:
Monitor variables | Query parameters |
Can store any complex state that the monitor requires. | Must be one of the following types: boolean, decimal, float, integer, string. |
Can be updated by the monitor. | Can only be read by the query. |
Are private to that monitor instance. | Are controlled by Scenario Service clients. |
A monitor can subscribe to a channel to receive all events sent on that channel. A query cannot subscribe to a channel. However, running Apama queries automatically receive all events sent on the com.apama.queries channel as well as all events sent on the default channel. For example, monitors, adapters, and the engine_send utility can send events to the com.apama.queries channel.
Both monitors and queries can send events to a channel. In both monitors and queries, the send statement sends events to only those components that are connected to that correlator. For both monitors and queries, sending events to other correlators in the cluster requires connections created by the engine_connect utility or the use of Universal Messaging to connect the correlators to the same set of Universal Messaging channels.
In general, monitors follow a more imperative pattern while queries have more declarative clauses. For example, a monitor can use conditional if ... else statements to determine whether there is a match that triggers some processing. A query specifies where, within, and/or without clauses to define filters, time constraints, and exclusions, respectively, right in the event pattern. In general, this allows queries to be simpler than monitors.