Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kousa: Arch: Consider using Phoenix Tracker or Phoenix Presence instead of database #407

Open
ityonemo opened this issue Feb 25, 2021 · 5 comments
Labels
kousa Issues related to the elixir API
Projects

Comments

@ityonemo
Copy link
Contributor

For keeping track of user online state. This information doesn't really need to live in the database, and having frequent read/write events to the db on this content will eventually hammer your database and cause responsiveness issues.

You don't need to pull in Phoenix as a dependency to use Phoenix Tracker, it exists as standalone library. https://hexdocs.pm/phoenix_pubsub/Phoenix.Tracker.html#content

@benawad
Copy link
Owner

benawad commented Feb 25, 2021

I'm all for moving this out of the db, if you have a good way to fetch your followers that are online.

Right now it's easy because I can just do a join (https://github.com/benawad/dogehouse/blob/staging/kousa/lib/data-layer/follower_data.ex#L25), how would you go about querying that if we move it out of the db?

@ityonemo
Copy link
Contributor Author

ityonemo commented Feb 25, 2021

I've actually never used tracker, I've only used Phoenix Presence, which builds on top of Tracker (but requires full Phoenix dependency), so here is my guess:

  1. whenever someone connects in with you register their genserver with Tracker

  2. Tell Tracker you are available. This sets the CRDT to register you as "being present". This then broadcasts over a pubsub channel that you are here.

  3. then, you query the DB to get a list of people you are following.

  4. subscribe to a Phoenix PubSub channel for each of the people you are following.

  5. fastlane-query Tracker to know immediately if anyone you are following is online, set their state to "online".

  6. if the online state changes, your genserver gets sent a message that "hey X is online".

  7. Note: Phoenix PubSub knows how to plug into erlang distribution so when you go multinode everything will magically work with zero lines of code.

  8. Tracker uses is a CRDT, so when you go to distributed erlang across multiple nodes you will have eventually consistent state across all of the nodes (it won't matter which node their WS (singular or plural) is connected to), with zero lines of code.

Note this is a "push" model, not a "pull" model.

I might not be opposed to working on this to get a better understanding of how Tracker works.

@benawad
Copy link
Owner

benawad commented Feb 25, 2021

  1. We could limit the number of people you can follow for this to work, but on the other side of things you can invite one of your followers to a room who is online. Big accounts could have millions of followers which I assume is too big for this method to work.

Also displaying when a user was last online is a thing which might just need to live in the db

We could still add this to alert you when someone you follow comes online though

@ityonemo
Copy link
Contributor Author

Big accounts could have millions of followers which I assume is too big for this method to work.

Haha why not? I'm not sure how far this scales! A million followers is still only like 16 MB or so. You can push bigger content than that over erlang distribution (especially after OTP23 so there is no longer HOL blocking erlang/otp#2133). Everything after that is diffed, so no biggie.

Also displaying when a user was last online is a thing which might just need to live in the db.

That is the case.

@benawad
Copy link
Owner

benawad commented Feb 25, 2021

If last online is going to be in the db, then might as well keep isOnline and have tracker for websocket updates and not queries

@benawad benawad added this to help wanted in main Feb 25, 2021
@ityonemo ityonemo changed the title Arch: Consider using Phoenix Tracker or Phoenix Presence instead of database Kousa: Arch: Consider using Phoenix Tracker or Phoenix Presence instead of database Feb 28, 2021
@TheOtterlord TheOtterlord added the kousa Issues related to the elixir API label Apr 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kousa Issues related to the elixir API
Projects
main
good for contributors
Development

No branches or pull requests

3 participants