Riak_Core with Elixir : Part One

This is going to be a multi-part blog series in which I’ll show how can you build an riak_core application using elixir.

I won’t go into details of what riak_core is or what can be achieved using riak_core. You can read about it here.

In this part we will try to configure riak core in our application.

Prerequisites

You would need to install erlang version 18 (please note that erlang version above 18 are not supported in riak_core as of now). Elixir version should be 1.3 or above.

Dependencies

We will create a mix project using command

mix new Vyuha --sup

once we have our project structure ready, we need to declare riak_core as a build time dependency in our mix.exs file.

defp deps do
[
{:riak_core, ">= 2.2.6", hex: :riak_core_ng}
]
end

Now we have to make sure that riak_core is started before our app by giving it as a runtime dependency. We will do it in application method in our mix file.

def application do
[ mod: {Vyuha.App, []},
applications: [:riak_core, :logger]]
end

In the first line, we specify the application module of our application (start/0 function will be called from Vyuha.App). In second line, we are telling mix to start riak_core and logger before it starts our application.

Configuration

Before we start our application, we need to configure riak_core by setting some default values. We will do this in our config.exs file

config :riak_core,
ring_state_dir: 'ring_state_dir',
handoff_port: 8099,
handoff_ip: '127.0.0.1',
schema_dirs: ['priv'],
ring_creation_size: 12,
vnode_inactivity_timeout: 1000

Here we are configuring riak_core for its different properties, like number of vnodes to create (which is 12 in this case), where to save ring configuration data etc.

Apart from configuring riak_core, we also need to configure two more runtime dependencies (these are part of elixir package). Again we will do this in config.exs

config :lager,
colored: true,
error_logger_hwm: 500
config :sasl,
errlog_type: :error

One more thing that we need to do before we can start our application is to create a “priv” folder under the root project folder and copy riak_core.schema from deps/riak_core. Run below command on root folder of your project.

> mkdir priv
> cp -r deps/riak_core/priv priv

Building Project

Get dependencies by running following command.

mix deps.get

Now run the application by running following command

iex --name nav@127.0.0.1 -S mix run

Here we have used a long name by providing a — name flag. This name identifies a Beam Instance on a machine. Name takes a form of arbitrary_prefix@host, where arbitrary_prefix is any name that you give to the Beam instance and host is the full ip address of the host machine. This will become important as soon as we try to run multiple Nodes on same machine.

Hint: if you get some weird riak_core errors, try to delete the ring_data_dir in your root folder, riak_core seems to store the node configuration from previous run and expects they are their in the next run.

This should start our riak_core application. In next post we will try to add functionality in our app. You can find code up to this point here.

Read part 2 here.

Like what you read? Give Naveen Negi a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.