This paper attempts to share with you some thoughts and decisions on the selection of Internet high concurrency technology solutions through progressive technical analysis. From the perspective of technology selection, it provides a reference for architects to consider all aspects of high concurrency business design.

Beautiful Concurrency & Pretty Erlang

preface

With the mission of “connecting people, connecting people and business”, Huanxin aims to provide the best quality global INSTANT messaging PaaS service for the majority of enterprise developers. How to achieve high concurrency scenarios, flexible guarantee of service quality is our consistent business requirements and technical pursuit.

This paper attempts to share with you some thoughts and decisions on the selection of Internet high concurrency technology solutions through progressive technical analysis. From the perspective of technology selection, it provides a reference for architects to consider all aspects of high concurrency business design.

Next, enjoy:

Beautiful Concurrency – The necessity of Concurrency

The world is concurrent, and so should software

The world we live in is a vast, concurrent system. Every moment, in every corner of the world, every individual of human beings is conducting frequent energy interaction with the world, and information interaction is also an important part of it. When we shift our identity and look down on all the creatures in the world (animals, plants, oceans, soil, machines, etc.) from the perspective of the creator, we can see that they also have an endless energy/information interaction with the world.

To interact effectively with the world, software should also be concurrent.

The world is distributed, and software should be distributed

The earth is round, the world is flat, and anyway, after the singularity of the Big Bang, there are distributed universes/worlds. As software that interacts with everything in the world, including ourselves, nature must also meet the requirements of distribution. The Geo. Distribution feature is only a reflection of concurrency in the spatial dimension.

The world is unpredictable, and software should be fault-tolerant

No world is perfect: conflicts, disasters happen all the time, no matter what dimension. As software, bugs and crashes are also unavoidable practical challenges. Even if there is a perfectly bug-free program, the hardware on which it is running can fail. To make software more fault tolerant, code independence (meaning that a failure does not affect other tasks than the failure task) and fault detection and fault handling are key: all of this requires concurrency, because serial programs are far less fault tolerant than concurrent programs.

Overview of concurrent scenarios

Seven Concurrency Models is based on Seven Concurrency Models in Seven Weeks by Paul Butcher. This is an overview of the most common Concurrency scenarios for architects. On this basis, I added some of my own thinking on expansion (

See italics below

) :

1. Threads and locks

The threading and locking model has many well-known shortcomings, but it is still the technical basis for other models and the preferred choice for many concurrent software developments. –

In fact, this solution is an anti-pattern, with bugs and dead locks following it in high concurrency scenarios, making developers and operations staff tremble.

Ugly Locks to Ugly Concurrency: Locks and Condition variables is fundamentally flawed!

2. Functional programming

One of the reasons functional programming is becoming increasingly important is that it provides good support for concurrent and parallel programming. Functional programming eliminates mutable state, so it is fundamentally thread-safe and easy to execute in parallel.

Beauty of function, beauty of logic! I believe that many people make such an exclamation when switching from Imperative Programming to Functional Programming mg. In fact, this progress just reflects the evolution track of human beings in the process of continuous evolution, continuously refining their cognition of the world and gradually shifting their thinking mode from concreteness to abstraction. Back on the topic of high concurrency, functional programming, with its simple strategy of immutable state, has won the perfect accolade and is now the king of tomorrow.

3. Separation of identification and status:

If one thread references a persistent data structure, changes made by other threads to the data structure are invisible to that thread. Therefore, the significance of persistent data structure for concurrent programming is very strange, which separates identity and state.

— This is another clever strategy, and well-known Version control systems such as Git, including bitcoin/blockchain, are all concrete practices under the guidance of this idea. Due to the lack of space, this article will not expand further.

4. The Actor Model:

A widely applicable concurrent programming model, suitable for shared memory model and distributed memory model, also suitable for solving geographically distributed problems, can provide strong fault tolerance.

First introduced to the Actor model through Erlang, and then to frameworks implemented in various languages such as Akka (based on Scala), I’ve come to appreciate how well the model works in high-concurrency scenarios. This will be covered later in this article. In addition, do a small recruitment advertisement, huanxin communication cloud R & D team is recruiting talents, welcome to understand Erlang, have related high concurrency development experience partners to join, empty seat waiting!

Click here to contact us directly!

5. Communicating Sequential Processes (CSP)

: Superficially, the CSP Model is similar to the Actor Model in that both are based on messaging. However, the CSP Model focuses on the channel that passes information, while the Actor Model focuses on the entities on both ends of the channel, and code using the CSP Model has a significantly different style.

— This is where Channel can do its thing. Go go go! Due to space limitations, this article will not expand further. Similarly, welcome to Golang, have related high concurrency development experience partners to join, empty seat waiting!

6. Data-level parallelism:

