Skip to content

Subscriptions

Charles d'Avernas edited this page Nov 30, 2026 · 1 revision

A subscription tells a broker which cloud events to deliver, where, and how. This page covers what a subscription selects and from where it starts. Delivery covers how its events are delivered, retried and acknowledged.

Anatomy of a subscription

apiVersion: cloud-streams.io/v1
kind: Subscription
metadata:
  name: billing
  namespace: orders
spec:
  partition:                    # optional: only read one partition, e.g. the events of a source
    type: by-source
    id: https://orders.example.com
  filter:                       # optional: only deliver the events that match (see Filters below)
    type: sql
    sql: type LIKE 'order.%'
  mutation:                     # optional: transform each event before delivering it
    type: expression
    expression: '${ .data |= del(.creditCard) }'
  stream:
    offset: 0                   # optional: where to start, and track an offset (see Where a subscription starts below)
  subscriber:
    endpoint: https://billing.example.com/events   # or a sink, or a Kubernetes object (see Transports and Knative)
  deadLetter: {}                # optional: set failed events aside rather than faulting (see Delivery)
  suspended: false
  • Partition: a subscription reads the stream of its namespace, or one of its partitions. Reading a partition is cheaper than filtering the whole stream, when a partition holds the events to deliver.
  • Filter: narrows the events down further, with CloudEvents SQL, attribute patterns or a jq condition (see Filters).
  • Mutation: transforms each event before delivery, with a jq expression or a webhook (see Mutations).
  • Subscriber: where the events go: an endpoint, over HTTP or another transport, a sink that holds an endpoint and its credentials, or an addressable Kubernetes object, such as a Knative Service.

Where a subscription starts

spec.stream decides where a subscription starts, and whether it tracks an offset:

spec.stream Behaviour
offset: 0 Delivers every event from the start of the stream or partition, then the new ones
offset: n Delivers from offset n
offset: -1 Delivers the events published from now on
absent Delivers the events published while the subscription is active, and tracks no offset

A subscription that tracks an offset commits it to status.stream.ackedOffset as events are delivered. It resumes from it after a restart, catches up with what it missed while its subscriber was down, and can be replayed. One that tracks no offset never catches up: an event it fails to deliver is dropped, unless it has a dead-letter policy.

Mutations

A mutation transforms each event before it's delivered, e.g. to remove sensitive data, or to adapt events to a subscriber that expects another shape. The stored event is never changed.

spec:
  mutation:
    type: expression            # a jq expression, applied to the whole event
    expression: '${ . + { "region": "eu" } }'
  # mutation:
  #   type: webhook             # a service that receives the event and responds with the mutated one
  #   webhook:
  #     serviceUri: https://mutator.example.com/mutate

A mutation is applied at every delivery attempt: a webhook's failures are retried as the subscriber's are, within the same timeout.

Filters

A subscription delivers every event of its stream or partition, unless its spec.filter narrows them down. Filters come in three types:

spec:
  filter:
    type: sql                 # CloudEvents SQL, as Knative triggers and the CloudEvents Subscriptions API use
    sql: type LIKE 'order.%' AND (NOT EXISTS region OR region IN ('eu', 'uk'))
  # filter:
  #   type: attributes        # regular expressions, or runtime expressions, that the attributes must all match
  #   attributes: { type: '^order\.', subject: '${ . != null }' }
  # filter:
  #   type: expression        # a jq condition on the whole record, data included
  #   expression: '${ .data.total > 100 }'

CloudEvents SQL implements version 1.0 of the CloudEvents SQL Expression Language, and passes its conformance tests:

  • Language: context attributes and extensions by name, AND, OR, XOR and NOT, comparisons, LIKE with %, _ and \ escapes, IN, EXISTS, integer arithmetic, and the functions LENGTH, CONCAT, CONCAT_WS, LOWER, UPPER, TRIM, LEFT, RIGHT, SUBSTRING, ABS, INT, BOOL and STRING. Keywords and function names are case-insensitive. Values are cast implicitly as the specification says, e.g. sequence = 10 compares a string extension as an integer.
  • Errors: an event only passes the filter if the expression evaluates to true without any error. Addressing an attribute the event doesn't have is an error, so check optional attributes with EXISTS first: subject = 'a' fails for an event without a subject, while NOT EXISTS subject OR subject = 'a' passes it. The playground shows each event's errors.
  • Precedence: AND, OR and XOR share the same precedence, and group to the right, as in the specification's grammar and the Go and Java SDKs: a OR b AND c reads a OR (b AND c). Use parentheses when mixing them.
  • Admission: expressions are parsed when the subscription is admitted. One that doesn't parse, calls a function that doesn't exist, or can't evaluate to a boolean is refused, with the position of the error.
  • Performance: expressions are parsed once, and LIKE patterns are matched without backtracking, in linear time.
  • Data: like the specification, CloudEvents SQL filters don't read the event's data. Use a jq expression to filter on it.

Replay

Operators replay a subscription with POST /api/resources/v1/namespaces/{namespace}/subscriptions/{name}/replay. The subscription delivers every matching event again, from the start of the stream, or from a point in time:

POST /api/resources/v1/namespaces/{namespace}/subscriptions/{name}/replay?from=2026-11-25T10:00:00Z
  • What the time resolves to: the first event recorded at or after it, in the stream or partition the subscription consumes. If there's none, it resolves to the next event to be published.
  • Which time is used: the time at which the gateway recorded the event, which never decreases along the stream, rather than the event's time attribute, which its publisher sets.
  • Reading the stream: reads accept time instead of offset too. Reading forwards starts at the first event recorded at or after it, and reading backwards at the last one recorded before it. Records carry their timestamp.

Suspension

A subscription can be suspended by setting spec.suspended to true, for example while its subscriber is under maintenance. While it is suspended:

  • it keeps its broker, and its phase is suspended;
  • nothing is delivered;
  • changing its offset takes effect when it is resumed.

The offset of whatever has been delivered is committed when it is suspended. Once spec.suspended is set back to false, delivery resumes from that offset, and the events published in the meantime are caught up. A subscription that does not track a stream offset misses them.

Operators suspend and resume subscriptions from the dashboard, or with:

POST /api/resources/v1/namespaces/{namespace}/subscriptions/{name}/suspend
POST /api/resources/v1/namespaces/{namespace}/subscriptions/{name}/resume     # also clears the fault of a faulted subscription
POST /api/resources/v1/namespaces/{namespace}/subscriptions/{name}/skip?offset=42   # skips the event that faulted it

Clone this wiki locally