Polling vs. Subscription

Communication between a server and a client can be either Polling or Subscription based. Polling is typically a periodical “client request and server response” flow, while a Subscription does not involve a repeated action on the client side.

Many parameters exist for optimizing the data flow of both approaches. Such details can be quite complex and depend on the actual implementation of Polling or Subscription in the particular protocol, meaning while the general concept of a subscription is the same, the details of the configuration and behavior of a subscription is quite different between OPC UA and MQTT for example.

Note: While both approaches can technically be used for other purposes, this document focuses on acquiring the most current value from a server.

General Data Flow Principles

Simple Polling

When a client actively makes a request for data at a server, and the dataflow ends with the response of the server, we call this Polling. Each time the client wants more data, it must send a request again, and the server will respond to that request independently.

Polling is typically done on a fixed frequency, like once per five seconds. This implies that the client is responsible for acting according to the given frequency, and the server only responds when being asked for data.

Analogy: Jupp regularly wants to get the most current newspaper. Every Friday Jupp goes to the shop and buys the most current newspaper.

title

While the data flow ends with each response of the server, this does not necessarily mean that the established connection between the client and the server is interrupted between two requests. Many implementations of Polling mechanisms keep the connection alive to save on the overhead it takes to establish a new connection each time when requesting data.

Simple Subscription

A Subscription uses a very different data flow compared to Polling. When a client subscribes to data, it essentially announces its interest for that data to the server and then waits for the server to send data when it changes.

In this data flow the client acts only once and then it is the server’s task to repeatedly send a reply. This reduces the communication overhead a lot compared to the Polling case.

Analogy: Jupp regularly wants to get the most current newspaper. He goes to the shop and orders a regular delivery of the newspaper.
Jupp receives the newest issue automatically at home, when it is published.

title

A subscription is characterized by communication over the initially established connection between the client and the server. While there are protocols that support recovering from connection outages, the most typical way of dealing with this is for the client to create a new subscription once the connection to the server is possible again. The server will not do this by itself.

More Details on Polling

Impact of Rate of Change

When a client is polling for data on a given frequency, this is completely independent of the data having changed in-between two polling requests. The data flow communication is always the same, without knowledge of earlier polls. The difference between the rate of change of the data and the polling frequency has a significant impact on what data the client receives.

If the data changes more often than the client polls for it, the client is missing the changes between two polls.

Analogy: Every Friday Jupp goes to the shop to buy the most current newspaper. The newspaper is published also on Wednesdays, therefore Jupp misses one issue before he gets another one.

title

On the other hand, if the data changes less often than the client polls, the client receives the same data again.

Analogy: Every Monday Jupp goes to the shop to buy the most current football magazine. The magazine is published monthly; therefore, he buys the same magazine 4 times.

title

Obviously, a human would not do that, because he would know that he had already bought that magazine. The shop doesn’t bother if Jupp buys the same magazine 4 times. The same happens between the client and the server. With every poll the client gets the same data if it did not change, and what happens with that “same data” is the client’s responsibility.

There are cases where clients will throw away such “duplicate” data, but there are also use cases where keeping the data is beneficial, for example when data analysis tools required equidistant data distributions.

Timestamp of Data

When a client polls for current data, it is the preferred approach that the server does not only return the data itself, but also the information when that data occurred, also known as the timestamp of the data.

If the server provides the timestamp, the client can unambiguously identify that it is actually reading the exact same data if value and timestamp are the same. Many protocols that only support polling do not provide the timestamp. In this case reading the same value can also mean that the data has changed between two polls and returned to the same value. Also, the client will usually add the timestamp to data itself in such a case.

Further Observations

Server Side Caching of Data

To optimize the response to clients some servers cache the most current value, so they do not have to read it on each poll from the real (physical) source. In situations where the server cache is less frequently updated than being polled, the result can be a discrepancy between the response to the client and the actual (physical) value in the source.

Some protocols provide means to send with the polling request additional information enforcing a read from the actual source, to have guaranteed most current value. Using this option when polling very frequently can cause performance stress on the server and/or the physical source.

Synchronous or Asynchronous Communication

Synchronous communication for polling means the clients program code will wait for the server to respond to the request before continuing its execution. Polling data with a request/response data flow might sound like a synchronous program activity at first, but whether this is a synchronous or asynchronous communication depends on the implementation of the server. Also in the asynchronous communication for each request there is always one response.

Interfaces Using Polling

  • Modbus

  • PLC

  • OPC DA (uncommon)

  • HTTP

More Details on Subscription

Impact of the Update Rate vs Rate Of Change on a Subscription

Opposed to the Polling data flow, in the Subscription case the server needs to know when to send data. This information is often called Update Rate and is usually given to the server with a subscription request.

Update Rate is the amount of time that has to pass by before a client wants to be informed about a data change. If data changes more quickly, which means multiple data changes happen within the Update Rate time period, only the most current change is sent, while other data is discarded.

Analogy: Jupp wants to get the most current newspaper every week. On a Monday he goes to the shop and orders a delivery of the newspaper every 7 days.
He receives the newest issue automatically at home on Mondays, but he misses the issues published on the other weekdays.

title

If there is at least one change during the time of one complete Update Rate, this will be sent to the client when the Update Rate time ends. If data does not change during the time of an Update Rate, there is no change notification to the client.

Analogy: Jupp wants to get his favorite football magazine regularly, but not more often than once per week. On Monday the 1st he goes to the shop and orders a delivery of the magazine accordingly.

  1. The monthly issue of the magazine is published each last Friday of the month and is delivered to Jupp on the Monday after its publication.

  2. In addition, there is a special edition of the magazine published on Wednesday of week 2 of the month, that Jupp receives the Monday after.

  3. Another special edition is published on the Wednesday of the last week of the month. Jupp will not receive this one, because the regular issue coming out on the last Friday of the month and this will be delivered to him instead.

title

Because there is only data sent to the client when at least one change occurs during the time period of the Update Rate, this is the most efficient data flow with the least overhead between the client and the server. The Update Rate must be chosen by the client such that more frequent data changes being discarded is acceptable.

Timestamp of Data

In the Subscription case the server is responsible for sending a timestamp with the data. This is important because when the server sends the data to the client its timestamp is usually older, especially if the update rate is a long time span compared to the data change rate.

Further Observations

Server Side Queue

To optimize the response to clients some subscription protocols cache multiple data changes in a queue before they send a batch of changes to the client. This way communication from server to client happens on the defined update rate but can transport more than only the most recent data change. How often data batches are sent, what the size of the queue on the server side is, can usually be configured. Selecting different parameters allows to adjust how often data flows from client to server and how large the data messages can become.

Interfaces Using Subscription

  • OPC DA (common)

  • OPC A&E

  • OPC UA

  • MQTT

  • Kafka

  • WebSockets