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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews