Persist trading state to Postgres with boot rehydrate#166
Merged
Conversation
The edge held all orders, positions, fills, balances, the public trade tape, and funding history in memory only, so a restart wiped every user's state and emptied the book. Back those stores with Postgres. Reads stay on the hot in-memory path. Each mutator emits a PersistEvent onto an unbounded channel; a single background writer task drains it and applies the SQL in order, so the sync lock-guarded mutators never block on async I/O. On boot the edge loads a full snapshot from Postgres, repopulates the maps, and rebuilds each market's orderbook from the rehydrated resting orders. Derived/ephemeral state is intentionally not persisted: the orderbook is rebuilt from open_orders, index prices come from the live oracle, and SIWE nonces expire in seconds. The binary now requires DATABASE_URL (bundled Postgres already ships in docker-compose). Unit tests are unaffected — AppState::new stays a pure in-memory path with no channel.
dde4f76 to
d2694f9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Backs the edge's in-memory stores (orders, positions, fills, vault balances, public trade tape, funding history) with Postgres so a restart rehydrates instead of starting empty.
How
PersistEvent; one background writer task drains it and runs the SQL in order. Keeps the sync, lock-guarded mutators off async I/O.crates/perplex-edge/migrations/0001_init.sql, applied idempotently on connect.Derived/ephemeral state is deliberately not persisted: the orderbook is rebuilt from
open_orders, index prices come from the live oracle, SIWE nonces expire in seconds.The binary now requires
DATABASE_URL(the bundled Postgres already ships indocker-compose.yml). Unit tests are untouched —AppState::newstays a pure in-memory path with no channel.Test
Notes