Using redis with F# on Windows

In one of my next projects I’ll probably use F# + redis mix. I wanted to try it out, to get some general idea how it will work. I had no previous experience with redis, and I’m still beginner in F#. I wanted to do some end-to-end example for saving and reading data. My idea was to get some Bitcoin price from external service, save it to local redis instance, read it back and chart.

In this blog post, I’ll show how I got there, and what I learned in the process.

Installing redis on Windows

Redis is in-memory key value store. It’s very fast and can store data in few data structures – strings, hashes, lists, sets and sorted sets. Official releases run on linux, but Microsoft Open Tech group developed and maintains experimental port to Windows. And this version I used for my tests.

I simply downloaded code from github, build solution in Visual Studio, and run redis-server.exe from project folder. If you want to test some commands with client, run redis-cli.exe. And if you have no idea, what are the commands try.redis.io has pretty cool interactive tutorial, that cover some basics. Running redis from code build on your dev machine probably is not the best idea for production – I would instead choose to deploy it on some linux server. But for testing purposes it’s perfect.

Getting data from external service

That part was pretty straightforward. I decided to get data from bitstamp.net, because they API for Bitcoin ticker is super easy to use. It has some limitations, but I’ll get to this later. So I made request to https://www.bitstamp.net/api/ticker/ and parsed it using Json type provider from FSharp.Data package (you can install it with Nuget). Json type provider is awesome. After you feed it with some sample data, you have all your fields just “dot” away.

https://gist.github.com/mlusiak/7901990

One thing I learned hard way in this part of code, is that in currentPrice you need those parentheses. Without them, F# will treat the whole block (lines 13 – 15) as calculating value and binding it to currentPrice – not as a function. And because values are immutable by default, it will always “return” the same price.

Saving results to redis

To access redis, I used ServiceStack’s redis client. ServiceStack is great framework to access all kinds of webservices. You can install ServiceStack.redis package using Nuget. It’s free to use up to 20 types.

https://gist.github.com/mlusiak/7902309

You can see three approaches I took (the only difference are first two lines). First one didn’t work – it saved “empty” data (empty strings, integers equal 0, etc.). As Demis Bellot explained to me on StackOverflow, StackService rely on writable properties, which immutable records in F# don’t have. He suggested to use [<CLIMutable>] attribute (which I did in third approach). In between I found this blog post, that used mutable keyword on type fields. This also works, but is not as elegant as the attribute, because it exposes those mutable fields also within your F# code.

Getting data from redis

This was very straightforward. No issues here. It just worked.

https://gist.github.com/mlusiak/7903099

Charting the data

To visualize this data, I used FSharp.Charting library. To make it work, you have to install it with Nuget and reference some libraries like System.Drawing and System.Windows.Forms. I also had to load FSharpCharting.fsx file.

https://gist.github.com/mlusiak/7903123

And it works!

Bitcoin price 11th Dec 2013 around 2am CET
Bitcoin price 11th Dec 2013 around 2am CET

Putting it all together

To summarize – getting data, saving to redis, reading back and charting – all in around 30 lines (excluding referencing libraries) of F#.

https://gist.github.com/mlusiak/7903762

Two more remarks about this code. I call Bitstamp API every 31 seconds, because it gives new data every 30 seconds anyway. If you call it earlier, you’ll get the same data as last time. Second thing is this whole #if INTERACTIVE block. I don’t know why, but when I run code in F# interactive without it, it screamed about no references to libraries. I thought referencing them in project would be enough, but no. And if I wanted to compile and run .fs file, than it failed to compile with #r clauses in it (because .fs file don’t support them, they work only in .fsx files). #if INTERACTIVE allowed me to test code in F# interactive mode, but still compile and run this code as program.

Any comments about code, and way I did things will be appreciated. I’m new to F# world, and I am happy to learn what I could do better.

Some useful references

http://try.redis.io/
http://fsharp.github.io/FSharp.Data/
http://fsharp.github.io/FSharp.Charting/
http://blogs.msdn.com/b/fsharpteam/archive/2012/07/19/more-about-fsharp-3.0-language-features.aspx
http://caxelrud.blogspot.com/2012/11/using-nosqlredis-in-windows-with-f.html

2 thoughts on “Using redis with F# on Windows”

Leave a Reply

Your email address will not be published. Required fields are marked *