victorolinasc

victorolinasc

Hi everybody!

I have an expensive CPU action that I want to serve through a public internet endpoint. It doesn’t matter what it does, just that it usually takes some time (seconds) and that it costs CPU (say a few percent of CPU usage). For a real use-case you can think of an expensive hashing algorithm like PBKDF or any other on a signin endpoint.

What I want to avoid is being hit by a flood of requests and suffer from resource starvation where everything would become unresponsive. Like the signin case, a public endpoint without authentication makes it a perfect target for attacks that want to bring the server down. Think that all network solutions are in place like rate-limiting, WAFs and so on.

I thought about 2 strategies in the BEAM:

1- Using a pool of processes. I’ve implemented this with poolboy and works fine but is hard to tune it. I have to benchmark the cost of the function in a production server and reach a pool size that will be a good enough “sharing” of resources. I am thinking about switching to wpool and having a bigger than needed pool with a callback module that would check CPU usage before dispatching to the pool. This seems to me very unreliable and prone to error… If we get several concurrent requests and the CPU usage from all of these is under control, then they would all start at the same time and the CPU would spike anyway…

2 - Using a slave node for doing just this operation and fight with the emulator flags to have it use other logical cores. Suppose I have 8 cpus available, I could dedicate 2 or 3 to the slave node and the rest to the main system. Though, with this strategy, it would still be vulnerable to resource starvation and make all calls to it fail which is not my intention here. I’d rather have timeouts than a denial of service.

So, I’d like to know if there are any other algorithms or strategies to deal with this. I appreciate your time :slight_smile:

Showing Posts 19 to 10

victorolinasc

victorolinasc OP

Awesome! Thanks a lot for the info! Will try to play with uwieger/jobs :slight_smile: A very interesting library to have under my set of tools.

Thank you all for your replies. We have tested the process pool approach and it is handling it pretty good up to now. Though the subject keeps interesting me and I will try other approaches.

cmkarlsson

cmkarlsson

I have used jobs (GitHub - uwiger/jobs: Job scheduler for load regulation · GitHub) for these kind of things before. It allows you to have a queue which is regulated based on CPU sampling for example.

Or you can use a “circuit breaker” library to deal with this. They generally implement the correct algorithms for this. I’ve used fuse (GitHub - jlouis/fuse: A Circuit Breaker for Erlang · GitHub) in the past but it hasn’t had any updates the last couple of years so don’t know if it will work with the latest erlang versions.

dimitarvp

dimitarvp

You could also try hCaptcha as a fallback.

victorolinasc

victorolinasc OP

Thanks! That is also a nice idea. Our “challenge” was an external service that went down (Google :slight_smile:).

We are testing other on-premises fallbacks . There are some interesting alternatives currently…

Thanks for your suggestion!

drakkhenn

drakkhenn

I think I would have first used a “simple challenge” just to avoid “script kiddies” then your “annoying algo”. If you have a lot of users, to a specific cpu → server → hardware (GPU).

victorolinasc

victorolinasc OP

Thanks! That is my current approach and it works quite well with the only caveat of having to “guess” the proper pool size for each environment.

The “normal” schedulers context switch very frequently but dirty_cpu schedulers might not do that (depending on the NIF implementation) so I am trying to be safe here and not ever allowing it to reach 100%.

dom

dom

In that case I think you could just use a big enough pool (say 2 * core count) for low priority requests, and do the work directly in the request handler for high priority requests, bypassing the pool.

Reject low priority requests if the pool is full. You can also use s_broker if you want to get fancy and allow some queuing to deal with sudden spikes.

Even if the pool is saturated and CPU usage reaches 100%, the app will stay responsive, since the BEAM context switches very frequently.

chasers

chasers

Ah … yeah I had an API product for years that was designed to be async. So people would POST jobs with a callback URL. We’d hit that with results, or with an endpoint to gather results when the job was finished. It worked well and people understood why we had to do that based on the type of work we were doing.

Other than that, I would limit concurrency with a pool. So the whole system can have N number of concurrent jobs. You don’t even need poolboy for this … just a Dynamic Supervisor limiting the number of children.

I personally wouldn’t try and base it off of CPU. I’m sure it can be done I guess but I’d start with concurrency limiting I think to play with that. Maybe then have a failsafe based on CPU if you want to push the boundaries a bit more.

victorolinasc

victorolinasc OP

Unfortunately our solution here is not LiveView based but HTTP API based =/

chasers

chasers

You can easily do this with LiveView.

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
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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

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
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews