Preemptive scheduling
Not one single OTP Process
(what in other programming languages are called actors, green threads, fibers) can affect the latency of the others. The average execution time of any given function should remain mostly the same even under heavy load – a huge selling point IMO. That’s why Phoenix apps on $5 worth of servers can handle 2000+ requests a second (while a Ruby on Rails or Laravel vanilla app can be brought to its knees by 100-200 requests a second).
Parallelism / concurrency
Consider this:
def send_email_batch(list_of_emails) do
# This will receive no more than 100 email addresses
YourMailSender.send_batch_message(list_of_emails)
end
list_of_emails # Supposedly a very big list
|> Stream.chunk(100)
|> Task.async_stream(&send_email_batch/1, timeout: :infinity, max_concurrency: 20)
|> Stream.run()
This will get a big list of emails, break the list into smaller lists of 100 emails each and send each chunk (batch) in parallel, but it never sends more than 20 batches at the same time (the
:max_concurrency
option of Task.async_stream
), e.g. at any given time maximum of 2000 emails are being sent (if we assume this is your email sending provider’s API rate limit and that you want to comply with it).
This extremely transparent parallelism / concurrency is what brought me to Elixir.