A case of 5-cache design of information system practice

Source: Internet
Author: User

Description: Information System Practice Notes series is the author in peacetime research and development has encountered the size of the problem, perhaps simple and subtle, but often is often encountered problems. The author is more typical of which to collect, describe, summarize and share.

Absrtact: This article describes the interface between the information system or the platform which the author has contacted, and exhaustive to share it.

Body

Series of essays Directory: Information System Practice notes (http://www.cnblogs.com/taichu/p/5305603.html)

In

Reprint instructions: Please specify the original author, connection, and source.

What is 1.CACHE?

Recently has been making scala,spark,python, data mining things, as well as some of the complex science of research, in the enrichment and busy, although the interest is strong, but not physically tired, fatigue is very, helpless delayed the practice notes.

I'm sorry to have planned to hang out a post for at least 2 weeks. To share too many things, code word too much time, and sometimes even feel that the meeting may be better (a lot of people are disgusted with the meeting, it is because we have not held a meeting)! In fact, many things in addition to guide people, or test themselves to go, to experience, summary.

Gossip doesn't say, cut to the chase.

Many developers, especially those who develop the program, find a cache framework or class library available, such as Hibernate's cache function, and then use a brain. Although it will be effective and enjoyable, this is a good thing, both to increase experience, but also to understand the charm and efficiency of the framework, but in fact, there is the most important thing can be further understood. Why should I use the cache? What's the cache? What kind of scenario is used for the cache? Few people are quiet to ask.

What the hell is the cache? It's hard to articulate, but there are some points:

  • the cache is created to mitigate the speed differences between different nodes in the processing chain . To cite a number of examples:
      • computer system: generally has a speed gradient: CPU Internal Register (register) > Memory > Disk io> network io, In order to reconcile the speed of data transfer between them, you can set the cache structure, called cache;
        • Description 1: The above is the general case, do not consider the special magnetic array raid, tell SCSI, gigabit or million gigabit network, etc.
        • description 2:cpu internal also divided L1 , L2,L3, such as multi-level cache, the previous 386/486 processor did not divide so many cache levels;
      • metro system : Sometimes accidents, people, station outside the stations by the administrator guarding the entrance into the 1 floor in batches of the hall, The 1-storey Hall is a cache; the administrator in batches from the hall to the B1 layer platform, then B1 platform is also a cache; When the cache is almost full, the administrator will shout "Slowly, do not squeeze, temporarily closed, we wait patiently!" , so people were hoarding in different caches in order to coordinate the problem of the speed difference between the subway and the influx.
      • Factory pipeline : Another typical example is the assembly line of the factory. Before the old-fashioned assembly line of each station 1 people, handling 1 assembly actions, such as the yellow duckling toys loaded with 1 legs. A workpiece, if it flows through 10 processing nodes, there will be 10 people to handle 10 steps. Obviously, the boss expects the pipeline to be full of state, the most efficient flow of output. But accidents always occur, such as a workpiece n flow to the PX node, PX node workers are tired, slow processing, resulting in the Px-1 step outflow of the workpiece piling up, and this PX processing node processing station of the workpiece accumulation site is a buffer buffer (that is, cache), it is more and more full, To remind the workers of PX, step up the dry, otherwise once the cache burst, you can not overcome (catch up) the speed difference (time difference) problem! (voiceover: You can see the efficient and scientific pipeline mechanism, as well as "cruelty").
      • producer Consumer mode: the Classic programming model "producer and consumer" also has a cache design that coordinates production and consumption at a low speed, similar to "Factory line"
  • The
  • cache actually uses a spatial cost to mitigate the uncoordinated time.
    • You must ask, "if there is a difference in speed, then the cache is useless?" "And, indeed, the big cache is also used to temporarily overcome the speed difference, it is expected to use space for a temporary accumulation of time to recover the occasional cause of the uncoordinated (speed difference is absolute, this in the design of the scene prior to know, but the speed of uncoordinated is sporadic, this to distinguish open!) )。 If you can never "recover", then the big cache will have problems, then is the design of the problem, it is not the cache mechanism can be solved. The
    • processing node px is often different in speed, so it must be arranged with different algorithms. For example,
      • arranges the code of a compiled language such as C + + by compiling the program, and takes into account the differences of cpu/mem/disk/network, keeping the MEM snippet high hit, reducing page loss interruption, or from Decun (relative to memory MEM, Disks are external memory devices, and disk IO is much slower than mem.
      • through the interpreter (such as Java's virtual machine JVM), Java and other explanatory language code into pseudo-instructions, it also fully consider the cpu/mem/disk/network speed difference; The program itself is dead, but the compiler or interpreter can do a lot, Reasonable arrangement of the program sections, data sections, and consider how to import multi-core CPU and so on. The
      • program itself should also take full advantage of methods such as "multithreading", "Concurrency Computing", "distributed computing", and so on, from the macro-level of the algorithm, taking into account, to solve the difference in speed, so that the appropriate use of the cache to prevent occasional speed uncoordinated, rather than the algorithm itself has a permanent mishap " Speed mismatch ". A number of mainstream mature top three frameworks can be properly used to solve practical problems. The
      • program is ultimately a business need to achieve the functional goal (Feature). And the function and the business itself is characteristic, has the time and space characteristic, is the high concurrency business? Or a random business? What is the temporal and spatial nature of the business? Is it related to the rush hour? Is it related to the maintenance period of the website? Is it related to the time period of shopping? In short, from the business trigger, the need for professional domain knowledge (industry support), and the need for deep technical skills, such a scenario from the system analysis and system design behavior, is how important! There is a shortage of talent in this area.
  • The time-space that the cache handles is not coordinated, may be sporadic, or it may come from a business model;
    • Through the above different levels (compiler, interpreter, program, Framework class Library, business scenario analysis) disposition, basically understand and can properly handle the business, to achieve the scene, the use of the necessary cache structure. But the business model will always be sporadic inconsistency, especially in today's multi-tasking computer architecture (formerly mainframe is multi-process, and now the microcomputer is multi-tasking and refinement to the thread!) )。 It is easy to calculate the force is not enough to increase the core of more CPUs, the amount of data is not place, the increase in Mem,mem will also lead to low-speed disk IO and network IO also become larger.
    • In addition, in the process of continuous tuning and expansion of the business model, the uncoordinated will naturally occur, and if the program model is relatively good, the relevant necessary place has the cache, then the cache will provide room for the operation of the program.
    • Even a simple business model, and tuning is complete and always working. However, it is possible to break the balance due to problems such as "network failure, CPU failure, memory or disk corruption, a compute node offline". At this time, even ligatures log logs to record status is also very extravagant or stretched. At this point, if the program in the framework of the design has been planned for the full and necessary cache, it can improve the robustness, and even in the occasional disaster impact of strong operation, restore operation.
  • Both the objective world and the procedural world are complex, and complex systems are everywhere unexpected and uncoordinated, and the cache comes into being, everywhere:
      • The micro instructions in the CPU use the L1,L2,L3, they are all levels of cache;
      • Mem,disk, network cards and other devices, whether in the hardware interface program BIOS, or in the programmer's application, all arranged the cache, to deal with the device by external speed mismatch impact.
      • Many models in the program design, such as consumer producers, are bound to have a cache to alleviate the production and consumption of the speed mismatch;
      • Real-World Warehouse inventory, small supermarket stock, emergency parking belt on the elevated road, the upper and Lower Fork area, the exhibition door of the back-shaped line guardrail, the pipeline of each node buffer buffer belt, and so on, are a good example of the cache;

Only by understanding the boundaries of natural phenomena, business laws and procedural capabilities can we deeply understand how to use the cache design in programming, in what kind of business scenarios, in order to reach the benefits and results. It is not recommended for technical use only, it must be clearly used for the purpose of the correct use.

A case of 2.CACHE design

Here is a simple example, or the previous few shared client and server, they are to provide GIS map service server and client, and this GIS Business Server to use a lot of cache; Split Client business presentation does not say, Many backend services are provided by the application server doing GIS business, short for GS, and GS need to get equipment information from Device management platform, DS.

  1. Because DS does not provide subscription to device information (unfortunately), it is not possible to use the efficient observer pattern to obtain device change information, but only by the GS through the phase synchronization (polling model) to take a huge cost to DS to obtain in fact very little changes in Device update information. In order to phase synchronization, GS can only in memory (or DB) to do a cache, to store such as every 30 minutes of synchronization information;
  2. First because it is 30 minutes in sync, and according to the classic use of the Java EE, through DAO, the business is to obtain data from the DB, so the synchronization data is placed in the DB. In order to improve the speed, solve the high-speed requirements of the business scenario and the slow speed of the DB processing, the DB data "up" into the memory of the cache, do business directly into the cache, processing the cache; This naturally raises the question that the cache's dirty data needs to be synchronized back to the DB, Because the data exists in a single place, a cache is temporary, and a db is persistent, there are synchronization issues between them, which is often the case with the cache. In fact, because the original program framework to do poorly, resulting in batch modification, but also left a lot of small tail, can only casebycase processing, to find a calculation, when let can also be in the scope of human allowed to large area of the regression test, not elaborate.
  3. We GS platform, also and third parties, such as a city intersection bayonet management Platform (KS) docking, the last time talked about the platform docking problem, where the assumption of smooth docking. Then the KS data, will also be phased synchronization to our GS. A typical application is the GS to the KS subscribe to a "alarm a", when the KS time A and issued "alarm a", will be pushed to the GS platform, then the GS platform distributed to the client. There is obviously a speed of uncoordinated, if the alarm is frequent, GS temporarily processing, then the cache, how to do? Also can not write code, our GS and GS client is through the ACTIVEMQ message middleware to deliver messages. After the subscription, messages and users of different topics will have their own buffer to store, even if the KS platform burst a large number of alarm A to gs,gs too late to distribute, then put in MQ's own management cache queue, MQ itself processing. Of course, here we see some of the cache issues discussed above:
    1. If the "alarm a" of the KS platform continues to be transmitted at high frequencies, then the final GS receives the code to get into MQ, or it can be put into MQ, but MQ will eventually crash. So here is encountered in the previous several mentioned platform docking, the two interactive platform between the signaling and the data "space-time characteristics" must be defined clearly, for example, we GS and KS define the maximum concurrency can not exceed 1 seconds 10 (10CAPS), otherwise GS refuse to be accommodated and handled by the cache as much as possible. There is a limit to everything, and the discussion is clear!
    2. There is also a hidden problem, we do not know to see it. This assumes that my GS Java receive code, from the network socket socket to get the bulk byte stream, the speed is very fast, put into MQ almost do not spend time. If there is no such hypothesis, and the speed is really slow, then MQ will not eventually "explode", first rushed to the time of the GS receive code fragments. This judgment is generally based on the experience of the programmer and the experience of the industry. It's always very fast, and putting a message is always a lot faster than consuming a message.
  4. The cache can also exist at a high level and at higher levels of abstraction. For example, the current Hibernate library supports the mapping of objects to the data model, while the business is targeted at objects, not directly naked and interacting with the data. Actions for objects such as func1 () can also be cached by the cache, so that the second time the same call to Func1 () is greatly increased in speed and efficiency. Of course, you will see a dirty data and failure problem, no longer discussed.

[END]

A case of 5-cache design of information system practice

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.