Build an event-driven shelf: async and efficiency

Source: Internet
Author: User

The problem arises in the system that I am currently participating in, the system needs to collaborate with multiple subsystems that the company already exists, and the whole architecture looks like this:

Upper client, various clients

The mid-tier application layer facilitates business operations, business integration, interfacing with APIs and lower-level service applications, exposing interfaces to provide data to upper-level clients.

Lower layer of service, separate deployment of internal application services, independent management of different departments.

The lowest level of data persistence, the company's own development of the cloud deployment environment, one-time commit automatic compilation, automatic release.

Problem

Personal main development work around the application layer, need to call the lower service module, but due to historical problems and business requirements, each application layer of operation (commit), need to cross-module data validation, and ultimately submitted to a number of distributed subsystems.

Because "data integrity" is the most important principle of the system, so it needs to be verified multiple times, the entire application layer of business operations becomes quite time-consuming (across multiple systems for data validation, network IO time, etc.), seriously affect the application layer users of the use of physical examination, so that the overall system in the efficiency of a significant discount.

Solution Solutions

For the system performance solution, I think the simplest three ways: "Asynchronous" "cache" "Parallel."

The "1" establishes a validation rule for the calibration rules, asynchronously caches it to the application system, and reduces the time spent in a for-loop multiple network IO (cross-module) checksum, but the new problem is that the new generation of data within the cache time period may affect the eventual data consistency and consider a temporary discard.

"2" The simplest solution is to implement the time-consuming operation (network check) asynchronously;

asynchronous operations in. Net

After 4.5, it is possible to execute asynchronously with a simple implementation of a task:

        static void Main (string[] args)        {for            (int i = 0; i <; i++)            {                asyncmethod (i);            }            Console.readkey ();        }

  

        #region Async Mode One        //asynchronous operation        private static async void Asyncmethod (int input)        {            Console.WriteLine ("{0}" into asynchronous operation! ", input);            var result = await asyncwork ();            Console.WriteLine ("Final result {0}", result);            Console.WriteLine ("{0}" exits asynchronous Operation! ", input);        }        Simulate time-consuming operation (Async method)        private static async task<bool> asyncwork ()        {            await task.delay ();            return true;        }        #endregion

  

        #region Async mode two public        static void Asyncmethod (int input)        {            Console.WriteLine ("{0}" into asynchronous operation! ", input);            Task.Factory.StartNew (() =            {                System.Console.WriteLine ("{0} time-consuming operation ...", input);                Thread.Sleep (+);                System.Console.WriteLine ("{0} time-consuming operation ended", input);            });        #endregion

Execution results

You can see that loops during system synchronization are not blocking tasks because of waiting, more solutions want to try out in the next work, or three words "async" "Cache" "parallel".

Build an event-driven shelf: async and efficiency

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.