An OPC UA subscription has two clocks, not one: the server samples a value at the sampling interval, and the subscription publishes whatever has queued up at the publishing interval. Nearly every "we are losing data" and "the client goes dead after a switch reboot" report traces back to a mismatch between those two clocks or to the keepalive and lifetime counters derived from them. The protocol is explicit about the arithmetic; most client code is not.
This guide covers the parts of a production OPC UA client that the quick-start examples skip, with the subscription API as published in the opcua crate 0.12.
Sampling interval, publishing interval and the queue between them
Three parameters interact, and the failure mode of each is different:

| Parameter | What it controls | Wrong value causes |
|---|---|---|
| Sampling interval | How often the server reads the underlying value | Data change missed between samples; server CPU load |
| Publishing interval | How often queued notifications are delivered | Latency; server load on many subscriptions |
| Queue size | How many samples are buffered between publishes | Silent loss when sampling is faster than publishing |
The relationship that matters: if sampling is faster than publishing, the queue fills. When it is full, the oldest notification is discarded — silently, by design, with no error to the client. A client configured with a 100 ms sampling interval, a 1000 ms publishing interval and a queue size of 1 therefore delivers one sample in ten and looks perfectly healthy.
The rule that avoids the trap: either set the queue size to at least ceil(publishing / sampling) so every sample survives to the publish, or deliberately set it to 1 and accept discarded intermediate samples as the intended behaviour. The dangerous configuration is a queue size between 2 and ceil(publishing/sampling) - 1, because it delivers some samples and drops others with no pattern the trend can reveal.
Creating the subscription
In opcua 0.12 the subscription is created on the session with the timing parameters as explicit arguments:
fn create_subscription<CB>(
&self,
publishing_interval: f64,
lifetime_count: u32,
max_keep_alive_count: u32,
max_notifications_per_publish: u32,
priority: u8,
publishing_enabled: bool,
callback: CB,
) -> Result<u32, StatusCode>
where CB: OnSubscriptionNotification + Send + Sync + 'static;
The three counters are not arbitrary and the specification fixes their relationship:
lifetime_countis how many publishing intervals may pass with no notification from the client before the server deletes the subscription. The specification requires it to be at least three timesmax_keep_alive_count.max_keep_alive_countis how many publishing intervals may pass with no data change before the server sends an empty keepalive message. The client must respond to keepalives to hold the subscription open.
A configuration that works well in practice on a plant network:
| Setting | Value | Reason |
|---|---|---|
| Publishing interval | 500 ms | Matches the fastest tag most operators actually look at |
| Lifetime count | 60 (30 s) | Survives a switch reboot of a few seconds |
| Max keepalive count | 20 (10 s) | Detects a dead server long before the subscription dies |
| Max notifications per publish | 0 | Unlimited in one publish, avoids fragmentation |
| Priority | 0 | Priority is not honoured consistently across servers |
Set lifetime_count to exactly 3 × max_keep_alive_count and you have the minimum the specification allows — which is also the tightest margin against a transient network stall. Doubling it costs nothing on the server and buys a much wider recovery window.
Why a reconnect is not a re-subscription
When the TCP connection drops, the session is gone. A naive client reconnects, creates a new session, creates a new subscription and re-adds every monitored item. That works, but it also means every client on the site re-subscribes at once after a shared network event, and the server sees a spike exactly when it is least healthy.
OPC UA has a purpose-built mechanism for this: the session can be reactivated rather than recreated, and subscriptions can be transferred to the new session. The sequence is:
- Reconnect the transport.
- Call
ActivateSessionwith the same session id and authentication token, before the session timeout expires. - Call
TransferSubscriptionswith the subscription ids andsend_initial_values = false. - Only if the server returns
BadSubscriptionIdInvalidfor an id do you create a new subscription and re-add the monitored items for that id.
send_initial_values = false matters: with true the server re-sends the current value of every monitored item, which looks like a burst of changes to the historian and creates a false step in every trend. Use true only when you genuinely need to re-establish state after a long outage.
The recovery window is governed by SessionTimeout on the server, not by the subscription lifetime. A session that times out cannot be reactivated at all, so a client that intends to survive a 60-second outage must request a session timeout longer than that, which in turn means the server keeps session state for every client for that long.
Certificates in a container
An OPC UA client with SecurityPolicy other than None needs a client certificate, and this is where containerised deployments fail most often:
- The application instance certificate has an expiry date. A certificate that expires at 03:00 on a Sunday takes the client down with a
BadCertificateExpiredthat most applications log at debug level. - The server must trust the client certificate. Trust is stored in the server, so adding a new client means a change on the server, not on the client — plan for it in deployment, not at 2 a.m.
- Certificate and private key must be readable by the container user. A mounted secret with mode
0600owned by root will fail a process running asnodewith a confusingBadSecurityChecksFailed.
The practical pattern is a startup check that reads the certificate, logs its subject and expiry, and refuses to start if it expires within the deployment window. Failing loudly at boot beats failing silently at 03:00.
# Startup guard: refuse to launch with a certificate that expires soon.
# Works with the PEM the crate itself writes, no Rust API needed.
END=$(openssl x509 -in /app/pki/own/cert.der -inform DER -noout -enddate | cut -d= -f2)
DAYS=$(( ( $(date -d "$END" +%s) - $(date +%s) ) / 86400 ))
[ "$DAYS" -lt 30 ] && { echo "OPC UA client cert expires in $DAYS days" >&2; exit 1; }
Deadband: the filter that decides your historian size
A monitored item can carry a data-change filter, so the server only sends a notification when the value moves by more than a threshold. This is the single most effective control over historian growth, and it is usually left at the default of zero.
| Deadband type | Unit | Use for |
|---|---|---|
| Absolute | Engineering units | Temperatures, pressures with a known noise band |
| Percent | % of EU range | Values whose range differs per tag |
| None | — | Counters, states, anything where every change matters |
Set the deadband just above the measurement noise, not just below the resolution you care about. A pressure transmitter with ±0.02 bar of noise and a 0.1 bar deadband sends nothing during steady state and still catches real transitions — roughly a 50 to 200 times reduction in stored points on a typical process tag.
What to verify before you call it done
A client is ready for production when you can answer these with a test, not an opinion:
- Kill the network for 20 seconds: does the session reactivate and the subscription transfer, with no gap in the historian other than the outage itself?
- Restart the OPC UA server: does the client reconnect and recreate subscriptions, and does it back off rather than reconnect-storm?
- Watch a value for an hour with the client's own timestamp and the server's timestamp displayed: is the difference stable, or growing?
- Publish a value faster than the publishing interval and confirm the queue behaviour matches what you intended rather than what you assumed.
A gateway built this way is still a read path. It is not a control path, and OPC UA subscriptions should not be used to close a control loop: publishing intervals are best-effort, and a queue that discards under load gives you no timing guarantee at all. Use it for supervisory data, and a deterministic fieldbus for anything that moves.



