Skip to content

Should the v2 typestates store events in memory along side persistance? #1197

Closed as not planned

Description

@arminsabouri

Which crate is this feature request for (if any)?

payjoin

Question

Session history is stateful data structure that informs the application of things that happened during the payjoin session. The only way to instanitate session history is to replay the event log (database reads); however, if we keep track of the events in memory as well, applications can access their session history without replaying. I think this can be more ergonomic and save on unnecessary DB reads.
Otoh storing the same data in the same order in two places sounds like it could lead to edge cases.
Lastly, if there are any prod applications that use a noop persister they cannot access the session history as a result they cannot access their session status. If we store events in memory they can.

Opening this ticket as an open question.

Activity

  1. nothingmuch commented on Dec 12, 2025

    @nothingmuch
    Contributor

    Otoh storing the same data in the same order in two places sounds like it could lead to edge cases.

    event data is immutable so caching it in memory wouldn't be an issue IMO

  2. DanGould commented on May 4, 2026

    @DanGould
    Member

    After thinking about this a bit in relation to #1510, I'd push back on adding the in-memory cache and propose tightening the persister contract instead. Taking the three motivations in turn:

    Perf (saving DB reads): I doubt we're gonna need it. . Sessions are short and event counts bounded. Wait until we hit a performance issue / profiling says it's a problem.

    Dual-storage edge cases: Real risk but manageable with a strict "one apply function, persister-first then memory" discipline (same pattern Postgres uses apparently) but that's complexity to take on purely for an unprofiled win.

    Noop persister can't access status: This is the strongest motivation in the OP, but it's arguing for a feature to support a use case that, given persistence is actually required for v2 sessions, shouldn't be supported in the first place. NoopSessionPersister currently compiles cleanly but produces a session that can't be resumed and (per its own doc) "cannot be used to replay a session." That's a footgun.

    Alternative proposal:

    1. Move NoopSessionPersister behind _test-utils, or remove it entirely (my preference, the impl is ~15 lines — anyone who actually wants discard semantics can write it).
    2. Promote InMemoryTestPersister to a public InMemoryPersister. It's a legit persister, events are saved, the log is replayable, just not durable across processes. That's what examples & doctests want.
    3. Examples switch to InMemoryPersister, so the migration cost of removing Noop stays small.

    Net: the noop-status motivation goes away by construction, the dual-storage complexity stays out of the typestates, and the perf question can be revisited if/when profiling justifies it.

  3. arminsabouri commented on May 4, 2026

    @arminsabouri
    ContributorAuthor

    After thinking about this a bit in relation to #1510, I'd push back on adding the in-memory cache and propose tightening the persister contract instead. Taking the three motivations in turn:

    Perf (saving DB reads): I doubt we're gonna need it. . Sessions are short and event counts bounded. Wait until we hit a performance issue / profiling says it's a problem.

    Dual-storage edge cases: Real risk but manageable with a strict "one apply function, persister-first then memory" discipline (same pattern Postgres uses apparently) but that's complexity to take on purely for an unprofiled win.

    Noop persister can't access status: This is the strongest motivation in the OP, but it's arguing for a feature to support a use case that, given persistence is actually required for v2 sessions, shouldn't be supported in the first place. NoopSessionPersister currently compiles cleanly but produces a session that can't be resumed and (per its own doc) "cannot be used to replay a session." That's a footgun.

    Alternative proposal:

    1. Move NoopSessionPersister behind _test-utils, or remove it entirely (my preference, the impl is ~15 lines — anyone who actually wants discard semantics can write it).
    2. Promote InMemoryTestPersister to a public InMemoryPersister. It's a legit persister, events are saved, the log is replayable, just not durable across processes. That's what examples & doctests want.
    3. Examples switch to InMemoryPersister, so the migration cost of removing Noop stays small.

    Net: the noop-status motivation goes away by construction, the dual-storage complexity stays out of the typestates, and the perf question can be revisited if/when profiling justifies it.

    I concur with this general assesment. The only other usecase I can think of is you need access to prior events in the current typestate and its too much boilerplate to pass everything from typestate to typestate -- this may be more pressing in the multiparty settings. But storing events in mem is a solution and perhaps not the right one as it removes some of the nice typesaftey features we have from out typestate paradigm. We will come back to that problem when we actually have it

  4. DanGould commented on May 4, 2026

    @DanGould
    Member

    Seems that in-memory cache may be able to be done without breaking the existing v2 API just by having a more constrained ~CachedPersister trait that is a Persister but has the proper apply semantics to do memory alongside long-term persisted storage. So I'm closing this as not planned (for now) and will gladly re-open it as soon as we bump into an issue from either performance or multiparty boilerplate, or if someone bonks me that this is the wrong decision.

    In the meantime I'm going to move the alternative proposal to a new issue to slide into to v1 so that we get rid of the Noop footgun.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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