Redis — Installation and Explanation with Examples

HARDIK CHUGH
4 min readAug 27, 2019

--

Redis — Installation and Understanding at Command Line

Redis( Remote Dictionary Server) is an open source in memory data structure store i.e data is stored in main memory rather than being stored in some database. It can be used as a cache, database(NOSQL) and message broker, but in most cases it’s used as cache.

It stores data in the form of key value pairs.Data Structures supported by Redis are as follows:

  1. Strings
  2. Hashes
  3. Lists
  4. Sets
  5. Sorted Sets
  6. Bitmaps
  7. Hyperloglogs
  8. Geospatial Indexes

But primarily we use Strings, Hashes,Lists,Sets and Sorted Sets.

Languages which can be used with redis

Example

Twitter uses Redis for showing Timeline on users Feed

Twitter drives its timeline service with the help of Redis. Timeline is an list of tweets which are indexed by an id. Chaining tweets together in a list produces the Home timeline.

Installation

To install redis on mac os, follow these steps:

brew install redis

Now we need to start the redis server, which can be done by:

brew services start redis

If anytime you want to stop redis server , we can do that by:

brew services stop redis

Now in order to check whether redis server is working or not:

Redis server Working

Understanding Redis at Command Line

We can enter redis-cli with the help of following command:

redis-cli // Enter redis-cli to perform operations

1. Strings

Strings are the most basic kind of Redis value. Redis Strings are binary safe, this means that a Redis string can contain any kind of data, for instance a JPEG image or a serialized Ruby object.

A String value can be at max 512 Megabytes in length.

 SET age 29 // creating single Key value pairs GET age //returns the value set for age INCR age // Increments the age by 1 DECR age // Decrements the age by 1 MSET name "Johny" age "23" country "India" //creating multiple key value pairs  GET country // returns the value of country

EXPIRE age 10 // Expires value of age in 10 seconds
Strings Code Snippets -Redis
String Data Structure

2. Lists

Redis Lists are simply lists of strings, sorted by insertion order.We can add elements to Redis lists on the head (on the left) or on the tail (on the right) of the list.

LPUSH people “John” // pushes value of people from left of ListLRANGE people 0 -1 // Returns all the values in list RPUSH people "Karry" // Pushes value of people from right side of ListLLEN people // Returns length of the listLPOP people // Pops up value from left of the listRPOP people // Pops up value from Right of the list
List Data Structure

3. Sets

Redis Sets are an unordered collection of Strings. It is possible to add, remove, and test for existence of members in O(1).Redis Sets have the desirable property of not allowing repeated members. Adding the same element multiple times will result in a set having a single copy of this element.

SADD languages “Java” // adds memebers to set stored at keySMEMBERS languages // returns all the elements of the setsSISMEMBER languages "Java" // returns 1 if that element is present in set else returns 0
Sets Data Structure

4. Sorted Sets

Redis Sorted Sets are, similarly to Redis Sets, non repeating collections of Strings. The difference is that every member of a Sorted Set is associated with score, that is used in order to take the sorted set ordered, from the smallest to the greatest score.

ZADD items 5000 “Hard-drive”ZRANK items "Mouse" // returns the rank of set as per ascending orderZRANGE items 0 -1 // returns all the items in the set in ascending orderZINCRBY items 3 "Mouse" // increments the value for set mouse by 3
Sorted Sets Data Structure

I hope this gives pretty decent overview to start with Redis. To learn more about Redis checkout: https://redis.io/

Cheers!!!

--

--