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)

Leveraging data types (Simple)


One of the categories in which Redis stands out as a clear winner is its support for data types. Support for lists and sets makes it appropriate to call Redis an advanced data-store or data-structure server. It also provides very powerful and efficient atomic operations for manipulating data in these data structures. The official website of Redis provides extensive information about all the data structures available. But here we will discuss only a few important data structures and their use cases.

We have already discussed how to use the basic data structure, strings. Now we will see how to use lists, sets, and hashes. We will discuss each data structure in detail in the next section.

How to do it...

  1. Open redis-cli or connect to the redis server through Telnet.

  2. All the commands will be issued in redis-cli.

  3. Let us create a list in Redis by using the following commands:

    LPUSH greet "Hi"
    (integer) 1
    LPUSH greet "Redis"
    (integer) 2
    LRANGE greet 0 -1
    1) "Hi"
    2) "Redis"
    LLEN greet
    (integer)2
    
  4. Now let us have a look at how to use sets:

    SADD set1 "1"
    (integer) 1
    SADD set1 "2"
    (integer) 1
    SADD set1 "3"
    (integer) 1
    SADD set2 "5"
    (integer) 1
    SADD set2 "3"
    (integer) 1
    SADD set2 "4"
    (integer) 1
    SUNION set1 set2
    1) "2"
    2) "3"
    3) "5"
    4) "1"
    5) "4"
    
  5. Hashes are more advanced data structures that open up lots of possibilities. Let us see how to use hashes:

    HSET hash1 field1 "Hi"
    (integer) 1
    HSET hash1 field2 "Redis"
    (integer) 1
    HGETALL hash1
    1) "field1"
    2) "Hi"
    3) "field2"
    4) "Redis"
    

There's more...

We have seen how to use data structures. Now let us see where to use the data structures, and the different commands possible in each of them.

Strings

Strings are the most basic data type in Redis, implemented using a dynamic string library written in C. This basic type is binary safe and used to create more complex data types such as lists and sets. A string value can hold up to 512 MB of data. The string value can be anything from the HTML contents of a page to a binary stream of an image file.

There are many useful commands available to use on strings. A few notable ones are INCR, DECR, INCRBY, SET, GET, and APPEND.

Tip

To know all the available commands in Redis, visit http://redis.io/commands.

Lists

Without the complex data types like lists and sets, Redis will be nothing but a MemCached server with persistence. In Redis, lists are nothing but a list of strings arranged based on the order of insertion. This ensures that the data can be accessed in the same order in which it was inserted. Internally, lists are implemented using Linked List, which has both pros and cons. The advantage is that adding an element in the head or tail of the list takes the same time. For example, adding an item to a list with 10 items or 10,000 items takes the same time, but the main disadvantage is that accessing an element in the middle of the list takes longer than accessing elements near the extremes. Being a database, it is critical to be able to add elements to a large list in an efficient way.

The LPUSH command is used to push new elements into the list and the LLEN command outputs the number of elements in the list.

Note

The lookup operation is O(N), where N is the index of the element.

Various operations available for list manipulation make it easy to model queues or stacks. The maximum number of elements that can be stored in a list is 232-1.

Sets

Redis sets are an unordered data collection of binary safe strings with no duplicate members. You should consider using sets when you have a collection of items and you need to add, delete, or verify the existence of members in a very efficient and fast manner. Another desired behavior is no duplication, and also support for peek and pop of elements (using the SRANDMEMBER and SPOP commands). Sets can also store up to 232-1 elements.

Note

Redis sets provide a constant time O(1) for all the mentioned operations, irrespective of the number of items you have in the set.

Sets also support complex operations like intersections and unions of members on the server side, which lets us perform transformations on the data before we get the output data. SADD is used to add an item into a set.

Note

Sets can be used to store indexes or inverted indexes, which are a critical part of any search system.

Sorted sets are introduced as a solution to a few shortcomings in sets, and are the most advanced data type in Redis. Similar to sets, sorted sets are also collections of non-repeating binary safe strings, but with a score associated to the member. The associated scores are used to sort the set from the smallest to the greatest score. We can add, delete, or update elements in a sorted set quickly, because elements are inserted in an order rather than ordered afterwards.

Note

Sorted sets achieve time complexity of O(log(N)).

Due to the highly optimized and efficient working of sorted sets, they can be used to maintain leader boards, timestamp range data, or to implement auto-completion with Redis. They make good priority queues and can be used to implement weighted random selections.

Hashes

Hashes are perfect to represent objects as a map between string fields and string values. Hashes are equivalent to a hash table or a hash map in Java. If we need to store some data related to an object and do not want to perform encoding (in JSON or XML, for example), hashes are the best suited. Hashes can be used to represent linked data structures using references.

Tip

Complex data structures can be created using hashes, by creating a hash with a reference to lists and sets.