Always retain last message for each subject in a stream
Proposed change
Add a --keep-last-msg-per-subject flag to the nats server configuration i.e. nats stream add --keep-last-msg-per-subject <more-options> <stream> that works with all limits-based message retention policies (i.e time-based, size-based, count-based).
Use case
Stream limits options (e.g. max-age, max-bytes, max-msgs etc.) mostly seem to operate on a per stream basis. The exception is the max-msgs-per-subject which allows for a per subject based limit.
It would be nice to have the capability to always retain the last message on all given subjects in a stream while setting a time-based message retention policy i.e. delete all messages past an age of 10 mins except the last.
A use case of this can be as follows:
- We want to record changes to a configuration (stored in a nats subject) and keep a history for a certain period of time e.g. 5 days (specified by the --max-age flag)
- We always want to be able to fetch the last configuration to initialize our long-running application (which may run for weeks on end) whenever it restarts
Contribution
Yes, I can contribute to this feature
Source: nats-io/nats-server