top | item 47219653

(no title)

linkdd | 16 hours ago

You would have a process handling the calls to the postgres.

That process has as local state the database connection and receive messages that are translated to SQL queries, here 2 scenarios are possible:

1) The query is invalid (you are trying to inert a row with a missing foreign key, or wrong data type). In that case, you send the error back to the caller.

2) There is a network problem between your application and the database (might be temporary).

You just let the process crash (local state is lost), the supervisor restarts it, the restarted process tries to connect back to the database (new local state). If it still fails it will crash again and the supervisor might decide to notify other parts of the application of the problem. If the network issue was temporary, the restart succeeds.

Before crashing, you notified the caller that there was a problem and he should retry.

Now, for the caller. You could start a transient process in a dynamic supervisor for every query. That would handle the retry mechanism. The "querier process" would quit only on success and send the result back as a message. When receiving an error, it would crash and then be restarted by the supervisor for the retry.

There are plenty of other solutions, and in Elixir you have "ecto" that handles all of this for you. "ecto" is not an ORM, but rather a data-mapper: https://github.com/elixir-ecto/ecto

discuss

order

No comments yet.