Skip to content

Allow publishers to specify a different subject for "Nats-Expected-Last-Subject-Sequence" #5280

@cchamplin

Description

@cchamplin

Proposed change

I'd like to request a change to allow publishers to provide a specific subject for use when nats-server enforced "Nats-Expected-Last-Subject-Sequence". Adding an additional header field that allows publishers to optionally change the subject used for "Nats-Expected-Last-Subject-Sequence" from the subject of the message being published to to a subject provided in the header.

The proposal is for a "Nats-Expected-Last-Subject-Sequence-Subject" header that when set with a string value becomes the subject used during enforcement of the last sequence. The header does not necessarily need to be that string exactly, the functionality is the actual proposal.

Use case

The specific use case I am trying to solve here is to allow publishing to complex and dynamic subjects while still enabling optimistic concurrency control.

For example in an event sourcing application an aggregate might be defined against the subject:

  • orders.[order_id]
    • orders.1
    • orders.2
    • orders.3
    • ...

Today we can use "Nats-Expected-Last-Subject-Sequence" to ensure that when we publish to orders.2 we are publishing against the known last sequence for messages on the orders.2 subject.

However, it is often desirable to have more complex subject patterns. For example image a pattern that looks like this:

  • orders.[order_id].[event_type].[visibility]
    • orders.1.created.public
    • orders.1.approved.private
    • orders.2.created.public
    • ...

Today we cannot use "Nats-Expected-Last-Subject-Sequence" with this pattern and still have OCC for the aggregate orders.2 when publishing. If a publisher attempts to set the expected sequence and then publish to order.2.rejected.private the sequence for that subject is likely 0 as opposed to sequence for the last event published to order.2.>

By allowing publishers to override and customize the subject used in the expect last subject sequence check OCC can be enforced for the aggregate itself versus the subject being published to. For example by setting the proposed "Nats-Expected-Last-Subject-Sequence-Subject" to "order.2.>"

Relevant Slack Conversations:

Contribution

Possibly

Metadata

Metadata

Assignees

Labels

proposalEnhancement idea or proposal

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions