Redis, developed in 2009, is a flexible, open-source, key value data store. Following in the footsteps of other NoSQL databases, such as Cassandra, CouchDB, and MongoDB, Redis allows the user to store vast amounts of data without the limits of a relational database. Additionally, it has also been compared to memcache and can be used, with its basic elements as a cache with persistence. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets. Redis is written in c.
Why Redis is different compared to other key-value stores?
Redis is a different evolution path in the key-value DBs where values can contain more complex data types, with atomic operations defined on those data types.
Redis is an in-memory but persistent on disk database, so it represents a different trade off where very high write and read speed is achieved with the limitation of data sets that can’t be larger than memory. Another advantage of in memory databases is that the memory representation of complex data structures is much simpler to manipulate compared to the same data structure on disk, so Redis can do a lot, with little internal complexity.
Redis Data Types
Redis has five data types: Strings, Sets, Sorted Sets, Lists, Hashes
Strings
Strings are Redis’ most basic data type.
Some common commands associated with strings are:
SET: sets a value to a key
GET: gets a value from a key
DEL: deletes a key and its value
INCR: atomically increments a key
INCRBY: increments a key by a designated values
EXPIRE: the length of time that a key should exist (denoted in seconds)
Strings can be used to store objects, arranged by key.
Sets
If you want to combine strings, you can do that with REDIS sets, a collection of unordered strings.
Some common commands for Sets are:
SADD: Add one or members to a set
SMEMBERS: Get all set members
SINTER: Find the intersection of multiple sets
SISMEMBER: check if a value is in a set
SRANDMEMBER: Get a random set member
Sets can be helpful in a variety of situations. Because each member of a set is unique, adding members to a set does not require a “check then add” operation. Instead the set will check whether the item is a duplicate whenever a SADD command is performed.
Sorted Sets
Sorted sets have an intuitive name: they are a collection of strings associated with a number and are arranged by default in order of least to greatest.
This datatype works well with ranges, and, because they are ordered from the outset, adding, remove, or updating values can be done quickly.
Some common commands for Sorted Sets are:
ZADD: Adds members to a sorted set
ZRANGE: Displays the members of a sorted set arranged by index (with the default low to high)
ZREVRANGE: Displays the members of a sorted set arranged by index (from high to low)
ZREM: Removes members from a sorted set
Lists
Lists in Redis are a collection of ordered values. This is in contrast to Sets which are unordered. You can add elements to the beginning or end of a list (even when there are over ten million elements in the list) with great speed.
Some common commands associated with Lists are:
LPUSH: Add a value to the begining of a list
RPUSH: Add a value to the end of a list
LPOP: Get and remove the first element in a list
RPOP: Get and remove the last element in a list
LREM: Remove elements from a list
LRANGE: Get a range of elements from a list
LTRIM: Modifies a list so leave only a specified range
Hashes
Hashes in Redis are a useful tool to represent objects with many fields. They are set up to store vast amount of fields in a small amount of space. A hash can store more than 4 billion field-value pairs.
Some common Hash commands are:
HMSET: Sets up multiple hash values
HSET: Sets the hash field with a string value
HGET: Retrieves the value of a hash field
HMGET: Retrieves all of the values for given hash fields
HGETALL: Retrieves all of the values for in a hash
Great article sir, i have looking for data structure implementation on the web. this is surely a good tutorial.