Part 1: Make yourself a little (ex)Redis

Konrad Oleksiuk
3 min readJun 25, 2017

--

I often try to build some software with new technologies I learn during my spare time (or even work time!). But I always tend to drop what I do and start doing new things, so I had to rework the way I build personal things. Solution? Write a series of blog posts that describe what I do. Let’s start!

I will not describe how to install & setup Erlang, Elixir. What I can do is to promise I will use as little dependencies as possible. In fact, I will try to keep first few posts without any external dependencies — they maybe will come later. I do not have a plan how this will end up — I plan to implement GET, SET, PING and benchmark them using redis-benchmark.

What I plan to use: Elixir & Mix, gen_tcp to handle client-server connections and few options for a storage — ETS, Mnesia & in-memory to start with. I chose knagaroo name for the app, but you can always go with whatever you pick.

mix new knagaroo --sup

This will create a new Elixir app with a supervisor. You can also add few more options to this mix task, to find out what are they, take a look at really great documentation by typing mix help new. You can now enter this directory and run mix test just to make sure everything works well.

~/Dev/elixir
⟩ cd knagaroo/
~/Dev/elixir/knagaroo
⟩ mix test
Compiling 2 files (.ex)
Generated knagaroo app
..
Finished in 0.04 seconds
2 tests, 0 failures
Randomized with seed 752462

There we go, feel free to delete those tests if you don’t need them, same with code in lib/knagaroo.ex. I chose to start building this small key-value DB by adding modules that later will integrate with parsed commands sent via TCP connection.

I began with tests for in-memory storage. This one is quite simple, as I use erlang’s map for that. Note: I could use Agent for this type of storage, but I wanted to make it more verbose.

test/storage/memory_test.exs

We will also need this module to run those tests, simply place in lib/storage/memory.ex this code:

defmodule Knagaroo.Storage.Memory do
end

And run tests:

⟩ mix test...Finished in 0.05 seconds
4 tests, 4 failures
Randomized with seed 194689

Nothing special, that was expected, let’s add a code for Memory server:

I used type specs for public methods, that’s something new for me to add in a project — I will try to use them as much as possible during this project to also learn new things. Overall it’s a quite simple module. I also decided to make SET operation synchronous for now, although it wasn’t really necessary. I will take care of tweaking later (I will try to post about benchmarking Elixir code).

That’s all for now, we have running storage module, let’s check it:

⟩ mix test
Compiling 1 file (.ex)
....
Finished in 0.06 seconds
4 tests, 0 failures
Randomized with seed 186099

Now we’re good to go later, in next post I will setup application to start with this in-memory storage and create a client that will use it.

--

--

Konrad Oleksiuk

Passionate programmer, currently working for Leadfeeder. I’m trying to make myself comfortable when running 10km. Right now I mostly do Ruby for living.