Redis learning objectives

Introduction and installation of Redis

What is Redis?

Redis is an open source (BSD-licensed), in-memory data structure storage system that can be used as a database, cache, and messaging middleware. It supports many types of data structures, such as strings (hashes), lists (Lists), sets (sets), sorted sets (sorted sets) and range queries, bitmaps, Hyperloglogs and Geospatial index radius query. Redis is built with replication, LUA scripting, LRU eviction, transactions and different levels of disk persistence, And high availability through Redis Sentinel and Automated Cluster

performance

Here are the official bench mark numbers:

The test completed 50 concurrent execution of 100,000 requests.

The value set and obtained is a 256-byte string.

Results: The reading speed is 110,000 times /s, and the writing speed is 81,000 times /s

Brief history of Redis

In 2008, Salvatore Sanfilippo, the founder of Merzia, an Italian start-up company, created a database in order to avoid the low performance of MySQL and developed it in 2009. This database is Redis.

As of March 15, 2010, the development of Redis is hosted by VMware.

Development of Redis has been sponsored by Pivotal since May 2013.

Note: Pivotal is a new company formed by EMC and VMware **. Pivotal seeks to provide a native foundation for a new generation of applications, built on the transformational IT characteristics of leading cloud and web companies. Pivotal’s mission is to advance these innovations for enterprise IT architects and ISPs.

Support language

Supported data types

String, hash, list, set, sorted set

The installation

Download address

redis.io/

Uploading to the Server

Unpack the

The tar ZXVF redis - 5.0.3. Tar. GzCopy the code

Install dependencies

yum -y install gcc-c++ autoconf automake
Copy the code

precompiled

Switch to the decompression directory

CD redis - 5.0.3 / makeCopy the code

The installation

Creating an installation directory

mkdir -p /usr/local/redis
Copy the code

Do not use: make install (make install default installation directory /usr/local/bin)

Use: If you need to specify the installation path, add the PREFIX parameter

make PREFIX=/usr/local/redis/ install
Copy the code

Installation successful Figure

Redis – cli: the client

Redis-server: indicates the server

Start the

The default installation path is /usr/local/redis/bin

Start the

./redis-server
Copy the code

Change the default foreground boot to background boot

Copy redis.conf to the installation path

cp redis.conf /usr/local/redis/bin/
Copy the code

Conf in the installation directory and change daemonize to yes

During startup, specify the configuration file path

Access from a Windows client

Install the Redis client

Establishing a connection -> failed

Modify the redis.conf configuration file

Comment out bind 127.0.0.1 to enable all IP addresses to access Redis. If you want to specify multiple IP addresses, but not all IP addresses, you can set bind

Disable the protection mode and change it to no

Adding Access Authentication

After the modification, kill -9 XXXX Kills the Redis process and restarts redis

Connection established again -> successful

We can change the default number of databases to 16

If you change database 32, the default value is 32

After the modification, kill -9 XXXX Kill the Redis process and restart the redis process

Relational and non-relational databases

Relational database

A database is organized by a relational model, which is a two-dimensional table model. A table name in a two-dimensional table is a relation, a row in a two-dimensional table is a record, and a column in a two-dimensional table is a field.

advantages

  • Easy to understand
  • Easy to use, universal SQL language
  • Easy to maintain, rich integrity (entity integrity, referential integrity, and user-defined integrity) greatly reduces the probability of data redundancy and data inconsistencies

disadvantages

  • Disk I/O is a concurrency bottleneck
  • Massive data query is inefficient
  • Horizontal expansion is difficult. It is impossible to expand performance and load capacity simply by adding hardware and service nodes. When upgrading and expanding the database, maintenance and data migration need to be stopped
  • Associated query of multiple tables and complex SQL query of complex data analysis type have poor performance. Because acid must be designed in three normal forms.

The database

Orcale, Sql Server, MySql, DB2

Non-relational databases

Non-relational, distributed, and generally not guaranteed to follow the ACID principle of data storage systems. Key-value pair storage, structure is not fixed.

advantages

  • Add fields as needed, no need for multiple table lookup. You only need to fetch the value of the id
  • It applies to SNS (social network service software). Facebook, Twitter)
  • Not strictly a database, but a collection of structured ways to store data

disadvantages

  • Only suitable for storing some relatively simple data
  • Data not suitable for complex queries
  • Not suitable for persistent storage of large amounts of data

The database

  • K-v: Redis, Memcache
  • Documents: the mongo
  • Search: Elasticsearch, Solr
  • Scalability Distributed: HBase

To compare

content Relational database Non-relational databases
The cost of Some require Orcale. It’s basically open source
Query data The storage speed is slow Data is stored in cache, fast
Storage format Only basic types are supported K-v, documents, pictures, etc
scalability Multi-table query mechanism, difficult to expand There is no coupling between the data and it is easy to expand
persistence Suitable for persistent storage, mass storage Not suitable for persistent storage, mass storage
Data consistency Strong transaction ability, emphasis on data consistency Weak transaction capability and emphasis on final consistency of data

Redis – cli Redis operation

Redis – cli Redis connection

-h: Specifies an IP address

-p: indicates the specified port

-a: Indicates an authentication password

PING returns PONG

Specify the database

Redis – cli Redis operation

Operating the String

Set: Adds a String of data

Get: Gets a String of data

Mset: Adds multiple String data

Mget: Obtains multiple String data

The hash operation

Hset: Adds a hash data

Hget: Obtains a hash data

Hmset: Adds multiple hash data

Hmget: Obtains multiple hash data

HgetAll: Gets data of the specified hash type

Hdel: Deletes one or more hash data.

Operating the list

Lpush: Left adds (header)list data

Rpush: Add (tail) type data right

Lrange: obtains data of the list type. Start Start subscript End End subscript inclusion relationship

Llen: Obtains the number of entries

Lrem: Deletes several specified list types from a list

Operation of the set

Sadd: Adds data of the set type

Smembers: Gets data of the set type

Scard: Obtains the number of entries

Srem: Deletes data

Operating sorted set

A sorted set is sorted by score value. The higher the score value, the further it goes.

Zadd: Adds sorted set data

Zrange: Gets sorted set data

Zcard: Obtains the number of cards

Zrem: Deletes data

Zadd needs to place a Float or Double fractional value argument before the value argument

Redis stores data in a hierarchical, directory form

Set the key expiration time

Redis has four different commands that can be used to set the lifetime of the key (how long the key can exist) or the expiration time (when the key will be removed) :

EXPlRE

< TTL > : Used to set the lifetime of the key to TTL seconds.

PEXPIRE

< TTL >: used to set the lifetime of the key to TTL milliseconds.

EXPIREAT

< timestamp>: used to set the expiration time of the key to the timestamp specified in seconds.

PEXPIREAT

< timestamp > : Used to set the expiration time of the key to the number of milliseconds timestamp specified.

TTL: If the value obtained is -1, no validity period is set for the key. If the value is -2, the validity period has passed.

Methods a

Method 2

Methods three

The first parameter: key

The second argument: value

NX is set if it does not exist, XX is set if it does exist

The fourth argument: EX is second, PX is millisecond

delete

Del: Used to delete data (general, for all data types)

Hdel: used to delete hash data

Tips: Command is Java Chinese method name, parameter: remove parentheses, quotation marks, change commas to Spaces

Zadd needs to place arguments of type Float or Double before value arguments

Redis learning video!!