Section 1 redis components: 1 - 3, web development history and redis Profile

Redis lesson plans

1, A Brief History of the development of NoSQL databases

1, changes in the history of web system

 

About web1.0 era

Basically render some simple static pages, it does not involve too many complex business logic, simple single function, basically server performance will not have too much pressure

Shortcomings: 1, Service, more and more complicated relations calls, setting up a local front-end environment is no longer a simple matter. Consider teamwork, we tend to consider building a centralized development server to resolve. This solution maybe okay, but the front-end development is not friendly to the back-end development compiled for. God, I just want to adjust the next button styles, have local development, code upload, validate several steps to take effect and so on. Perhaps accustomed to it is also good, but not always so stable development server, often need to rely on the back-end developers get when things go wrong. Only the front-end development seems difficult to localize, but the impact on the efficiency of R & D is actually quite large.

2, maintainability JSP code, etc. getting worse. JSP is very powerful and can be embedded in Java code. This powerful front and rear ends so that responsibilities are not clear, JSP into a gray area. Often in order to catch the project, for a variety of urgent needs, it will rub a lot of miscellaneous business code in the JSP. When accumulated to a certain stage, they tend to bring a lot of maintenance costs.

 

About web2.0 era

 

 

 

With the era of Web2.0, greatly enhance the user visits, while producing a large amount of user data. Coupled with the popularity of smart mobile devices later, all Internet platforms are facing a huge challenge performance. Including web server CPU and memory pressure. The database server IO pressure

 

In order to solve the problem of server performance pressure, there has been a wide variety of solutions, the most typical is the use of MVC architecture, MVC is a very good cooperative mode, from the architectural level allows developers to know what code should be written in any place. In order to make easier the View layer altogether, you can also choose Velocity, Freemaker and other templates, making the template can not write Java code. Function appears weaker, but it is this limitation that the front and rear ends clearer division. But it will also face the following problems

 

1, the front end of the development of severe dependence development environment. In this architecture, the front and rear ends of collaboration has two modes: one is to write front-end demo, written after, to make the back-end sets of templates. Taobao is now still early, including a large number of business lines is this pattern. The benefits are clear, demo locally developed very efficient. Lack of back-end sets of templates is also needed, it is possible to set wrong, after also need to set the front end to determine the cost of back and forth communication adjustment is relatively large. Another collaboration model is responsible for front-end browser-side View layer template development for all development and server-side, Alipay is this pattern. UI benefits are related to the front-end code is like to write, do not be too concerned about the back-end, front-end development is the lack of binding heavy back-end environment, the environment has become an important factor affecting the efficiency of front-end development.

 

2, the front and rear ends of responsibility still tangled. Velocity template is still pretty strong, variables, logic, macros, and other features, can still be achieved through a variety of business logic context variable to get. In this way, a little weak as long as the front-end, back-end often will be asked to write a lot of business code in the template layer. There is a big gray area is the Controller, page routing features such as front-end should be most concerned about, but it is implemented by the backend. Controller and Model itself will often tangled, people looked Code teeth often appear in the Controller layer. These problems can not all be attributed to the quality of programmers, or JSP enough.

 

How to solve the load pressure on a Web server, the most common way is to use the service and forwarding services to achieve the split nginx web cluster, etc.

 

 

 

But this will be a problem, how to solve the session sharing problem, and the problem session storage across multiple back-end servers, and so on tomcat

In order to solve the problem of storing session, there are a variety of solutions

Option One: stored in a cookie inside. Insecurity, negative

Option Two: store them in a file or database. Slow

Option Three: session replication. A large number of session redundancy, waste of large node

Option 4: Use NoSQL database cache. For example redis or memcache like the perfect solution

 

 

NoSQL application scenarios

 

  • Concurrent reading and writing of data with a high
  • Reading and writing of massive data
  • High data scalability
  • Fast enough to quickly access data

NoSQL NA scene

  • Need transaction support
  • Sql structured query based storage, handling complex relationships requires ad hoc queries (user-defined query to the query).

 

 

Summary: do not need sql and sql with a situation does not work, please consider using NoSql

2, NoSQL database Brotherhood

