views:

275

answers:

3

Erlang works with message passing between actors as it's concurrency model.

Assume I have 3 actors who sell items. The total number of items is 7. How do they excactly sell 7 items? How do they coordinate themselves? We could have one actor with the number of available items, acting on "buy" messages (inventory actor). This would be a SPOF though.

The same goes concurrency in other languages like Java when using message queues for concurrency instead of locks.

(Best without an Amdahl bottleneck)

+1  A: 

Do these actors act in a vacuum? They must either have 7 buyers or 7 items in inventory. Perhaps the queued buyers or inventory store should coordinate them.

Joe Koberg
+3  A: 

You have a process that represents a thing - in this case an inventory. When other processes want to buy, they ask the inventory, do you have one? can I buy it?

A process representing a delivery will tell the inventory, here are 20 new things...

Gordon Guthrie
+2  A: 

Hi funzel,

I would implement a server process responsible for the stock management, e.g. using a dets or a Mnesia table as backend. This porcess could be part of a supervision tree, so if it fails it would be restarted automatically. My salesman processes - the 3 actors you've mentioned above - communicate with this server process asking it for the items they sell. As long as there are enough items there's no problem, otherwise the salesmen would get the answer that the item is sold out - and another process gets the information to purchase new ones. **smile**

Btw, take a look at my series I've started at http://mue.tideland.biz/2009/03/software-architecture-with-erlangotp.html.

Warm regards

mue

Mue
Ah, excellent. A concrete answer, people on twitter just sneered. But would a central resource like a Mnesia table be "Erlang concurrency", it looks more like a shared resource (like using a database for sharing state)? (But with the pro in Erlang of being part of a supervisor tree)
Stephan Schmidt
Yep, Erlang also knows shared resources in forms of processes, tables, or the database. But it makes using them simple and save. Message queues handle synchronization problems, supervision cares for restarts, and Mnesia knows replication over different nodes. You have to care, but you get helped.
Mue
You could even get away without a process managing the tables, since that is what Mnesia does anyway. If you use transactions you'll make sure that the table is updated atomically.
Adam Lindberg