BankAccount - good fit for akka persistence?

190 views
Skip to first unread message

Jan Vincent Liwanag

unread,
Mar 23, 2014, 7:38:02 AM3/23/14
to akka...@googlegroups.com

Consider an actor ‘BankAccount’ which receives messages:

  • Deposit(amount, branchName, time)
  • Withdraw(amount, branchName, time)
  • GetBalance

Within the actor, I only keep the balance as part of the state. The branchName (which is the branch of the bank) and time (time when the request was made) does not affect the actor’s state since it does not affect the balance.

I want to save all Deposit and Withdraw transactions to a SQL database — the amount, branchName, time as well as whether or not the transaction was successful or not. (Withdraw may fail if there’s not enough balance.) I do not need to save GetBalance requests.

Every end of day, I want to save the user’s balance to a different table on the same SQL database.

Given this, is it a proper fit for akka persistence? I’m thinking I can do event sourcing. I will need to write a custom journal and snapshot plugin however since I want it to write to a SQL database with a predefined schema. The journal and snapshot plugins however are not generic — that is, they can only handle specific messages / state — in this case Deposit and Withdraw messages and balance as a state. Is this acceptable?


Jan Vincent Liwanag

Patrik Nordwall

unread,
Mar 24, 2014, 3:42:47 AM3/24/14
to akka...@googlegroups.com
Hi Jan,

Yes, that would be possible. Real bank account transactions are always event sourced, in some way.

Cheers,
Patrik
 


Jan Vincent Liwanag

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--

Patrik Nordwall
Typesafe Reactive apps on the JVM
Twitter: @patriknw

Jan Vincent Liwanag

unread,
Mar 24, 2014, 10:04:49 AM3/24/14
to akka...@googlegroups.com
Is it "normal" to create a custom journal and snapshot mechanism for prespecified messages? Or should journal and snapshot implementations be generic -- that is, they should be able to handle any type of message and state - much like the default leveldb one?

Jan Vincent Liwanag

unread,
Mar 24, 2014, 10:06:41 AM3/24/14
to akka...@googlegroups.com

Akka Team

unread,
Mar 24, 2014, 10:39:27 AM3/24/14
to Akka User List
Hi Jan,

You can have only one persistence plugin per system so if you implement an SQL based storage plugin it will be shared by all persistent components. It might be a working solution to have a plugin that delegates to an "ordinary" storage for normal messages, and stores a specific subset of messages in the SQL storage.

If you are fine with your journal entries getting into SQL after a delay, you might want to use a View for that. Then all your messages are persisted to an "ordinary" journal first (for quick recovery) and the view puts them into a different storage (for example for querying reasons). The second storage populated by the view will see the data delayed.

-Endre


On Mon, Mar 24, 2014 at 3:04 PM, Jan Vincent Liwanag <jvli...@gmail.com> wrote:
Is it "normal" to create a custom journal and snapshot mechanism for prespecified messages? Or should journal and snapshot implementations be generic -- that is, they should be able to handle any type of message and state - much like the default leveldb one?

--
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Akka Team
Typesafe - The software stack for applications that scale
Blog: letitcrash.com
Twitter: @akkateam

Jan Vincent Liwanag

unread,
Mar 24, 2014, 9:53:29 PM3/24/14
to akka...@googlegroups.com
Oh. I see. It would've been nice if I can choose the persistence plug in per actor type.

I've read up on Views but don't exactly understand what they are for. Is it better to have the view to log the messages into sql than to have the processor do it?

Sent from my iPad
You received this message because you are subscribed to a topic in the Google Groups "Akka User List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/akka-user/yg3_Hapvli0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to akka-user+...@googlegroups.com.

Patrik Nordwall

unread,
Mar 25, 2014, 9:30:55 AM3/25/14
to akka...@googlegroups.com
On Tue, Mar 25, 2014 at 2:53 AM, Jan Vincent Liwanag <jvli...@gmail.com> wrote:
Oh. I see. It would've been nice if I can choose the persistence plug in per actor type.

I've read up on Views but don't exactly understand what they are for.

Replaying the persistent messages of a Processor or the events of an EventsourcedProcessor in another actor (the View). In the view actor you can build up another representation of the state, and/or optionally save it in another data store (e.g. optimized for queries).
 
Is it better to have the view to log the messages into sql than to have the processor do it?

That is a better solution if the eventual consistency aspect is alright for you.

/Patrik



--

Jan Vincent Liwanag

unread,
Mar 25, 2014, 10:26:12 AM3/25/14
to akka...@googlegroups.com
Got it. Thanks for the advice!

Sent from my iPhone
Reply all
Reply to author
Forward
0 new messages