Why should I choose Erlang+go for Server architecture (2)

Source: Internet
Author: User
Tags new set
This is a creation in Article, where the information may have evolved or changed.

original articles, reproduced please specify the Source: Server non-amateur research http://blog.csdn.net/erlib author Sunface

Why do I have to choose Erlang?

First, Erlang is particularly suitable for Small and medium-sized team entrepreneurship:

Erlang has an exceptionally mature, telecom-class, large-scale OTP application library that requires very simple code to build an exceptionally stable, fault-tolerant, scalable, and highly concurrent server framework, which is the most valuable core value of Erlang.

Erlang is a natural concurrency language :

Erlang concurrency is language-level, from the beginning of the development of the CSP concurrency mode, in process units, no shared memory between processes, variable immutable implementation of the guarantee of a lock-free concurrency model, it is also exceptionally efficient, in other words: You just write code as usual can be concurrent, Without worrying about any underlying implementations, your code can run perfectly in parallel on multicore servers, and if you can write beautiful concurrency-level algorithms and code (as few sequential codes as possible), you can run 32 times times more performance on a 32-core machine! The concurrency model of the Go language is also about Erlang, but I think the concurrency model of Erlang is better because there is no shared memory between processes and there is no lock at all.

Thirdly, I will introduce my original business requirements :

a multiplayer online game, a player to take a step to broadcast the message to the same screen players, players chatting, fighting more involved in a lot of news broadcast; but it's not easy to fix. Requirements: How does the online player list work? Yes, are you thinking about what kind of lock is right? The key words of the two scenarios mentioned are: High concurrency, mass broadcasting; you will also think of "lock".

I tried to use the completion port +TPL library +protocol buffer in. NET to complete the above functions, but did not pass the test tests, the test model is chat. In terms of sending and receiving messages, the client and server have little or no pressure to send and receive. But once the radio is on, the pressure goes up. The frequent creation of objects can lead to garbage collection, while garbage collection can cause both CPU and memory to be erratic, and the pool of pooled objects can be mitigated, but the problem can not be solved completely, then the thought is human intervention garbage collection, What are the criteria for judging? That is to use PerformanceCounter Bar, the results found that PerformanceCounter a call allocation of memory is quite large! The result of the last version is: Chat room model, one person speaks to everyone, 300 people online can be stable, the number of a lot began to calm down. These are the results of quantitative analysis, and the tools used are the Performace profile tools in Visual Studio2010.

the second problem that needs to be solved is the concurrent lock-up, and the simplest test model is the online player list. This problem also bothered me for a long time, trying all kinds of locks, or throwing exceptions, or the performance of the decline, the problem. follow up also to solve the TCP communication data format, as well as sticky packets and other issues ...

project time is tense, there are many risks, the technical solution should be identified and then to advance other things, but the options available are C + + and Erlang. To be honest with me and the foundation of the team If you use a C + + scenario, you can do it, but troubleshooting and performance optimization will be a huge challenge. What about Erlang? From the beginning of the text referenced in the paragraph, as if this is what I need, simple to understand A little bit of grammar, it was a surprise, and it was kind of nice to have had previous contact with F #. And I am particularly concerned about:

Advantages:

1. Concurrency-oriented, mature and proven frameworks are available, and network parts are well encapsulated

2. Memory Cache solution Process dictionary, read-write speed of the former is 50ns-100ns level

3. The syntax for parsing binary data is intuitive, simple and powerful (there are a lot of binary data to be processed in the game

4. No shared memory! No locks! (We have not shown the use of locks in the code)

Disadvantages

1. The transition from one language to another can be a different form of discomfort:

2. Control logic simple only if and case, and if there is no else, no continue break goto

3. Including kernel libraries and standlib libraries, many functions and variables are named differently from traditional languages

So we decided to use Erlang to re-write a new set of architectures, and it turns out that the decision was absolutely right, that a very small game server that needs to be restarted, hot, and stable is really important, and that the development process and maintenance are so fast and easy, Our team agreed: Never thought development would be such a pleasant thing!

Now that Erlang has been blown away by me, why use go?

In view of the go language has been including women, I do not introduce, about my own situation, I this person has no other hobbies, most of the spare time spent in the so-called "programmer to keep learning will not be outdated" on, so in 11, know the Go, Intermittent study after a year, Go1.1 version came out, found that the improvement is very large, began to seriously study and perennial in Google-group and foreign Daniel's blog world, self-feeling can also. Of course I am definitely not go "Pilgrim", also found that go is not very perfect, specifically see "Why I want to give up go", the author's point of view I do not agree, but some of the views are agreed, such as many go enthusiasts are very justifying, if you dare to say what "bad", Just wait for the meter to be checked;

golang
project nodejs python c++/ruby Erlang
system Mature 4 3 5 5 3
Development Efficiency 5 5 3 4 5
Performance 3 3 5 5 4
Encrypt publication 3 0 4 3 5
Logic Simple 5 5 3 4 5
Easy to learn 5 5 2 5 4
Cross-platform 5 5 5 5 5
As you can see from the table above, the complementarity between Erlang and go is still good, and the system maturity of Go has yet to be improved.

Since both Erlang and go are great languages, there is a problem: two or more? After careful deliberation, I and the team chose the latter. First, Erlang's OTP write server concurrency framework is simple, stable, and high-performance, and Erlang's Mnesia database is lightweight: fast, distributed, and very primitive (supported by the Erlang Standard library). All of this frees the programmer from tedious work, but Erlang also has a very important problem (which may be prominent or irrelevant in different business scenarios, not a problem at least 85% of cases): It's a virtual machine language, For sequential code execution speed is only one-seventh of C, although can take advantage of multi-core, but in large MMORPG, message-intensive, CPU bottleneck is quite obvious, will affect the player's smooth experience feeling (ARPG).

So I thought if this part of logic was written in go, would it be good to use the advantages of both languages to complement each other? Heartbeat than action, due to our Erlang game architecture of the Lotus is still very low, so separate out the map server, with go re-implementation of the next, through the socket with the Erlang architecture part of the communication, found that the effect is very good, go performance, Concurrent native support coupled with the Erlang write game framework, the performance is no less than the C + + framework, but the latter everyone understands that Zhongguancun programmers are said to average life expectancy of more than 50 years, a large part of the reason is because of this.

How can I get to the next road?

Mixed-type programming will be the mainstream of the future, because no language is perfect, including by many "pilgrims" advocated by go, if we can choose the right language according to their own business scenarios, that dare not say things half 10 times times, at least the multiplier should be some , so do not be the mainstream language (java,c++) imprisoned our world, limited our innovation, if you can do easy and happy development, then this world how beautiful!!


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.