Is Elixir and Phoenix a good choice for real time trading systems?

Is Elixir with Phoenix a good choice for building real-time stock trading systems ?

Requirements are High Availability and Low Latency.

What are your thoughts about this ?

What will be pros and cons if one uses Elixir and Phoenix in real time trading systems ?

5 Likes

IMO yes! It’s possible to treat offers as processes & index them to get really fast matching. Use commanded if you need event-sourcing for auditability.

5 Likes

It depends on your definition of real time. Erlang is quite popular in real time ad exchanges. Lack of garbage collection pauses give more predictable latency than some languages. It is considered “soft” real time, though.

When things need to be really fast and predictable, then C++ was popular, sometimes supervised by Erlang. That is a pretty good approach, isolating the pieces that need to be fast. Now speeds are such that things are done in silicon.

We wrote an embedded “logo inserter” box for the broadcast television industry in a mixture of Erlang and C++. The C++ handled the low level processing: pull a frame from a Blackmagic SDI interface, add an image, send it out another interface, 60 frames per second. Erlang handled everything else, and restarted the C++ if it should crash in time for the next frame. Bulletproof.

16 Likes

Thanks @jakemorrison and @AndyL.

Does Elixir with Phoenix is good choice for real-time stock trading systems ?

Depends on what you mean by this. I’ve heard success stories with c++ and erlang (c++ handled computations, erlang handled some networking). And some experiments with erlang + rust (now rust handles the computations).

I don’t quite see how phoenix fits in a trading system, though.

4 Likes

Low latency can mean a second to some people, and a millisecond to others. What range do you have in mind?

2 Likes

@dom Trading system placing buy/sell equity or FX orders to the market will target a latency of under 20ms

That sounds quite doable. Keep in mind that Erlang is optimized for robustness above math performance, so it lacks things like native integers (it uses bignums) and can be slow. If your system is computation-heavy, managing big baskets, etc. that may be a worry. NIFs may help. For stuff like order routing and as a backend for user GUIs though it should be a nice platform to work on.

2 Likes

Take a look at https://chaince.com/, it’s a crypto exchange which written by Elixir.

3 Likes

@dom and @gonglexin Thanks for your feedback’s.

I would think that from the point of view of a market maker, a real exchange and a crypto exchange are very different beasts in terms of response time / performance requirements … I think even ads (see adroll) come closer to what’s required from a real time system than a crypto exchange due to the inherent limits of crypto trading (which seems to be mostly retail for now).

@idi527 I agree with you !!

The beam is a top choice for trading platforms. The author of Learn You Some Erlang For Great Good, Fred HĂ©bert, was building an ad auction system while working on his book I believe.

3 Likes

Although it doesn’t seem like you really need it here, there is an interesting talk about building hard real time systems on top of the Erlang VM: https://www.youtube.com/watch?v=yjmZoAtkkLY
Even if you don’t need hard real time, the talk gives some nice insight into using NIFs and dedicated hardware to build high performance systems.

2 Likes

Wish I could see the code of that.

As a related thought, maybe we as a community should add another category in the Awesome Elixir (curated Elixir libraries list) that has pointers to libraries that allow you to have an Erlang-compliant node written in other languages.

I heard about C++, Rust and Go libraries but that’s about it. There are probably more?

3 Likes

hmm…yes, we think it’s a good choice for trading system. we wrote https://chaince.com as mentioned above by @gonglexin (and thank you so much :grinning:) based on pure elixir/phoenix/OTP infrastructure.

we’re using phoenix channel with websocket as order submission and trading reports system, it’s very fast. we think it can be called “real time”.

as to the High Availability, it’s just the pros of OTP. there’re 12 nodes in our cluster, and the whole cluster can be rolling restarted any time we like, without any service interruption.

“elixir is a good thing, maybe the best of things, and no good thing ever DIES” :relaxed:

4 Likes

Java … as much as I dislike that language, that can be a real deal-maker for large enterprise-y shops that are heavily vested in the Java ecosystem.

That said, you don’t really need a full node to just supervise external processes or do simple communication with them (stdout/stdin style). A port is enough, as it lets you launch, sent/recv data and get notified of termination. This really opens the door to executables written in all sorts of languages.

Goldman uses Erlang.

There was a big scandal a few years ago about one of their Erlang programmers.

3 Likes

@KishoreKarunakaran I’ve been working on just that :slight_smile: and get sub ms latencies with colocated servers. I’ve open sourced the plumbing for the framework here https://github.com/fremantle-capital/tai but it’s still very much in the alpha stages. I’m posting it with a huge disclaimer that this is the project I used to learn Elixir, so there’s bunches of code that needs improvements :grin:

Currently it only does cryptocurrencies but I’ve been playing around with adding an adapter for stock trading through https://alpaca.markets/

1 Like

Friends who work in high frequency trading tell me that in the hot path where latency is critical that languages with non-deterministic garbage collection such as Elixir are not suitable as the latency is not low enough it consistent enough.

Outside of the hot path Java and Python are used, so I suspect latency is less important there.

I suspect Elixir hits an awkward middle ground where it is too slow for the fast bits and too niche for the rest (given the extreme hiring requirements of these companies).

1 Like