Redis is all the rage lately. If isn't helping your company get bought for a billion dollars, it's helping run your favorite sites like Stack Overflow, GitHub and craigslist.
Redis is a key/value data store focused on performance. At a glance, it's deceptively simple with only five primitive data types and a set of simple commands. But with simplicity comes great flexibility and Redis shines in use cases from tiny to enormous and everything in between. Redis most likely won't replace your traditional relational database, but it's many uses (like caching, queues, pub/sub) can augment any application.
If you'd like to learn more about Redis, we recommend the following resources. Go ahead and read through them. We'll wait here until you're ready.
Redis in .NET
While there has been some effort to run Redis on Windows, it's not officially supported. AppHarbor has your back though with our Redis To Go add-on. After installing the add-on you can access your Redis To Go instance from your local machine but just be aware that there may be a little bit more latency between your local machine and Redis To Go than there will be between our servers and theirs. This is nothing to worry about though, Redis is so fast you probably won't notice.
To interact with the server you'll want to use either ServiceStack.Redis from Demis Bellot or Booksleeve from Marc Gravell. Both make all the Redis commands available to you in a .NET friendly way. For this post our examples will use ServiceStack.Redis, but Booksleeve will work just as well if it's more your style.
Create a new AppHarbor application and add the Redis To Go add-on. The free 5MB plan will suffice for this example. Once you've set up your application, create a new project in Visual Studio. The easiest way to get ServiceStack.Redis is via NuGet so add that to your project as well.
To access your Redis To Go instance we need to grab the server URL. From your application's page click on the Redis To Go entry under 'Installed Add-ons' and then click 'Go to Redis To Go'. You should see a screen that looks like this:
Copy the URL at the top of the screen that starts with
redis://. We'll use this URL in our local environment by creating an application setting in our app.config (or web.config in a web project) like this:
<add key="REDISTOGO_URL" value="redis://redistogo-appharbor:firstname.lastname@example.org:9081/"/>
Be sure to add a reference to
System.Configuration so that we can access the app setting from our code. If you're using a console app, make sure you are not using the client profile.
Now we're ready to write some code! Let's write a simple string value to the 'hello' key and then write it back out to the console:
static void Main(string args)
var url = new Uri(ConfigurationManager.AppSettings["REDISTOGO_URL"]);
using (var redis = new RedisClient(url))
If everything checks out when you run the program
'world!' will be displayed.
Turning it up to 11
We were recently very impressed with Compilify, a web site that allows you to compile and run .NET code from your web browser. Compilify works by taking the code entered in the browser and putting into a Redis-based queue to be processed by a background worker. The background worker listens for new jobs in the queue, executes the code contained in the queue items and uses Redis' publish/subscribe facilities to send a message with the outcome back to the browser. Check out the source code of the worker to get a sense for how Redis makes some traditionally complicated tasks very simple and straightforward. Learn more about Compilify from our interview with Justin Rusbatch.
Web or Worker
Redis isn't just for worker processes. Your web applications can take full advantage of it as well. One of the strongest use cases for Redis is caching. If you're starting to see performance issues in your site, adding Redis as a caching layer can provide significant performance boosts with minimal code. Read Kevin Montrose's answer about how Stack Exchange uses Redis for caching to learn more.
Have you deployed an AppHarbor app that uses Redis? If so, we want to hear about it! Drop us a note describing how you used it and if we feature it on the blog, we'll send you some AppHarbor swag. If you have any questions about using AppHarbor and Redis, we're always standing by to help.