You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Should the v2 typestates store events in memory along side persistance? #1197
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.
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:
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).
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.
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.
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:
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).
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.
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
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.
Activity
nothingmuch commentedon Dec 12, 2025
event data is immutable so caching it in memory wouldn't be an issue IMO
DanGould commentedon May 4, 2026
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.
NoopSessionPersistercurrently 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:
NoopSessionPersisterbehind_test-utils, or remove it entirely (my preference, the impl is ~15 lines — anyone who actually wants discard semantics can write it).InMemoryTestPersisterto a publicInMemoryPersister. It's a legit persister, events are saved, the log is replayable, just not durable across processes. That's what examples & doctests want.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.
arminsabouri commentedon May 4, 2026
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
DanGould commentedon May 4, 2026
Seems that in-memory cache may be able to be done without breaking the existing v2 API just by having a more constrained ~
CachedPersistertrait that is aPersisterbut has the properapplysemantics 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.
spacebear21 commentedon May 22, 2026
relevant experiment: https://github.com/evanlinjin/rust-payjoin/pull/1/changes#diff-c1603ee6a674bb75255aa448057630506153e363297fd58668e6679f10909bc6