Redis: The Key-Value NoSQL Database

Redis: The Key-Value NoSQL Database

Introduction

Redis is a key-value NoSQL database that has become a mainstream player in the industry. Similar to Memcached, it supports a wide range of stored value types, including strings, lists, sets, sorted sets, and hashes. These data types are supported with rich operations, such as push, pop, add, remove, intersection, union, and difference, all of which are atomic. On top of this, Redis supports various ways of sorting, making it an efficient and powerful tool for caching data in memory.

Advantages

  1. Abnormally Fast: Redis is extremely fast, with a setting operation rate of about 110,000 per second and a read operation rate of 81,000 per second.
  2. Rich Data Types: Redis supports a wide range of data types, including lists, sets, collections, and hashes, making it easy to solve problems in applications.
  3. Atomic Operations: All Redis operations are atomic, ensuring that when two clients simultaneously access the Redis server, they get the latest value after the update.
  4. Multi-Utility Tool: Redis is a versatile utility that can be used for various tasks, such as caching, messaging queues, and transient data storage.

Installation

To install Redis on CentOS, follow these steps:

  1. Download the Redis package: wget http://download.redis.io/releases/redis-4.0.9.tar.gz
  2. Extract the package: tar xvf redis-4.0.9.tar.gz
  3. Build and install Redis: cd redis-4.0.9/; make && make install
  4. Start the Redis server: /mnt/redis-4.0.9/src/redis-server /mnt/redis-4.0.9/redis.conf &

Redis API

Redis supports five data types: strings, hashes, lists, sets, and sorted sets. The Redis-py API provides two classes for implementing Redis: Redis and StrictRedis. The Redis class is a subclass of StrictRedis and is used for backward compatibility with older versions of Redis-py.

Connecting to Redis

To connect to Redis using Redis-py, you can use the following code:

import redis
r = redis.Redis(host='192.168.10.128', port=6379)

Connection Pool

Redis-py uses a connection pool to manage all connected Redis servers, avoiding the overhead of establishing and releasing connections. By default, each instance of Redis will maintain its own connection pool.

Redis Operations

String Operations

Redis provides a range of string operations, including:

  • SET: Set the string value of a key.
  • GET: Get the string value of a key.
  • MSET: Batch set the string values of multiple keys.
  • MGET: Batch get the string values of multiple keys.
  • GETSET: Set the new value and return the original value.
  • GETRANGE: Get a substring of a string value.
  • SETRANGE: Modify a substring of a string value.
  • SETBIT: Set a binary bit value in a string.
  • GETBIT: Get a binary bit value from a string.
  • STRLEN: Return the byte length of a string.

Example:

r.set('name', 'xiaogang')
print(r.get('name'))  # Output: "xiaogang"

Hash Operations

Redis provides a range of hash operations, including:

  • HSET: Populate fields in a hash table.
  • HMGET: Return one or more values from a hash table.
  • HEXISTS: Check if a field exists in a hash table.
  • HDEL: Delete a field from a hash table.
  • HKEYS: Return all field names in a hash table.
  • HGETALL: Return all fields and values in a hash table.
  • HINCRBY: Increment a field value in a hash table.
  • HINCRBYFLOAT: Increment a field value in a hash table.

Example:

r.hset('info', 'name', 'lisi')
r.hset('info', 'age', 24)
r.hset('info', 'sex', 'male')
print(r.hmget('info', 'name', 'age', 'sex'))  # Output: ["lisi", "24", "male"]

List Operations

Redis provides a range of list operations, including:

  • LPUSH: Push a value to the left of a list.
  • RPUSH: Push a value to the right of a list.
  • LPOP: Pop a value from the left of a list.
  • RPOP: Pop a value from the right of a list.
  • LLEN: Return the length of a list.

Example:

r.lpush('list', 'value1')
r.rpush('list', 'value2')
print(r.lpop('list'))  # Output: "value1"

Set Operations

Redis provides a range of set operations, including:

  • SADD: Add a value to a set.
  • SREM: Remove a value from a set.
  • SCARD: Return the number of elements in a set.
  • SISMEMBER: Check if a value is a member of a set.

Example:

r.sadd('set', 'value1')
r.srem('set', 'value1')
print(r.sismember('set', 'value1'))  # Output: False

Sorted Set Operations

Redis provides a range of sorted set operations, including:

  • ZADD: Add a value to a sorted set.
  • ZREM: Remove a value from a sorted set.
  • ZCARD: Return the number of elements in a sorted set.
  • ZSCORE: Return the score of a value in a sorted set.
  • ZRANK: Return the rank of a value in a sorted set.

Example:

r.zadd('sorted_set', {'value1': 1})
r.zrem('sorted_set', 'value1')
print(r.zscore('sorted_set', 'value1'))  # Output: None