Hidden inside each laptop is a supercomputer, the GPU. GPU utilizes data-level parallelism, which can not only rapidly process images, but also be used in a broader field. If you want to do finite element analysis, fluid dynamics calculations or other large number of digital calculations, GPU performance will be the best choice.

— In the past two years, Jensen Huang (CEO of Nvidia) has brought world-shaking innovations from his fireplace, brought previously untouchable Gpus into the home, and brought computing power wars one after another. It is believed that in the near future, with the support of GPU computing power and the universal use of ARTIFICIAL intelligence algorithm, there will be countless AI application layers emerging in an endless stream. They will fall from the cloud to the edge, and human beings may enter the uncertain era of man-machine hegemony earlier than they imagined.

7. Lambda architecture:

Lambda architecture combines the features of MapReduce and streaming processing, and is an architecture that can handle a variety of big data problems.

Lambda architecture also adopts data parallel processing technology, but it magnifies the micro scene of parallel computing power to a larger scale: Distributing data and computation to a cluster consisting of thousands of machines, integrating concurrent and distributed features into one solution, through a combination of two layers (batch-layer, speed-layer /Streaming Process), Achieve high computing efficiency and low latency “have your cake and eat it”.

Pretty Erlang

The core network of the global instant messaging cloud of Huanxin is developed based on Erlang/OTP. Up to now, it has served hundreds of thousands of APP customers with daily messages of billions of magnitude in a single cluster, and it is still challenging new heights. After years of experience accumulation, we are still constantly optimizing the system, extracting the endogenous value of resources such as computing and network, and challenging the system’s constantly improving index requirements. Thanks to Erlang/OTP, we are like a warrior wielding a sword standing on the shoulders of giants, able to cope with various business pressures and constantly changing business needs. Here’s a quick summary of some of Erlang/OTP’s “intoxicating qualities” for those who want to get into a pit:

Let it Crash!

Once again, let us understand that “the height of thinking determines the height of life”. While other languages or solutions are defensive, trying to catch exceptions/errors and save the day, Erlang adopts “Let it Crash!” OTP’s Supervisor Behaviour Hierarchy (Hierarchical) management reporting mechanism is based on a rigently designed unidirectional and bidirectional link mechanism.” “Let it crash” while cleanly and beautifully managing the behavior of all actors in the event of an exception is simple but effective, with unexpected benefits:

  • The code is concise and easy to understand, with fault-tolerant code only at the level where crash handling is a concern;

  • Due to the design of the Actor Model, actors are independent of each other and do not share state. Therefore, the crash of any Actor does not affect other actors, not to mention its Supervisor, so the Supervisor can deal with the crash of the managed Actor calmly.

  • Administrators can also leave the crash alone, just log the crash, and then follow up by viewing crash notifications. This strategy can also be used in subsequent Pattern Matching, where we can record the information in _Other (all matches are not triggered) protection matches without having to worry about unexpected scenarios!

Actor Model

Actor Models are Pure OO designs (Java class and method designs are not, surprise?). Each Actor encapsulates the state, and there is no way to manipulate objects outside of Manipulate. They can only notify actors by sending messages, and actors themselves control the processing of the message. This simplification also provides surprisingly powerful support for high concurrency.

Actor Model is suitable for shared memory Model and distributed memory Model, also suitable for solving geographical distribution problems, can provide strong fault tolerance, use good!

In my previous work, I made a product of network security filtering, which was a set of message queue processing mechanism designed and written by my team to decouple the processing of the same data by different businesses. Later, I suddenly found that it was actually a simple Actor Model design, but I did not know Erlang/OTP at that time. How many days and nights of my life could have been saved if I had used this weapon earlier!

Functional Programming

There are many books on functional programming, so this article will not expand them too much. Here are my personal experiences:

  • Program will eventually to the machines processing, so as far as possible according to the thinking mode of the machine to write programs (though sometimes let programmers head), the most simple (a + b) and (+ a, b) : the former friendly to human beings, while the latter is friendly to the machine, and thus leads to better process consistency, which in turn makes the system can design more simple;

  • Imperative programming consists of a series of statements that change global state, whereas functional programming abstracts the computation into expression evaluation. These expressions are made up of pure mathematical functions that are class I objects (we can manipulate class I objects as if we were manipulating numbers) and have no side effects. Functional programming is especially suitable for concurrent/parallel programming because it is easier to be thread-safe without side effects.

  • Beauty of function leads to beauty of concurrency! Excerpts from the original:

For me, a beautiful program is one that is so simple and elegant that it obviously has no mistakes, rather than merely having no obvious mistakes. If we want to write parallel programs that work reliably, we must pay particular attention to beauty.

Pattern Matching

Here’s another design to knock your head off: With the syntax of Pattern Matching, functions turn into proof questions that Conditon (but don’t have to) the scene you care about, and let Pattern Matching do the rest for you. Surprisingly, where Pattern Matching works isn’t just where case/if appears — your entire code works under the spell of Pattern Matching.