1, memcache Introduction

  • Early emergence of NoSql database
  • All data in memory, generally do not persist
  • It supports simple key-value mode
  • Generally as an auxiliary cache database persistence database
  • Almost covering most of the functionality Memcached
  • All data in memory, support for persistence, mainly used for backup and recovery
  • In addition to supporting the simple key-value mode, also supports a variety of data structures, such as the list, set, hash, zset like.
  • Generally as an auxiliary cache database persistence database
  • Now available in the market with very much an in-memory database
  • High-performance, open source, free mode (schema free) document database
  • All data in memory, if enough memory to save less frequently used data to the hard disk
  • Although the key-value model, but to value (in particular, json) provides a rich query capabilities
  • Support for binary data and large object
  • RDBMS may be substituted according to the characteristics of the data, an independent database. Or with RDBMS, storing specific data.
    • HBase is the Hadoop project database. It requires a large number of random data, the real-time scene in the read and write operations. HBase goal is to deal with very large amount of data tables, can be more than one billion rows of data with ordinary computer processing, but also processing several million rows of data table of elements.

2, redis Introduction

3, mongoDB Introduction

4, columnar storage HBase Introduction

3, Redis basic introduction and use scenarios

redis official website address:

https://redis.io/

Chinese website

http://www.redis.cn/

Basic introduction 3.1, redis of

 

 Redis is one of the more popular NOSQL system, which is an open source using ANSI c language key-value storage system (as distinct from MySQL table stored in the form of two-dimensional.). And Memcache similar, but to a large extent compensate for the lack of Memcache. And as Memcache, Redis data is cached in computer memory, the difference is, Memcache only the data cached in memory, can not be automatically written to the hard disk on a regular basis, which means, a power failure or restart, clear the memory, data loss . So Memcache application scenarios without having to apply to a cache of persistent data. The different Redis is that it will periodically update the data written to disk or to modify the operation of the additional write log files for persistence of data

 

3.2, redis applicable scene

 

1. Take the latest N operation of the data

For example, take a typical latest articles to your website, the following way, we can review the latest 5000 collection of ID on the List of Redis, and the part beyond the collection fetched from the database

  • Use LPUSH latest.comments <ID> command to insert data into the set of list
  • Insert after the completion of use LTRIM latest.comments 0 5000 command so that it is always only saves the last 5000 ID
  • You can use the following logic (pseudo-code) and then when we get a page commentary on the client

FUNCTION get_latest_comments(start,num_items):

    id_list = redis.lrange("latest.comments",start,start+num_items-1)

    IF id_list.length < num_items

        id_list = SQL_DB("SELECT ... ORDER BY time LIMIT ...")

    END

    RETURN id_list

END

If you have a different dimension of screening, such as a category of the latest N pieces, then you can build a List Click classification only keep ID, then, Redis is very efficient.

2. The chart application, whichever TOP N operation

This is different from the needs of the above requirement that the previous operation time-weighted, this is a condition for the weights, such as per-sort the top, this time we need a sorted set to run for, will you want to sort value is set to score sorted set of the specific data corresponding to the set value, a time required to execute a command ZADD.

3. The need for accurate application to set an expiration time

For example, you can score the value of the above mentioned sorted set the expiration time set to the time stamp, then you can simply sort by expiration time, time to clear stale data, not only Redis is to remove the stale data, you can put Redis as time expired in the database is an index to the data, with Redis to find out what data needs to delete outdated, then precisely delete the records from the database.

4. Counter Application

Redis commands are atomic, you can easily take advantage of INCR, DECR command to build counter system.

5.Uniq operation, obtaining a certain time all the data duplication value

The use of Redis set the most appropriate data structure, and only need to keep the data to set the throw on the line, meaning a collection set, it will automatically re-ranked.

6. Real-time systems, anti-spam system

Set by the above mentioned features, you can know whether the end-user a certain operation, you can find a collection of its operation and compared statistics. Not impossible, just think.

7.Pub/Sub build real-time messaging system

The Redis Pub / Sub system can build real-time messaging system, such as with many examples Pub / Sub constructed live chat system.

8. Construction system queue

List can be constructed using the queue system can be constructed even using sorted set have priority queue system.

9. Cache

The data is stored directly into memory, the performance is better than the Memcached, more diverse data structures.

 

3.3, redis features

Efficiency: 110,000 speed is read Redis times / s, write speed is 81000 times / s

Atomicity: All operations are atomic Redis, while Redis also supports the full implementation of atomic and several operations.

It supports a variety of data structures: string (string); List (list); the hash (hash), set (set); zset (ordered set)

Stability: persistence, master-slave replication (cluster)

Other features: support expiration time, support services, message subscription.

 

Guess you like

Origin www.cnblogs.com/mediocreWorld/p/11456513.html