Welcome to weblogs.com.pk Sign in | Join | Help


Redis Series

redis-logoREmote DIctionary; or Redis is an open source data structure server; its a key-value database and can be used as NoSQL database, cache and message broker.

redis-cliIts distinguishing feature is that we can store data structures such as strings, hashes, lists, sets, sorted sets, bitmaps, hyperloglogs and geospatial indexes. It also offers functions around the data structures for instance range queries for sorted sets, radius queries for geospatial. It has replication support built in and we can have master-slave based tree like Redis cluster. It has Least Recently Used based Eviction / Cache Expiration mechanism along with transaction support. There is also Lua scripting support as well. Redis typically has all the data in the memory but it also persists it on to the disk for durability; it journal its activity so in case of any failure only few seconds of data get lost; it can write data to file in the background using the journal and we can also snapshot the in memory data.

We can get Windows optimized Redis releases from https://github.com/MSOpenTech/redis/releases that are maintained by https://msopentech.com; a Microsoft subsidiary; they had AppFabric product that had Redis like Caching component; it seems they dont have any plans to continue it any further given they are now Open source friendly company and instead is offering Windows optimized Redis through GitHub; and its great!

I simply run the installer and it did everything “Windows way” the binaries are in Program Files; and there is also Redis service defined; we can configure it as desired; run it from Administrative command prompt. Similar to ZooKeeper; it comes with redis-cli that we can use to connect to local Redis server. There are plethora of commands that we can play with using the CLI. Some of them are shown in the screenshot.

We can use keys command to query the keys and del to delete them. SET command has nx parameter; if specified; it will only set the key value if its not defined. There is also xx parameter; if specified; it will only set the key value if key already exists. These are useful when multiple clients want to set the same key. SET also has ex and px parameters where we define expiration time of the key in seconds and milliseconds respectively

  • GETSET is an interesting command; it sets the new value and retrieve the old value in the single go; useful for resetting counters!

redis-keys-set  redis-keys-expiration

  • We can give multiple key names while deleting

The keys and values can be maximum of 512Mb in size, keys can be any binary data; string, integer or even file content; but its recommended to use appropriate sized keys with type colon value colon something else; for example user:khurram etc

Using MGET and MSET we can retrieve and set multiple keys; useful for reducing latencies. We can use EXPIRY existing-key seconds to set the cache expiry of existing key; and use TTL key; to know the remaining time for cache expiry.

For the lists; there are LPUSH (Left / Head) and RPUSH; using which we can push multiple values against single key (Lists). We can use LPUSH/RPUSH key val1 val2 … to push multiple values at once. LRANGE is used to retrieve the values and takes start and end index parameters. We can give –1 as parameter for last index, –2 as second last; so to retrieve whole list we will use LRANGE list 0 -1

  • The lists can be used for Producers / Consumer scenarios; RPOP exists especially for Consumers; and when list is empty; it will return null
  • There is also LPOP but not used in Producer / Consumer; Producer should use LPUSH and Consumer RPOP
  • BRPOP and LRPOP are Blocking versions of RPOP and LPOP; and instead of polling; consumers can uses BRPOP

LTRIM is similar to LRANGE; but it trims the remaining values; we can use it before pushing the data and it will only take the defined elements

Given Redis is a network server; we should secure our Redis; we should use iptables / firewall so clients from known locations can connect to it; there’s also a security section in the conf file; on Windows; the conf file is passed as parameter to the service binary and its in Program Files\redis; we can open it up and enable authentication

  • Additionally you can run the service under specific login, giving required permissions to run as service, can listen on network and NTFS permissions. Its always a good idea to run the services (and especially network services) under a login with just enough permissions. Take a look at http://antirez.com/news/96 how one can compromise Redis in few seconds


Redis will not let read/write data unless client authenticate themselves first


You can see that similar to ZooKeeper; Redis can be used as foundational service in modern distributed applications. Similar to ZooKeeper; the application workers connect to Redis server over network and there are libraries for many languages; from C/C++ to Java/C#, Perl to Python, ActionScript to NodeJS and Go. In the next post; we will build some client applications

Published Tuesday, March 07, 2017 5:09 PM by khurram


No Comments

New Comments to this post are disabled