Codementor Events

Redis — Installation and Explanation with Examples - HARDIK CHUGH - Medium

Published Apr 25, 2020
Redis — Installation and Explanation with Examples - HARDIK CHUGH - Medium

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:

Strings
Hashes
Lists
Sets
Sorted Sets
Bitmaps
Hyperloglogs
Geospatial Indexes
But primarily we use Strings, Hashes,Lists,Sets and Sorted Sets.

1*88LJIfBZLpswtRtRso1JJA.png

Example

1*lI3jk9H6G8ynKRY-u5JTrw.png

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

  1. 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 List
LRANGE people 0 -1 // Returns all the values in list 
RPUSH people "Karry" // Pushes value of people from right side of List
LLEN people // Returns length of the list
LPOP people // Pops up value from left of the list
RPOP 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 key
SMEMBERS languages // returns all the elements of the sets
SISMEMBER languages "Java" // returns 1 if that element is present in set else returns 0

Sets Data Structure

  1. 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 order
ZRANGE items 0 -1 // returns all the items in the set in ascending order
ZINCRBY 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/

Discover and read more posts from HARDIK CHUGH
get started
post commentsBe the first to share your opinion
Show more replies