Another talk about caching and Redis

Source: Internet
Author: User

How is Redis going to be used since the last time I shared it? "has been in the past 1 years, and has experienced a lot of 1. From the code of the first line of our site to the present, our cache module is also constantly upgrading, which is really a bit of experience, recently there are friends to explore the cache, I think can be summed up under the next humble opinion, look forward to have more in-depth research.

What is a cache?

I often see people in a group or in the community who have a lot of questions about caching, confusing the purpose of caching, and confusing. NET Redis drivers, middleware differences and choices. Caching is not really something that looks esoteric or difficult to navigate, it is typically used to store some commonly used data into memory to speed up data reads and reduce direct access to DB traffic to reduce DB pressure . More common scenarios such as:

    1. Static dimension table class data, such as address libraries, units, and so on.
    2. User session
    3. Some high-real-time, high-frequency computing data, such as the number of user visits, article reading, user blacklist and so on.

In the traditional architecture, the cache is purely a copy of the DB data, as described above for the program to read the data more quickly. Since it is copy, you don't have to worry about losing or even tiny errors. Must be the first to guarantee the DB, then the cache is considered. In addition, now the distribution of large lines, clusters abound, the cache should be divided into multi-level, from the single-machine memory to the centralized cache of the last penetration of the DB

But now many large Internet architectures have different applications for caches, such as Sina Weibo, where they use Redis not as a simple cache, but as a first-tier storage, and then asynchronously back to DB. You can refer to this article.

Recently encountered a very interesting discussion, referring to the user blacklist function design. Have a friend db dependence super strong, come up is User table plus field ah? read too slow? Index Ah, and stuff like that. I think this is very interesting, I used to take it for granted. Why? At the beginning of the project is to design a database to start, modeling is ER diagram, up is the db three paradigm. That I can hardly change this kind of thinking now. The idea of modeling such as learning oo,ddd always revolves around DB First's thinking. If you bypass the DB, think about caching to design such a function, the feasibility and performance can be improved a lot.

. NET cache applications

For a stand-alone application, the memory cache (System.Runtime.Caching) is sufficient, the cluster environment should be on the centralized cache, more commonly used is memcached and Redis, the difference between the two can be said to be good.

Memcached is more like the memory cache, the function is single, can only do normal cache operation (Put/get/remove ... )

The Redis feature is richer, supports more data structures, and more computing commands, so caching modules such as sessions are more suitable for memcached, and are more suitable for redis with real-time computing properties. But at the same time using the two services, but also only large companies can do, the general portrait of me, or prefer redis, after all, rich features.

On the drive of Redis, I also often see Sevicestack.redis/stackexchange.redis make people do not know the choice.

Both I use, because Servicestack originally was open source free later in order to support development, people by the way after V4 began to join the restrictions, began to collect money. However, V3 is still free, use the time need to pay attention to all the dependence should use V3 below Oh. V3 version Unfortunately, many features are not very well supported, such as Pub/sub.

Stackexchange.redis from the famous StackOverflow, they have the revenue of the website, naturally keen open source free. But the quality is very reliable, the new features support is very good.

The above is on GitHub on a search.

Another open source project Cachemanager.net recently was also very hot, poke here. Many people do not understand what it is, it is actually a middleware, itself does not directly provide the interface with the cache (REDIS\MEM) API, the current version of it is the use of Stackexchange.redis to drive, the blog Park has a very detailed introduction (here). It is dedicated to shielding the complexity of various cache services, providing a simple and consistent API that allows developers to use a set of code that can be configured with a memroycache/centralized cache (REDIS/MEM). The strongest is that it provides a multi-tier cache scenario (based on Redis pub/sub), as long as a simple configuration achieves cache synchronization between tiers. (The internal principle is that, with Redis Pub/sub, the Sub is automatically removed from the response cache whenever the cache changes). A recent update of our company also switched to Cachemanager.net, and had to say it was really good.

Design caching appropriately

1. Proper design of key

The most important feature of the cache is its key-value form, even though the various data structures of Redis are also. Key-value is the root cause of its fast, so a reasonable key will make the search more convenient.

This will also make a piece of data based on the scene is designed to multi-key-value, for example: I mentioned in the previous article of the fuzzy matching function will be the name of the key, and if it is simple to access user information based on UserID, the UserID will be designed into key. You can also see from here that the cache doesn't mind saving a lot of the same data.

2. Reasonable use of cache expiration time

The above mentioned cache can be lost, indeed if it is a memory cache, it will be released as the process of the application terminates. In addition to this release, the cache can also be set to expire. Why do you design this? Imagine that the machine memory will not be bigger than the hard disk, space is effective, precious resources are naturally to save as often as possible the data (hot data). So a reasonable design failure time will keep the data always the most active part. Of course, the expiration time will also cause, cache Avalanche and other a series of problems, here is an in-depth article worth to see (poke here)

Not to be continued ... The next article will continue to share the cache module I am using, the. NET cache module design, involving the use of Servicestack.redis features, cachemanager.net.

Another talk about caching and 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.