Saturday, 14 May 2016

Redis Tutorial (Basics & Configuration)

Background

I am just going to quote Redis from their website -

"Redis is an open source (BSD licensed), in-memory data structure store, used as database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster."

It is written in C language.




Peculiar features /  Advantages

  • Very Fast : Since Redis stores it's data entirely in memory (used disk for persistence) it is very fast in terms of I/O.
  • Various Data Structure Support : As saw before it supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries.
  • Atomic operations : All Redis operation are atomic which means there is no race condition. Each client/thread will get updates data.
  • Multi Usability - Redis can be used as a database, as a cache or a message broker (like activeMQ)

Installation

To install redis simply install following command -
  • sudo apt-get install redis-server

To start redis server run following command
  • redis-server


That's it your redis server is up and running.

You can test your server with redis-cli
  • redis-cli
and then type ping you should get reply as pong


NOTE : You can also install redis desktop manager for Ubuntu

You can set the redis configuration using following command -
  • CONFIG GET CONFIG_SETTING_NAME
You can get all configuration by using *
  •  CONFIG GET *
To edit a configuration use following command -
  • CONFIG SET CONFIG_SETTING_NAME VALUE


 Data Types in Redis

There are 5 data types that redis supports -
  1. Strings
  2. Hashes
  3. Lists
  4. Sets
  5. Sorted Sets

 Strings

You can do string data storage with GET and SET commands. You can store upto 512 MB of data in a String.
  • SET name aniket
  • get name

 Hashes

Sets the specified fields to their respective values in the hash stored at key. This command overwrites any existing fields in the hash. If key does not exist, a new key holding a hash is created.

You can do hash operations with HMSET, HGET or HGETALL
  •  HMSET employee1 name aniket code 007
  •  HGET employee1 name
  •  HGETALL employee1


Lists

 Insertion order is retained. You can use lpush and lrange to add and view the list
  • lpush countrylist India
  • lpush countrylist China
  • lrange countries 0 -1



Sets and Sorted Sets

Sets are unorderd collection of strings where as in sorted setseach string is associated with a score that is used to sort the strings.

You can use SADD and SMEMBERS to add and view members of a set where as you can use ZADD and ZRANGEBYSCORE to add and view strings in sorted sets.

In sets each string is unique unlike list. So adding same key twice will result in just one entry in the set.





Related Links




No comments:

Post a Comment

t> UA-39527780-1 back to top