w3resource logo

gallery w3resource

Redis – NoSQL Database, an introduction

Secondary Nav


This presentation describes an introduction of Redis, a NoSQL Database.


What is Redis?

► Redis (Remote DIctionary Server) is an open source, networked, single threaded, in- memory, advanced key-value store with optional durability. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.

★ Developer(s) : Salvatore Sanfilippo, Pieter Noordhuis
★ Initial release : April 10, 2009
★ Stable release : 2.8.12 / June 23, 2014
★ Development status : Active
★ Written in : ANSI C
★ Operating system : Cross-platform
★ Available in : English
★ Type : Key–value stores
★ License : BSD

Redis : What for?

★ Cache out-of-process
★ Duplicate detector
★ LIFO/FIFO Queues
★ Priority Queue
★ Distributed HashMap
★ UID Generator
★ Pub/Sub
★ Real-time analytics & chat apps
★ Counting Stuff
★ Metrics DB
★ Implement expires on items
★ Leaderboards (game high scores)
★ Geolocation lookup
★ API throttling (rate-limits)
★ Autocomplete
★ Social activity feed

Who's using Redis?

★ Twitter
★ Github
★ Weibo
★ Pinterest
★ Snapchat
★ Craigslist
★ Digg
★ StackOverflow
★ Flickr
★ commands per day
★ 500.000.000.000 reads per day
★ writes per day
★ 500+ servers 2000+ Redis instances!

key Features

★ Atomic operations
★ Lua Scripting
★ Pub/Sub
★ Transactions
★ Master/Slave replication
★ Cluster (with automatic sharding)*
★ Automatic failover (Redis Sentinel)
★ Append Only File (AOF) persistence
★ Snapshot (RDB file) persistence
★ Redis can handle up to 232 keys, and was tested in practice to handle at
least 250 million of keys per instance.
★ Every list, set, and sorted set, can hold 232 elements.

key Advantages & Disadvantages

► Advantages

★ Blazing Fast
★ Robust
★ Easy to setup, Use and maintain
★ Extensible with LUA scripting

► Disadvantages

★ Persistence consumes lot of I/O when using RDB
★ All your data must fit in memory

Redis in-memory

► 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. At the same time the two on-disk storage formats (RDB and AOF) don't need to be suitable for random access, so they are compact and always generated in an append-only fashion (Even the AOF log rotation is an append-only operation, since the new version is generated from the copy of data in memory).

What happens if Redis runs out of memory?

► Redis will either be killed by the Linux kernel OOM killer, crash with an error, or will start to slow down. With modern operating systems malloc() returning NULL is not common, usually the server will start swapping and Redis performances will degrade so you'll probably notice there is something wrong. The INFO command will report the amount of memory Redis is using so you can write scripts that monitor your Redis servers checking for critical conditions.

Redis Persistence

★ RDB is a very compact single-file point-in-time representation of your Redis data. RDB files are perfect for backups. For instance you may want to archive your RDB files every hour for the latest 24 hours, and to save an RDB snapshot every day for 30 days. This allows you to easily restore different versions of the data set in case of disasters.
★ RDB is very good for disaster recovery, being a single compact file can be transferred to far data centers, or on Amazon S3 (possibly encrypted).
★ RDB maximizes Redis performances since the only work the Redis parent process needs to do in order to persist is forking a child that will do all the rest. The parent instance will never perform disk I/O or alike.
★ RDB allows faster restarts with big datasets compared to AOF.

Redis Replication

► Redis replication is a very simple to use and configure master-slave replication that allows slave Redis servers to be exact copies of master servers. Here are some important facts about Redis replication:

★ Redis uses asynchronous replication.
★ A master can have multiple slaves.
★ Slaves are able to accept connections from other slaves.Slaves can also be connected to other slaves in a graph-like structure.
★ Redis replication is non-blocking on the master side.
★ Replication is also non-blocking on the slave side.
★ Replication can be used both for scalability, in order to have multiple slaves for read-only queries or simply for data redundancy.
★ It is possible to use replication to avoid the cost of writing the master write the full dataset to disk.

Supported Language

► Many languages that have Redis bindings, including : ActionScript, C, C++, C#, Clojure, Common Lisp, Dart, Erlang, Go, Haskell, Haxe, Io, Java, JavaScript (Node.js), Lua, Objective-C, Perl, PHP, Pure Data, Python, R, Ruby, Scala, Smalltalk and Tcl.

Redis Data Structure

► Redis is not a plain key-value store, actually it is a data structures server, supporting different kind of values. Here is a list all the data structures supported by Redis
★ Binary-safe strings.
★ Lists : collections of string elements sorted according to the order of insertion. They are basically linked lists.
★ Sets : collections of unique, unsorted string elements.
★ Sorted sets, similar to Sets but where every string element is associated to a floating number value, called score.
★ Hashes, which are maps composed of fields associated with values. Both the field and the value are strings. This are very similarl to Ruby or Python hashes.
★ Bit arrays : it is possible, using special commands, to handle String values like array of bits: th.
★ HyperLogLogs : this is a probabilistic data structure which is used in order to estimate the cardinality of a set.

Redis Quick Start

Installing Redis

► The suggested way of installing Redis is compiling it from sources as Redis has no dependencies other than a working GCC compiler and libc. You can either download the latest Redis tar ball from the redis.io web site, or you can alternatively use this special URL that always points to the latest stable Redis version, that is, http://download.redis.io/redis-stable.tar.gz. In order to compile Redis follow this simple steps :

★ wget http://download.redis.io/redis-stable.tar.gz
★ tar xvzf redis-stable.tar.gz
★ cd redis-stable
★ make

Installing Redis

► At this point you can try if your build works correctly by typing make test, but this is an optional step. After the compilation the src directory inside the Redis distribution is populated with the different executables that are part of Redis:

★ redis-server is the Redis Server itself.
★ redis-sentinel is the Redis Sentinel executable (monitoring and failover).
★ redis-cli is the command line interface utility to talk with Redis.
★ redis-benchmark is used to check Redis performances.
★ redis-check-aof and redis-check-dump are useful in the rare event of corrupted data files.
It is a good idea to copy both the Redis server and the command line interface in proper places, either manually using the following commands:
★ sudo cp redis-server /usr/local/bin/
★ sudo cp redis-cli /usr/local/bin/
Or just using make install.

Redis - Starting/Working/Shutdown

► Starting Redis :
The simple way to start the Redis server is to execute the redis-server binary without any argument : $ redis-server
► Check if Redis is working :
To check if Redis is working properly is sending a PING command using redis-cli : $ redis-cli ping

► Shutdown Redis : SHUTDOWN [NOSAVE] [SAVE]
★ SHUTDOWN SAVE will force a DB saving operation even if no save points are configured.
★ SHUTDOWN NOSAVE will prevent a DB saving operation even if one or more save points are configured

Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program