[erlang-questions] Modelling question - concurrency and scalability

jm jeffm@REDACTED
Sat Aug 11 04:05:34 CEST 2007


Some random thoughts:

* Use multiple mnesia databases, if this proves implausible,
* write your own database with say 5-100 accounts per process (group)
and one DETS instance for each of these process (groups).


Then, construct an account name server (acct) which is responsible for
locating the account and provides interface functions for carrying out
the transaction.

The basic process may go something like this, very rough,


For {transaction, ID, Amount, DebitAccount, CreditAccount}
 DAPid = acct:find_account_pid(DebitAccount),
 CAPid = acct:find_account_pid(CreditAccount),
 acct:transaction(DAPid, DebitAccount, CAPid, CreditAccount, Amount).


-module(acct).

register_acct(Pid, Acct) ->
  %% puts account in an ETS table

unregister_acct(Pid, Acct) ->
  %% remove account from ETS table

find_acct(Acct) ->
  %% finds Pid in ETS table

transaction(TransFun, DAPid, DAcct, CAPid, CAcct, Amount) ->
  %% here be dragons - race condition exist
  %% this is the key function suggest looking at Mnesia code
  DAOldState = get_state(DAPid, DAcct),
  CAOldState = get_state(CAPid, CAPid),
  try
    debit(DAPid, DAcct, Amount),
    credit(CAPid, CAcct, Amount)
    ok
  catch
    %% unwind/rollback on failure
    restore_state(DAPid, DAcct, Amount)
    restore_state(CAPid, CAcct, Amount)
    fail
  end.


start() ->
   % starts server process by calling init


stop() ->
   % stops server process


init(Args) ->
   % real


I was flipping through "Programming Erlang" as I remember seeing
something on transaction in chapter 12 that deals with OTP Genric Server
and found example server 2 on page 297 which may be of interest.


Lastly, I'd record the steps between account balances so that you have
an audit trail, eg

{account, ID, Debit, Credit}

would be come

{account, ID, Debit, Credit, [Transactions]}

at least logically. Though it would be more like a journal in the sense
of a file system, not an accounting journal. Thinking about it modeling
each account as a journal may reduce or elimiate the need for cross
process locking.

As I said in the open random thoughts. Let me know how you go as I'm
interested to know the solution.

Jeff.





More information about the erlang-questions mailing list