Crystal or Elixir? A Good Problem To Have

Matt
Crystal or elixir

Both Elixir and Crystal were born from the Ruby community with a common goal: provide the same programming efficiency and happiness that Ruby brings, but with the speed to build scalable applications. The lack of speed in both running applications in production and running tests in development has plagued rubyist for a long time. We’ve worked around this problem with the help of background workers, CI servers, and just throwing more hardware at to help ease the pain.

For over ten years, we’ve been mostly fine with these work-arounds. We’ve built both small and big applications at Littlelines and never had any problems with scalability with Ruby. That being said, running a test suite with a thousands of tests is still a royal pain in the arse. And for over two years, we’ve been writing applications in Elixir for our clients and have enjoyed the speed that comes along with it.

A Closer Look at Crystal

Crystal and Erlang are fast…very fast. How do they do it? Short answer is that they’re compiled. Of course, there’s more to it than that, but this is the fundamental difference between them and Ruby. Each language brings its own flavors to the table, but my main point is that they are compiled to byte code when executed. Crystal is (relatively) new to the scene bringing nearly identical syntax as Ruby with the speed of C (again because its compiled native code using LLVM backend).

What we lose are some of the dynamic aspects of Ruby like executing strings as code at runtime. But, who does this anyway? We would consider this bad practice and ultimately leads to less maintainable and buggier code anyway. It turns out, we gain so much more from compiled code than just speed. For example, one of the favorite things is method overloading:

module Faker
  def self.words(count : Int)
    words = Data["lorem"].as Array
    words.shuffle(Faker.rng)[0, count]
  end

  def self.words
    self.words(1)
  end
end

Faker.words #=> "Lorem"
Faker.words(2) #=> "Lorem ipsum"

Boom. We can write methods and be assured what’s passed in as parameters is the type that we get. Not more if/else/is_a?/respond_to? stuff.

Perhaps most importantly with statically type systems like Crystal, many errors can be caught at compile time. Why? Because it has to - all types are non-nilable in Crystal and all null references are checked at compile time. This alone, can reduce many many of those pesky Ruby nil errors that keep our inboxes full of Airbrake notifications. For example:

puts user.first_name

$ crystal myapp.cr
Error in myapp.cr:5: undefined method 'first_name for Nil (compile-time type is (String | Nil))

puts user.first_name
          ^~~~~~

But Can I Put a Web Application in Production?

The jury’s still out (for me). I’ve been working with Crystal and its ecosystem for a few weeks and haven’t run into any roadblocks that would prevent me from putting applications in production today: A solid/fast language, background job processors, ORMs, dependency manager, mailers and an array of web frameworks to choose from:

  1. Amber doesn’t invent anything new when it comes to a web frameworks. It pulls in inspiration from other frameworks and feels very familiar if you have experiences with Rails or Phoenix. And it’s fast, according to this benchmark, it blows Phoenix out of the water.

  2. Lucky is an interesting one. It angle is that it catches bugs early with type safe database queries and form validations. It will tell is if your queries don’t match up to the database schema like wrong column name for field types at compile time. That’s awesome.

  3. Kemal like Ruby’s Sinatra, is a super simple web framework with web socket support. If you want to start playing with Crystal, I’d recommend install Kemal and giving it a spin - it’s an absolute pleasure to hack.

Going Further

I’m really liking the Crystal ecosystem so far and I’m excited for it’s eventual 1.0 release. If you want to learn more about Crystal, I’d check out their website and also be sure to also checkout a Awesome Crystal for a list of great Crystal projects in the works. Check out more upcoming articles on Crystal coming up.

Have a project we can help with?
Let's Talk

Get Started Today