[erlang-questions] Imagine every (active) entity is a process

Alexander Lamb alexander.lamb@REDACTED
Mon Nov 5 15:11:56 CET 2007


Hello,

So I am back with my design questions. I listened to the Armstrong  
interview and thought a bit more about processes keeping the state of  
an application.

For example, in an application which handles customers, would it make  
sense to create a process for each customer being worked on? Imagine a  
user searches for a customer to update his address, another user is  
creating an invoice for a customer, etc... When a given customer  
becomes active (in the sense that someone is working on the customer  
and his data), a process is created. Potentially, if the application  
is used by many users, you could have hundreds of customer processes  
being created. I assume this is not a problem with Erlang and  
encapsulated well the business logic.

Going one step further, I would imagine it is better to create only  
one process for any given customer at any given time. In other words,  
if I (a user) is updating customer "23" and at the same time, another  
user is adding an invoice to customer "23", we should both be speaking  
to the same process. It allows a better encapsulation and avoids  
mismatches because I would be updating a database with some customer  
info without the other user seing my update until a refresh. So I  
imagine it would be a good design to have one and only one process for  
a given entity with a given primary key.

Going this way, I therefore have a question (in addition to  
understanding if this is a correct way of thinking in Erlang):

When I would want to start working on customer "23" for example, I  
will first need to check if customer "23" is already an active  
process. If yes, I get the Pid and exchange whatever messages I need  
with customer "23". If not, I create the process and proceed.
Now, obviously, for my process to be known by all potential users of  
the process, I must register the process using the register(Name,Pid)  
function. As a name I could concatenate the entity name and its  
primary key (in my case: "customer_23").

Isn't there a race condition here. Since I have to check first if a  
process exists before creating it, I might end up in a situation where  
two other processes are doing the same thing and creating twice a  
process for the same entity. The second would obviously fail and we  
could catch the error. But is this a correct way of proceeding? Also,  
is it possible to have thousands of registered processes (as opposed  
to non registered processes)?

Thanks,

Alex
--
Alexander Lamb
Founding Associate
RODANOTECH Sàrl

4 ch. de la Tour de Champel
1206 Geneva
Switzerland

Tel:  022 347 77 37
Fax: 022 347 77 38

http://www.rodanotech.ch






More information about the erlang-questions mailing list