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)

Using languages and drivers (Simple)


All the major programming languages have support for Redis through their client libraries. Even if your language of preference does not have a client library to connect and use Redis, it is fairly easy to write a basic client of your own, as the Redis protocol is simple to implement. So, in this recipe, we will cover some clients worth mentioning for commonly used programming languages.

Tip

It is important to update the client libraries when we update the Redis server to get support for new commands and features.

Let us see how to use client libraries to connect to Redis. As part of this recipe, we will see how we can use redis-rb to connect to Redis using Ruby.

How to do it...

  1. You need to download the redis-rb client library from the official GitHub page (https://github.com/redis/redis-rb).

  2. Create a new Ruby script that will connect to the Redis server. The following code should make a connection to the server, assuming the server is running on the local machine at port 6379:

    require "redis"
    redis = Redis.new(:host => "127.0.0.1", :port => 6379)
    
  3. After connecting, we can execute the following commands:

    redis.set("redis", "rocks")
    redis.get('redis')
    
  4. Close the connection once done.

    redis.quit
    

There's more…

You can find multiple libraries that provide almost similar functionalities; the complete list of libraries available for Redis can be found on the official Redis website (http://redis.io/clients). Here we were focusing on stable, active, recommended, and feature-complete libraries only. An official supported library is available only for C; all the libraries for other languages were written and are maintained by the community or external developers.

Client Libraries

The following table has the recommended libraries for popular programming languages (which are stable enough to be used in production):