Python Operation Redis

Source: Internet
Author: User
Tags couchbase percona server install redis redis server

The difference between a non-relational database and a relational database:

Benefits of non-relational databases:

1. Performance NoSQL is based on key-value pairs, which can be imagined as the corresponding relationship between the primary key and the value in the table, and does not need to be parsed by the SQL layer, so the performance is very high.

2. Extensibility is also due to the fact that there is no coupling between data based on key-value pairs, so it is easy to scale horizontally.

The benefits of a relational database:

1. Complex queries can easily use SQL statements to make very complex data queries between a table and multiple tables.

2. Transactional support enables data access requirements with high security performance. For these two types of databases, the other's advantage is their own weakness, and vice versa. But in recent years both of these databases have evolved in a different direction.

For example, NoSQL databases are slowly beginning to have some of the complex query functions of SQL database, such as Couchbase's index and MONGO's complex queries. Support for transactions can also use some system-level atomic operations to implement methods such as optimistic locking to curve the salvation. SQL database also began to evolve slowly, such as the implementation of Handlersocker technology, can be implemented on the SQL layer of MySQL penetration, in a nosql way to access the database, performance can be achieved or even beyond the NoSQL database. scalability, such as Percona Server, enables the implementation of a cluster that is not centralized. Although the Poles are beginning to evolve another pole because of their weaknesses, the increase in these features will also weaken the advantages they would otherwise have, such as the increase in index on the couchbase gradually reduces the read and write performance of the database. So how to build a system's short-and long-term storage strategy, using their strengths is an important issue that architects need to think about.

Python Operation Redis

The concept of Redis:

Redis is a key-value storage system. Similar to memcached, it supports storing more value types, including string (string), list (linked list), set (set), Zset (sorted set-ordered collection), and hash (hash type). These data types support Push/pop, Add/remove, and intersection-set and difference sets, and richer operations, and these operations are atomic. Based on this, Redis supports sorting in a variety of different ways. As with memcached, data is cached in memory to ensure efficiency. The difference is that Redis periodically writes the updated data to disk or writes the modified operation to the appended record file, and Master-slave (Master-Slave) synchronization is implemented on this basis.

Redis is a high-performance Key-value database. The emergence of Redis, to a large extent, compensates for the lack of memcached such key/value storage, in some cases can be a good complement to the relational database. It provides clients such as Java,c/c++,c#,php,javascript,perl,object-c,python,ruby,erlang, which is convenient to use.

Redis supports master-slave synchronization. Data can be synchronized from the primary server to any number of slave servers, from the server to the primary server that is associated with other slave servers. This enables Redis to perform single-layer tree replication. You can write to the data intentionally or unintentionally. Because of the full implementation of the publish/subscribe mechanism, you can subscribe to a channel and receive a complete message release record from the master server when the tree is synchronized anywhere from the database. Synchronization is helpful for the scalability and data redundancy of read operations.

Installation of Redis

Redis is typically installed on a Linux system with the following installation steps:

#cd/USR/LOCAL/SRC

#wget http://download.redis.io/releases/redis-3.0.1.tar.gz

#tar xzf redis-3.0.1.tar.gz

#cd redis-3.0.1

#make

#src/redis-server &

Check if Redis starts properly

Ps–ef |grep Redis

NETSTAT–LNP |grep 6379

Python Module for installing Redis

Pip Install Redis

Redis is stored in the form of key-value, so when we operate. First we instantiate the IP and publish port of the host on the Redis as an object R, then execute set (' Hello ', ' world '), so we store a key in memory as Hello, the value of ' world '. We can understand {' Hello ': ' World '}, and when we want to read it, keys () is getting more than the key value.

Example:

Import Redis

R = Redis. Redis (host= "192.168.1.1", port=6379)

R.set ("Hello", "World")

Print (R.get ("Hello"))

Print (R.keys ())

# Print (dir (r))

2. Connection pooling

Redis-py uses connection pool to manage all connections to a Redis server, avoiding the overhead of each establishment and release of the connection. By default, each Redis instance maintains its own pool of connections. You can create a connection pool directly, and then as a parameter Redis, you can implement multiple Redis instances to share a single connection pool

Example:

Import Redis

Pool = Redis. ConnectionPool (host= "192.168.1.1")

R = Redis. Redis (Connection_pool=pool)

R.set ("Python", "study")

Print (R.get ("Python"))

Pipeline

Redis-py The default is to create each request (Connection pool request connection) and disconnect (return connection pool) One connection operation, if you want to specify more than one command in a single request, you can use Pipline to implement a single request to specify multiple commands, and by default Pipline is an atomic operation. Reduced power consumption

Redis is a CS-mode TCP server that uses a request-response protocol similar to HTTP. A client can initiate multiple request commands from a single socket connection. After each request command is issued, the client usually blocks and waits for the Redis service to process, and the result is returned to the client via a response message after Redis finishes processing the request. The basic communication process is as follows:

CLIENT:INCR X

Server:1

CLIENT:INCR X

Server:2

CLIENT:INCR X

Server:3

CLIENT:INCR X

Server:4

Basically four commands require 8 TCP messages to complete. Due to the network latency of the communication, the packet transfer time between the client and server takes 0.125 seconds. Then the above four commands 8 messages will take at least 1 seconds to complete. This way, even though Redis can handle 100 commands per second, our client can only issue four commands in a second. This shows that the processing power of Redis is not fully utilized. In addition to the ability to handle multiple key commands with a single command such as Mget,mset, we can also use pipeline to send out multiple commands from the client, without waiting for the response of a single command to return. The Redis server then processes multiple commands and packages the results of multiple commands back to the client. The communication process is as follows:

CLIENT:INCR X

CLIENT:INCR X

CLIENT:INCR X

CLIENT:INCR X

Server:1

Server:2

Server:3

Server:4

Assume that the TCP message is not split because it is too long. Perhaps two TCP messages can complete four commands, the client can put four commands in a TCP message sent together, the server can put the processing results of four commands to a TCP message return. By pipeline mode when there is a large amount of operation. We can save a lot of time originally wasted on network latency. Note that the pipeline Package command is sent, and Redis must cache all command processing results before all commands are processed. The more commands are packaged, the more memory is consumed by the cache. So the more you pack the more commands the better. Specific how much appropriate needs to be tested according to specific circumstances

Example:

Import datetime

Import Redis

def withpipe (R):

Pipe = R.pipeline (transaction=true)

For I in xrange (1, 1000):

Key = "Test1" +str (i)

Value = "Test1" + str (i)

Pipe.set (key, value)

Pipe.execute ()

def withoutpipe (R):

# pipe = R.pipeline (transaction=true)

For I in xrange (1, 1000):

Key = "Test1" + str (i)

Value = "Test1" + str (i)

R.set (key, value)

if __name__ = = "__main__":

Pool = Redis. ConnectionPool (host= "192.168.1.1", port=6379, Db=0)

R1 = Redis. Redis (Connection_pool=pool)

r2 = Redis. Redis (Connection_pool=pool)

Start = Datetime.datetime.now ()

# print (start)

Withpipe (R1)

End = Datetime.datetime.now ()

# Print ((End-start). microseconds)

# Print (End-start)

T_time = (end-start). microseconds

Print ("Withpipe time is: {0}". Format (T_time))

Start = Datetime.datetime.now ()

Withoutpipe (R2)

End = Datetime.datetime.now ()

T_time = (end-start). microseconds

Print ("Withoutpipe time is: {0}". Format (T_time))

Results:

Withpipe Time is:28000

Withoutpipe Time is:253000


Python Operation Redis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.