I recently began adding support for a global leaderboard system to my latest Unity game and decided that instead of the huge overhead related to going the RDBMS SQL route that i would have a crack at implementing back-end storage using a NoSQL database instead, which whilst much faster than traditional RDBMS is much more restrictive in terms of query. In fact Redis is for all intents and purposes almost impossible to query in any kind of depth. That said, I am not particularly interested in its relational query features but more in its ability to store and run basic queries on data very quickly, which Redis does extremely well.
Note that I am writing this article with a view to using Redis from Node.js, I will cover using Redis from .NET Core in a later article. If you missed my article on installing node.js then look here.
What is Redis
Redis is a fast in memory key/value store that can be used like a database. It differs aainly to SQL in that complex queries cannot be ran on the data, but if you do not need to carry out complex queries on the data (for example a persistent game world map or a leaderboard) then something like redis is ideal for storing the data.
Installing Redis
Windows
For Windows download the MSI release from here then install it. Redis will be installed as a service. You can take a look in Task Manager to ensure that Redis is running. To confirm the install open up command prompt and run Redis command line interface redis-cli.
Linux
Installing redis on Linux is not so easy because you need to build the latest stable release, firstly lets get the tools you are going to need to build Redis:
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install tcl8.5
Download and untar the latest stable build:
wget http://download.redis.io/releases/redis-stable.tar.gz
tar xzf redis-stable.tar.gz
Now lets build Redis:
cd redis-stable
make
make test
sudo make install
Run Redis as a background daemon (a script has been provided to do this), note that you will be asked to set some options, just choose the defaults for the moment:
cd utils
sudo ./install_server.sh
The above script creates a script in etc\init.d called redis_6379 (the numbers represent the port that you selected during install).
To get Redis to run automatically on boot:
sudo update-rc.d redis_6379 defaults
Installing Redis client library for Node.js
This is actually very simple, run the following:
npm install redis
We can now reference Redis from Node.js by importing Redis in our Node.js code:
var redis = require("redis");
Testing Redis with Node.js
Create a new file called redis_test.js and add the following code:
var redis = require("redis");
// Create the redis client
var client = redis.createClient();
client.on("error", function (err)
{
console.log("Redis error: " + err);
});
// Try out a simple command
client.set("my_key", "some value");
For more information on Redis see the Redis website