Reasons to use Redis

Source: Internet
Author: User
Tags failover redis server

  

Redis is a remote in-memory database that not only has strong performance, but also has replication features and a unique data model for solving problems. Redis provides 5 different types of data structures, all of which can be easily mapped to these data structures: Redis's data structures are designed to help users solve problems without requiring users to distort the problem to fit the database, as other databases do. In addition, features such as replication, persistence (persistence), and client-side sharding (client-side sharding) make it easy for users to extend Redis into a system that can contain hundreds of GB of data and process millions of requests per second.

The first time I used Redis was in a company where the company needed to search a relational database with 60,000 customer contacts, which could take 10-15 seconds per search, based on name, email address, location, and phone number. After spending a week learning the basics of Redis, I used Redis to rewrite a new search engine and then spend a few weeks testing the new system to get it to production level, and eventually the new search system could not only be based on name, email address, Information such as location and phone numbers to filter and sort customer contacts, and each operation can be completed within 50 milliseconds, which is 200 times times faster than the original search system.

1. Introduction to Redis

The previous description of the Redis database speaks only part of the truth. Redis is a very fast non-relational database (non-relational) that stores the mapping between key (key) and 5 different types of values (value) (mapping), which can persist data stored in memory to the hard disk, You can use replication features to extend read performance, and you can use client shards to extend write performance.

2. Reasons for using Redis

Readers with memcached experience may know that users can only add data to the end of an existing string with the append command. The memcached document declares that you can use the Append command to manage the list of elements. This is good! The user can append an element to the end of a string and use that string as a list. But then how do you delete these elements? Memcached uses the Blacklist (blacklist) to hide the elements in the list, thus avoiding operations such as reading, updating, writing to elements (including Memcached writes after a database query). In contrast, Redis's list and set allow the user to add or remove elements directly.

Using Redis instead of memcached to solve the problem not only makes the code shorter, easier to understand, easier to maintain, but also makes the code run faster (because users do not need to read the database to update the data). In addition, in many other cases, Redis's efficiency and ease of use are much better than relational databases.

A common use of a database is to store long-term reporting data and use these report data as aggregated data over a fixed time range (aggregates). A common practice for collecting aggregated data is to insert individual rows into a report table before they are scanned to collect aggregated data and to update the rows that are already in the aggregation table based on the aggregated data that is collected. The insert row is used for storage because the Insert row operation is very fast for most databases (the Insert row is written only at the end of the hard disk file). However, updating the rows in a table is a fairly slow operation, because this update may cause a random write, in addition to a random read, which can cause an arbitrary reading. In Redis, users can compute aggregated data directly using the Atomic (atomic) INCR command and its variants, and because Redis stores the data in memory 2, and the command request sent to Redis does not need to be processed by a typical query parser (parser) or the query optimizer (optimizer), so it is always very fast to perform random writes on Redis stored data.

Using Redis instead of relational databases or other hard disk storage databases avoids the need to write unnecessary temporary data, eliminates the hassle of scanning or deleting temporary data, and ultimately improves the performance of the program. While these are some simple examples, they are a good testament to the fact that "tools can dramatically change the way people solve problems."

3. Comparison of Redis with other databases and software

If you are familiar with relational databases, you must have written SQL queries to correlate data from two tables. Redis is a NoSQL database or a non-relational database that people often say: Redis does not use tables, its databases are not pre-defined or mandatory to require users to correlate different data for redis storage.

High-performance Key-value cache server memcached is also often used to compare with Redis: Both are available to store key-value mappings and have similar performance to each other, but Redis can automatically write data to the hard disk in two different ways, and Redis, in addition to storing normal string keys, You can also store 4 other data structures, and memcached can only store normal string keys. These differences enable Redis to address a broader range of issues and can be used as a primary database (primary) and as a secondary database for other storage systems (auxiliary).

The subsequent chapters of this book describe usage and query patterns when using Redis as primary storage (primary storage) and level two storage (secondary storage). In general, many users will only store data in Redis if the performance or functionality of the Redis is necessary: If the program does not have a high performance requirement, or if there is no way to store large amounts of data in memory for cost reasons, then the user may choose to use a relational database. or other non-relational databases. In practice, readers should decide whether to use Redis based on their own needs and consider using Redis as primary or secondary storage, and how to ensure data integrity through replication, persistence, and transactions. Shows a subset of the database servers and cache servers that are functionally overlapping with redis, and the differences between Redis and these databases and software can be seen from the table below.

Features and functionality of some database and cache servers

Name

Type

Data storage Options

Query type

Additional Features

Redis

Non-relational database using memory storage (in-memory)

strings, lists, collections, hash lists, ordered collections

Each data type has its own commands, plus bulk operations (bulk operation) and partial transaction support

Publish and subscribe, master-slave Replication (master/slave replication), persistence, scripts (stored procedures, stored procedure)

Memcached

Using the memory store's key-value cache

Mapping between key values

Create commands, read commands, update commands, delete commands, and several other commands

Multi-threaded server for improved performance

Mysql

relational database

Each database can contain multiple tables, each of which can contain multiple rows, a view that can handle multiple tables, spatial support (spatial), and third-party extensions

SELECT, INSERT, UPDATE, DELETE, function, stored procedure

Supports ACID properties (requires use of InnoDB), master-slave replication and primary master replication (Master/master replication)

PostgreSQL

relational database

Each database can contain more than one table, each table can contain multiple rows, a view that can handle multiple tables, support for space and third-party extensions, and customizable types

SELECT, INSERT, UPDATE, DELETE, built-in functions, custom stored Procedures

Supports ACID properties, master-slave replication, multi-master replication supported by third parties (multi-master replication)

Mongodb

Non-relational document storage using hard disk storage (On-disk)

Each database can contain more than one table, and each table can contain multiple Bson documents without schema (schema-less)

Create commands, read commands, update commands, delete commands, conditional query commands, and more

Supports map-reduce operations, master-slave replication, sharding, spatial index

4. Redis Additional features

One of the first things to consider when using a memory database like Redis is "where does the server store data when the server is shut down?" "Redis has two different forms of persistence that can write data stored in memory to the hard disk in a small, compact format: The first persistence method is a point-in-time dump (Point-in-time dump), and the dump operation can be executed in a specified number of write operations over a specified period of time. This condition is fulfilled when executed, and can be executed by invoking any one of the two dumps to the hard disk (dump-to-disk) command; The second persistence method writes all commands that modify the database to an append-only (append-only) file, which the user can base on the importance of the data , the append-only write is set to never sync (sync), one time per second, or one command per write. We'll discuss these persistence options in more detail in chapter 4th.

Also, although Redis performs well, it is limited by the Redis memory storage design, and sometimes using only one Redis server may not be able to handle all requests. Therefore, in order to extend the read performance of Redis and provide failover (failover) support for Redis, Redis implements the master-slave replication feature: the From server that performs the replication connects to the primary server, receiving an initial copy of the entire database sent by the primary server (copy) , then the master server executes a write command, which is sent to all connected slave servers to execute, thus updating the data set from the server in real time. Because the data contained from the server is constantly being updated, clients can send read requests from the server to any one to avoid centralized access to the primary server. We will discuss the Redis slave server in more depth in the 4th chapter.

Links: http://www.epubit.com.cn/article/200

Reasons to use 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.