Save state for handoff in postgres

I’m using horde for distributed supervision and registry. My current challenge is to implement state handoff. I want to avoid any external systems like redis and use the tools I already have: either the BEAM or Postgres.

I’m thinking to use postgres like a KV store and store the state as binary. https://medium.com/@kaisersly/storing-any-elixir-data-in-postgres-e8f93367f473

Is this a crazy idea, or I should use json instead of binary? Or not use Postgres at all?

Erleans, https://github.com/erleans/erleans, also supports Postgres for state storage for stateful grains.

I use binary term for this in the 2 provided state backends:

But the providers themselves decide whether to use a binary term or something else like json (or protobuf, etc). There certainly could be advantages to json, like if you for some reason wanted to query the state of grains (your processes) in the postgres table, but I think term_to_binary suits most needs.

The only issue with term_to_binary is technically you could run into problems when moving between versions of OTP. And definitely would if records were used (but since you are in Elixir you don’t have that worry). Which is why Orleans suggests JSON https://dotnet.github.io/orleans/Documentation/grains/grain_persistence/index.html#recommendations

5 Likes

Thanks. Probably not going to use Erleans (not sure if you were implying I should use it :slight_smile: ), but glad that the concept of using Postgres for state is used somewhere.

I’ll start with something simple, without any extra libraries, and see how it goes.

Oh yea, I just thought prior art could be of help :slight_smile:

Having a Postgres table with just id and metadata field (jsonb Postgres type for maps) worked quite fine for me in the past.

1 Like