jaybe78

jaybe78

Hello,

I’m designing a bidirectional friendship system where I store the data in a single dynamodb “Users” table.
For the record I only store one side of the relationship and use a GSI to get the edges.

Each user could have between 1 and 1000 friends or more, and I could have 30_000 or more users joining the app at the same time.

I want to develop a feature where when any user who joins the app, he can quickly sees his friends that are online.

Obviously that involves querying friends for every single users joining and also probably caching them (:ets ?).

Each user joining is tracked using Phoenix.track() against his channel(“user:*”) so that I know whether someone is online or not. Their status is stored in an ETS table.

My initial idea is to fetch users’s friends and filter them with what is stored in the ETS table mentioned just before, to know whether one user is online or not.

Obviously the complexity here is that I have to fetch a lot of data at the same time, it represents a lot of queries, so I thought about using genstage or broadway…

May be I can also get away querying less…
For example if user 1, 2 and 3 are friends and the first query executed is for user 1,
I would already get his relationship with 2 and 3, that I could cache !?

May be store in an ETS table each relationship for each user !?
1 => 2
1 => 3
2 => 1
2 => 3

Then before querying friends, for users 2 and 3, I can already notify them that user 1 is online.

If anyone could provide some guidance to set up something scalable.

cheers

Cheers

Showing Posts 1 to 10

jhogberg

jhogberg

Erlang Core Team

Have you considered using a relational database instead? They excel at solving this kind of problem.

krasenyp

krasenyp

Either a relational database or a graph database will do the job. DynamoDB is definitely not suitable for modelling relationships.

jaybe78

jaybe78 OP

Thanks but I’m not asking about designing the relation between users.
I’ve already done that and it works really well.

Now to be more specific, my needs are pretty simple, I just need to return a list of friends for a user.
With DynamoDB you can implement a sort of graph using proper primary and sort keys.
It is extremely fast !
I don’t need all the extra features I would get from a graph DB like fetching friends of friends…

This is not the issue I’m trying to solve here.

Whether it’s a graph db or dynamo db, you still have to query the database to fetch the list of friends and you still have to go over them to see which ones of them are online.

jaybe78

jaybe78 OP

I might be missing something but to me this issue has nothing to do with the DB used.
DynamoDb is fine at mapping users’s friends with way better read performance than a SQL DB.

My question is about the strategy to fetch all those data for each user joining.

I could simply load a user’s friends on “demand”, basically when he joins.
Would that scale well when I have 50_000 people joining at the same time ?
May be, may be not but long term I think I need something more efficient to handle all that work.

Or I could take advantage of stuff like genstage/broadway to fetch/update those users’ friends with their current status ?

Lucassifoni

Lucassifoni

I might mis-understand your post, but, are you planning for high scale and lots of users being generally there at the same time , or for some reason, your total user number is reasonable (let’s say 80_000) but your domain has a reason for (almost) all of them joining at the same time, making huge bursts of sudden joins ?

jaybe78

jaybe78 OP

I’d say it’s both, lots of users connected at the same time and “huge” bursts of sudden joins sometimes.
Though I’m more preoccupied by the burst of users joining, because my plan is to cache the user friends in ETS table so I would only fetch them once for each user.
Then as mentioned above I would have to filter them out based on whether they are currently available or not, but that step should be straightforward.

DynamoDB is capable of handling more than 10 trillion daily requests, with the ability to support peaks of over 20 million requests per second.
So my question really is to either fetch each user friends separately on demand, or centralize that in a genstage/broadway.

jhogberg

jhogberg

Erlang Core Team

It has everything to do with the database used: the fastest operation is the one that never runs, and with a relational/graph database you can avoid the reads altogether. You ask the database for the friends which are online and get only those in the result, deferring the retrieval of the less-interesting offline friends for later, which may very well be cached end-user-side (e.g. localStorage) on a long-term basis.

The moment that you start doing relational operations over data that you have retrieved from a NoSQL database, you have invented your own half-finished, slow, and bug-ridden relational database.

You will need to adapt the application to make good use of the relational model, but once you have, a sustained “50000 users with 1000 friends each leaving and joining every second” – and likely many of your other problems too – become a non-issue.

Not all your data needs to be in a relational database either, but you may as well put it there: in my experience every clever NoSQL project ends up needing relations everywhere sooner or later anyway.

LostKobrakai

LostKobrakai

That expects that the db knows the online status. I guess this topic is about using Phoenix.Presence to track the online status while not having it stored in a database.

jhogberg

jhogberg

Erlang Core Team

Yes: keep it updated, lazily if required. It’ll be less work in aggregate than having to shuffle hundreds or thousands of relation tuples for an application-side join every time a very social user joins.

jaybe78

jaybe78 OP

No this topic is not about using Presence.

Whether you use Presence or not, you still need to get the list of your friends you want to subscribe to.
The topic is about fetching/updating efficiently user’s relationships and their status.

As mentioned above, Presence will be used but at the very last stage, when friends of a users have been fetched. I’m not concerned by that stage as I plan to use Presence sparingly on a subset of the friends returned

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews