Book Image

Instant Redis Optimization How-to

By : Arun Chinnachamy
Book Image

Instant Redis Optimization How-to

By: Arun Chinnachamy

Overview of this book

The database is the backbone of any application and Redis is a next generation NoSQL database which can provide ultra-fast performance if tuned and calibrated correctly. Instant Redis Optimization How-to will show you how to leverage Redis in your application for greater performance and scalability. Instant Redis Optimization How-to will show you how to make the most of Redis. Using real-world examples of Redis as a caching and queuing service, you will learn how to install and calibrate Redis to optimize memory usage, read and write speed, as well as bulk writes and transactions. If you want to use Redis for its blazing fast capabilities, then this book is for you.Instant Redis Optimization How-to shows you how to optimize and scale Redis with practical recipes on installation and calibration for performance and memory optimization as well as advanced features like PUB/SUB. This book starts by providing clear instructions on how to install and fine-tune Redis to work efficiently in your application stack. You will also learn how to maintain persistence, how to optimize Redis to handle different data types, as well as memory usage optimization. You will then learn how to use bulk writes and transactions, as well as publish/subscribe features to get the most out of Redis. Offering best practices and troubleshooting tips, this book will also show you how to manage and maintain performance. This book finishes by recommending the best client libraries for all major programming languages. By the end of this book you will know how to create blazing fast applications using Redis.
Table of Contents (7 chapters)

Installing Redis (Simple)


Before we start learning about Redis and how we can leverage on its fast performance, we need to install and have a working server. The installation of Redis is easy and straightforward.

Getting ready

In order to install, we need a computer system with a working Linux environment or a Mac OS X. If using Linux, I assume you have installed the Debian or Ubuntu operating system for this book. For the other operating system, you can refer to the There's more section of this recipe. As the Debian or Ubuntu packages are usually outdated, we will see how to install the Redis server from the source. Make sure you are using the latest stable version, as Redis is under active development and new commands and improvements pop out in every version. There are no dependencies for the Redis server except libc, which will be available in your system by default. But to build the server from the source, you need the compiler.

How to do it...

  1. We will start with installing the build tools which are necessary to build Redis from the source. Open the terminal program and execute the following command:

    # sudo apt-get update
    
  2. Once the command executes, download the compiler with build essentials, which is a package of all the common build tools to build the Redis server from the source.

    # sudo apt-get install build-essential
    
  3. Now we have all the components to build the source. The following URL always has the latest stable build (2.6.13), which eliminates the confusion of version numbers:

    # wget http://download.redis.io/redis-stable.tar.gz
    # tar xvzf redis-stable.tar.gz
    # cd redis-stable
    # make
    # sudo make install
    

How it works...

Building a Redis server from the source is easy and it gets the latest version. The preceding steps will download the latest version of Redis from the repository, compile the C source, and install them in the machine. In this section, we will understand about various executables generated in this process.

You can install any unstable builds using the same procedure.

Tip

Caution

Be aware that the unstable builds usually have bugs and are not for production environments, so do not use them unless you are sure about what you are doing.

On successful execution of the preceding procedure, the following binaries are built from the source:

  • redis-server: The server executable

  • redis-cli: The command-line interface for working with the Redis server

  • redis-benchmark: For performance benchmarking and to test your setup

  • redis-check-aof: For debugging corrupt data files

  • redis-check-dump: For debugging corrupt data files

There's more...

Now that we have built the Redis binaries from the source, we will see how to set up the Redis server with configuration files and test the installation.

Setting up and testing

The make install command copies all the executables to the/usr/local/bin folder. The configuration files and init scripts are not yet in place to run Redis.

  1. You can run the install_server.sh script, which can be found inside the utils folder of the Redis source. In the terminal, go to the utils folder inside redis-stable and execute the following command:

    # ./install_server.sh
    
  2. The script will produce a series of questions about the path to where the configuration files are to be kept. If you are not sure, just press the Enter key (Return) to keep the default value. Once the installation is completed, the Redis server will be started by the script.

  3. Now the server is running with the configuration available in the file /etc/redis/6379.conf. Run redis-cli to test the server in another terminal window.

    # redis-cli
    
  4. If the Redis server is installed and is running, the prompt will look something like this:

    redis 127.0.0.1:6379>
    
  5. You can issue a couple of commands to get the feel of working with the Redis server:

    SET Android Google
    SET Windows Microsoft
    SET iOS Apple
    
  6. The preceding commands will create keys of type string (Android, Windows, and iOS) and set their values to their respective company names. To check the server, issue the GET command using the keys created to retrieve their values.

    GET Android
    Google
    GET iOS
    Apple
    QUIT
    
  7. The QUIT command closes the connection from the server. To stop the server, issue the following command. Using shutdown makes sure that Redis saves the data to the disk before quitting gracefully.

    # redis-cli shutdown
    

Now we have a working Redis server with the basic configuration.

Redis in Mac OS X

If you are using Mac OS X, you can install Redis from the source or by using Homebrew. The installation process from the source is the same as we did for Debian. So we will see how to install Redis in Mac using Homebrew. You should have OS X command-line tools installed in the system for Homebrew to work. If you do not have the command-line tools installed, you can download them from the Apple Developer website (http://connect.apple.com/).

  1. To install Homebrew, execute the following command in the terminal:

    ruby <(curl -fsSk https://raw.github.com/mxcl/homebrew/go) 
    
  2. To check the successful installation of Homebrew, run the following command:

    brew doctor 
    
  3. You are one step away from installing Redis. Execute the following command:

    brew install redis
    

Homebrew installs and configures the server. You can test the installation by running redis-cli and issuing commands to the server.

Redis in Windows

If you are a loyal user of the Windows operating system, you have hit a hurdle. Unfortunately, Redis is not officially supported in Windows 32- or 64-bit systems. But there are various ports available that are not of production quality and can be used only for development purposes. As Redis is not natively supported in Windows, the performance of ported builds is not even close to its performance in Linux. But if you do not have access to Linux systems, the compiled packages can be downloaded from the GitHub page of the officially recommended Windows port (https://github.com/dmajkic/redis/downloads).

As it is not officially supported for Windows, the book will focus on the Linux installation, and using a Linux system is strongly recommended.