Wednesday, 25 January 2012

Run Redis.io DB as Windows Service

Run Redis.io DB as Windows Service

I recently started experimenting with Redis database (a NoSQL DB) as an alternative to SQL-Server for certain development requirements.
"Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets."
Since I do most of my development under Microsoft Windows, I was hoping to run my Redis instance on my Windows7 x64 Pro development desktop; more specifically, I wanted to run Redis as a Windows service.

Since Redis natively targets Linux/Unix environments, I went searching to see if there was a Windows port of the Redis database project that included the ability to run it as a service.  I found two open-source projects that, when combined, allow me to run Redis on Windows as a Service.

Running Redis DB on Win-7 x64

Installation Notes


The first thing I did was acquire a Windows port of the Redis server. I ended up using this project's compiled-version: https://github.com/kcherenkov/redis-windows-service. The project is described as:
"Windows 32 and x64 port of Redis server, client and utils.
It is made to be as close as possible to original unix version."
That "close as possible" statement mainly refers to how Redis commands that would (under Unix) rely on fork() to perform background operations are implemented as foreground operations (in Windows port).  But, for purposes of my software development and testing, this would suffice.  I can run my "production" instance of Redis on one of my Linux virtual machine instances (in particular, I have it running on OpenSuse 12.1 x64).

Get the Redis.io for Windows Build
To begin with, download the actual Redis.io (for windows) builds from here: https://github.com/dmajkic/redis/downloads (In my case, I selected the latest x64.zip build, which was redis-2.4.2-win32-win64-fix.zip).

Within that zip-archive, you will see two sub-directories: one is "32bit" and the other is "64bit".  Those zip file directories include the redis-server.exe and redis-cli.exe files (and redis.conf configuration file, etc).  Simply copy the contents of the archive's "64bit" (or 32bit) directory into the chosen directory where you will run Redis from. For example, I placed the x64 files into c:\Redis\

Theoretically, I simply needed to get the redis-server.exe running as a service now...


How to Install Redis DB as a Windows Service:

Win-7 x64 Installation Notes

OK, I have the Redis for Windows executables in my c:\Redis\ directory. Now it is time to get this database running as a Windows Service.  I found one such project that appeared active enough to merit consideration: (link) Run Redis as Service on Windows project on GitHub.

You need to download the compiled executable (RedisService.exe), which is available as a rather small (7 or 8KB) file on the "downloads" page for the project, and place it in your Redis directory.

Note: you may wish to reference this Microsoft site: using SC to create a service if you wish to understand in more detail what the upcoming commands I discuss are doing.

Although you may experience issues (as I will discuss next), you are now supposedly ready to install and start the RedisService.exe (from the command-line in a Windows console window) with the following command (note: alter the "Redis242" service-name to whatever makes sense for you as a process-label; also, change c:\redis portions to whatever directory location you chose):


sc create Redis242 start= auto DisplayName= Redis242 binpath= "\"C:\Redis\RedisService.exe\" C:\Redis\redis.conf"

IF the above statement *appears* to work, the service may or may not start when you execute the following:

sc start Redis242


But, if you experience some of what I did, the service may be failing for what I will call "hidden" reasons...

Fix Redis Windows Service Problems

and Potential Issues to Workaround

What I discovered with this RedisService.exe windows-service for Redis is that it is quite typical for open-source code: it makes a lot of assumptions and does little to provide proper dependency-testing and meaningful error-condition notification.

When you create the service (per above code: sc create ...) and/or try to start the service (using sc start) it may appear to just "hang" or otherwise take a very long time to attempt to start prior to failing with timeout errors.

The reason for redis-windows-service failing to start properly will be obfuscated, and here are some reasons why:
  • Starting the Service will throw 1053 (timeout) errors without indicating why, but one possible failure reason is that you must have the .NET Framework 4.0.30319 installed for this service to work.
  • Next, depending on your security setup (like my Windows-7 Pro security settings), you may need to tell Windows Firewall that it is OK for this process to act on your local network.  The easiest way to do this is run the redis-server.exe from the command-prompt and allow it access (to local network, through Firewall) when prompted.
  • Next, if you attempt run the redis-server again, you may see another (otherwise hidden) issue in that the executable is not from a "trusted source" or such: again, this issue can be resolved by choosing to allow this un-trusted process to run when provided the option.


After resolving this list of potential issues, you should be able to execute the sc create command and then perform an sc start redis242 (or whatever name you gave the service), to start the Redis Windows service and no longer experience a 1053 error due to timeouts caused by hidden reasons.

Redis Windows Service-Shutdown Problems

Note: there are problems with shutting down this service!  So far, the only way I have found to truly stop it is to reboot my system.  Also, when attempting to delete the service (sc delete redis242 or such), you will not be able to truly delete it as long as any Windows Service-Manager windows are open.

Once the RedisService.exe is installed and actually working, even "sc delete" requires a system reboot to take effect, since you can not otherwise truly stop the service.

The good news...
Although this service is problematic (as of when I wrote this tech blog entry), the program will run as a service and the client (redis-cli) can now be executed against the service-induced redis-server to test SET/GET of keys, etc.

If you are interested in accessing Redis.io from JavaScript, you may want to read my blog about NPM (Node Package Manager) where my example for installing a Nodejs module used a node-redis module.  I am able to access my Redis (NoSQL) DB from both Linux and Windows versions of Nodejs via the node-redis module's functionality from within Javascript.

 

No comments:

Post a Comment