Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Support reading a stream backwards #47

Copy link
Copy link
@nordfjord

Description

@nordfjord
Issue body actions

Some entities have events that could be considered reset events. That is, after these events are appended the prior history ceases to matter in regards to the current state of the entity. TodoListCleared is an example of this.

It could be extremely useful to read a stream backwards in these cases as it can minimize roundtrips to the database.

I propose we add a function that allows reading a stream backwards from a position where a position of -1 indicates the end of the stream. Messages should be returned in descending position order.

Imagine a stream with 2100 events. In order to reconstitute its events we'd need three calls (given a batch size of 1000)

-- read 1000 messages backwards from end
select * from get_stream_messages_backwards('Stream-123', -1, 1000)
-- positions are inclusive so we read from (1100 - 1)
select * from get_stream_messages_backwards('Stream-123', 1099)
select * from get_stream_messages_backwards('Stream-123', 98)

An example query:

SELECT * -- replace with actual columns
FROM messages
WHERE stream_name = $1
  AND position <= $2
ORDER BY position DESC
LIMIT $3;

Reasons to avoid this:

  • Streams should short by nature, and message-db should not encourage technical solutions to modeling problems
  • Reading backwards might have worse performance characteristics due to BTree indices being optimized for sequential forward reads
natalie-o-perret

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.