Manuel Rubio
Manuel Rubio

Reputation: 114

Is it possible to make unique sessions in Phoenix Framework?

I was trying to avoid to fight with race conditions because of having two or more sessions opened for the same user so, I was thinking about to have only one session opened for each user closing old ones when a new one is opened. Do you know how to achieve this in the best way?

Upvotes: 0

Views: 205

Answers (1)

Kip
Kip

Reputation: 724

Personally I think that would be a horrid user experience - I would not expect be "managed" in this way by an application. I would expect I can have multiple sessions for a single user id.

Secondly, you would need to consider the possibility that your application is horizontally scaled and therefore multiple app/Phoenix servers. Now you have to manage uniqueness of a "user" across a distributed environment.

If you pursue this strategy then Phoenix Presence provides a means to manage distributed state but ... these are not atomic actions, they are eventually consistent. So that will just create another race condition if you already have one.

Otherwise you are looking at a centralised session store which would mean a redis instance or a database of some kind.

Personally, as a user, I really don't like sites that work the way you propose so hopefully you can resolve the race condition instead.

Upvotes: 2

Related Questions