dubeypranav4

dubeypranav4

How to Implement Dynamic Process Pooling in elixir with custom submission logic?

Hello Elixir Community,

I am working on an Elixir application where I need to introduce a process pooling mechanism (thinking of using :poolboy). The goal is to create a pool of worker processes that can dynamically handle different types of payloads( such as company IDs or other data). Here’s a brief overview of my current setup and the changes I plan to make:

Current Setup:

  1. Application Module: The application has several GenServers and one supervisor with all the genservers as direct childrens with a :one_for_one strategy.
  2. GenServer Process: Each GenServer has a self-calling mechanism to process a list of companies periodically with the help of delay and what we call :tick as a message. These genservers do thing like fetching data for these companies, tracking internal progress of what work has been done, state of work being done, submitting work and so on.

The way it currently works is that there is a process CompanyDataManager who has the responsibility of maintaining a list of companies which it periodically refreshes from an API. On arrival of a new company or change in it’s metadata, it does chores like creating a new schema, creating new required entities in existing table and so on, post which it updates it’s local ets and the company (along with it’s metadata) is then ready to be processed.

Other gen server processes right now call a function in CompanyDataManager::process_enabled_companies which basically takes a function and args as parameters and runs the function for all the companies.

New setup:

The new setup plans to introduce a pooling in between so that any genserver process right now can be retrofitted to run in a pooled fashion based on the defined config.

The payload submission logic should be overridable wherein one process might have a different way of generating/submitting payload and handling it.

The default payload submission logic would be same as now which would be an infinite stream or a process variable based circular list kind of setup to iterate over list of companies and submit it to the worker.

Concern/Questions

For most of the part I understand that i can individually convert each process into a supervisor with two childrens: 1) ProcessManager- which will manage the logic of submission to worker, 2) DynamicWorkerSupervisor: which will manage the workers and it’s lifecycles.

  • What i want to do is have a generic enough design so that i can later on move any other process from the :tick based mechanism to the pool based mech?
  • Float the errors up and have custom error handling logic, similar to how genservers errors are treated by Supervisors (fail if X no. of errors are observed in Y time).
  • Recommendation on a better design or alternative.

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey @dubeypranav4 welcome. The first rule of pooling in Elixir is that pooled processes are only useful if they actually have a purpose to existing while idle. Something like holding onto a resource (like a connection) or some piece of data. If your processes are just sitting around waiting to be assigned then I think you should just spawn a new process whenever you want one. There is no benefit to having them around already.

If you’re trying to limit how many of these are alive at a time you can control that from your Process manager.

The only final thing I’ll say is that while it’s a bit hard to tell from your description, make sure to avoid Process-related anti-patterns — Elixir v1.18.0-dev. Make sure to separate the runtime activity that actually needs to be performed from the structure of code or even data that the activity is being performed on.

dimitarvp

dimitarvp

Latency / speed. I experimented with task pools a year or so ago and found that if the task pool keeps all code-executing processes around then there’s a slight speed advantage (or not so slight; in some cases it was as high as 3ms better). It did help for my tests with trading API scanners.

…Come to think of it, I should really polish these projects and publish them, they’d make a good portfolio / bragging rights pieces.

dimitarvp

dimitarvp

I’ll finally have some free time this year and promise to assemble some for you and whoever is interested. Remind me if I forget, it’s an area of interest to me – but my curse is that I operate with limited energy and never finish those things once I get the information that I wanted (which is always at the 80% - 90% mark).

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement