How to send thousands of genserver messages correctly?

if 1000’s of genseever process sending message to a single genserver by using GenServer.cast. and single genserver can’t able to process message that much. but whwn i use spawn_link inside handle_cast then it creates no problem. but we know if we spawn that much process thwn it will increase load. so i am thinking about using queue to store and then process request.

now tell me, how to handle this type of situation?

Any suggestions?

Who are those “we” that know this? The Erlang runtime can handle hundreds of thousands of processes and this has been proven by practice.

2 Likes

yes, genserver can handle, i try this in benchmark, but i am asking, the logic i have to implement inside handle_cast is taking a lot of time. so firstly, i used spawn_link inside handle_cast and then it is working perfectly but when i remove spawn_link because spawn_link is increasing load and using a lot of resources, if i create process for every single request.

now tell me how to handle this problem?

how about using queue?

Again, how do you know this? Have you measured?

Actually what’s wrong with your program? Does it not work? Does it consume 15GB RAM?

1 Like

what are you trying to solve? there are several scenarios for that to happen and that are multiple ways of handling that.

edit: i’m asking that because specificity might direct several of the solutions possible. but delegating processing work to other processes is a normal thing, all http servers written in erlang/elixir does that. rabbitmq does that, ejabberd does that.

have you used that solution and the node was not able to handle the load? are you assuming that? how you’re measuring that?

2 Likes

i am creating a chat system. where many sessions send message to group genserver.

i was doing benchmark by using benchee library.
where i send 1000 GenServer.cast message to group genserver?

My advice for you is to avoid modeling data into process/genserver structure. with this brief description, a “group” seems to be a data abstraction not a execution abstraction, coupling both would make terrible to scale the system no matter what solution you choose.

Actually, I think using one process to represent each participant and one process to represent the “room” for ordering of the messages from different parties is a good idea; it is precisely that sort of thing that the Erlang process model is trying to solve.

On the other hand, I am not sure if the OP really need to spawn for message processing.

the logic i have to implement inside handle_cast is taking a lot of time.

How much time are you taking about? Why it is taking so long? I’d argue against throwing processes at a performance problem until the bottleneck is understood.

I think it’s better to ensure order with data storage + sorting and use a pubsub to distribute message between participants/users. a process is transient and can crash at any moment, if your order/history is ensured by it, when it goes down, also your history and order goes down.

It is our job as programmers to prevent that. Sure, we can use a database or something even fancier to provide better resilience; However, a GenServer is simple, has no dependency, and may be good enough for OP’s application.

it taking more than usual because it have to check ets table and find data, if it find then stop another genserver and start new genserver. if it does not find then they start a new genserver.

the problem rises here, when i use spawn_link inside handle_cast, if work fine but when i remove spawn_link then it does not start new genserver.

This should be fast.

BEAM lets you dynamically start GenServers cheaply. However, doing this in a high enough frequency so that it becomes a performance bottleneck smells like bad design.

Anyway, there is nothing inherently wrong about jamming a GenServer’s message queue with tens of thousands of messages occasionally, if the said GenServer only handle casts.

Slightly off-topic: If you want to get help on a forum, you need to isolate your issue and describe it in a few paragraphs. The process alone will help you to clear your mind and sometimes the answer might jump out without anyone else involved.

2 Likes

i found my problem. the problem is in benchee where it start to send messaage in a loop. handle_cast does not have any problem?