Using NIF to handle both compute-bound & IO-bound tasks in elixir 

Two type of tasks

  • CPU Bound
  • IO Bound

Core concept

Make full use of CPU

CPU Bound

Solution: return cpu time back to user space

  • less scheduling of threads
  • dedicated core for threads
  • make full use of cache
  • branch predicting friendly
  • SIMD

problem: to much waste of time on scheduling

IO Bound

Solution: give back cpu time back to kernel

  • less threads
  • callback / coroutine
  • reactor & proactor

problem:too much threads waiting blocked cpu

IO & Compute Both Bounded?



Analytics Service

Main task type:map/reduce on prefetched cache

Soluiton 1

Ruby + Rust


Rainbows for async



Expose Pure FFI

Ruby Native Extension

Wrapper at Ruby side


  • Eventmachine is a bullshit
  • Boilerplates
  • FFI safety
  • Mandatory Free

Why Rust



Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.

As a NIF library is dynamically linked into the emulator process, this is the fastest way of calling C-code from Erlang (alongside port drivers). Calling NIFs requires no context switches. But it is also the least safe, because a crash in a NIF brings the emulator down too.



Techempower round 14

alioth benchmark game (


”Rust now beats C++ in many benchmarks and is on par with others“


  • Meta Programming (macro)
  • Type Inference
  • Trait Mixin

Solution 2

Elixir + Rust


  • Elixir's high concurrency

  • Better interface between Elixir & Rust (Rustler)

But cons:

  • Elixir is much slower

  • Elixir has less 3rd-party libraries

  • Exotic grammar

  • Not compatible with Strikingly's deploy logic

Step by step

Translate interface code

Implement the server

System structure


  • You need a customized erlang VM (

  • elixir --erl "-smp +K true +A 10 +SDio 300 +SDcpu 100:100" -S mix phoenix.server

Deployed since 2016.11

0 Down time


  • Use proactor to read data from redis
  • Use Weld to improve throughput

Weld: computation engine from Stanford

That's it

Elixir + Rust Sharing

By Weiyüen Wu

Elixir + Rust Sharing

  • 1,004
Loading comments...

More from Weiyüen Wu