github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/blog/content/qihoo.article (about)

     1  Qihoo 360 and Go
     2  6 Jul 2015
     3  
     4  Yang Zhou
     5  
     6  * Introduction
     7  
     8  _This_guest_blog_post_was_written_by_Yang_Zhou,_Software_Engineer_at_Qihoo_360._
     9  
    10  [[http://www.360safe.com/][Qihoo 360]] is a major provider of Internet and
    11  mobile security products and services in China, and operates a major
    12  Android-based mobile distribution platform. At the end of June 2014, Qihoo had
    13  about 500 million monthly active PC Internet users and over 640 million mobile
    14  users. Qihoo also operates one of China’s most popular Internet browsers and PC
    15  search engines.
    16  
    17  My team, the Push Service Team, provides fundamental messaging services for
    18  more than 50 products across the company (both PC and mobile), including
    19  thousands of Apps in our open platform.
    20  
    21  Our "love affair" with Go dates back to 2012 when we first attempted to provide
    22  push services for one of Qihoo’s products. The initial version was built with
    23  nginx + lua + redis, which failed to satisfy our requirement for real-time
    24  performance due to excessive load. Under these circumstances, the
    25  newly-published Go 1.0.3 release came to our attention. We completed a
    26  prototype in a matter of weeks, largely thanks to the goroutine and channel
    27  features it provided.
    28  
    29  Initially, our Go-based system ran on 20 servers, with 20 million real-time
    30  connections in total. The system sent 2 million messages a day. That system now
    31  runs on 400 servers, supporting 200 million+ real-time connections. It now
    32  sends over 10 billion messages daily.
    33  
    34  With rapid business expansion and increasing application needs for our push
    35  service, the initial Go system quickly reached its bottleneck: heap size went
    36  up to 69G, with maximum garbage collection (GC) pauses of 3-6 seconds. Worse
    37  still, we had to reboot the system every week to release memory. It wouldn’t be
    38  honest if we didn’t consider relinquishing Go and instead, re-writing the
    39  entire core component with C. However, things didn’t go exactly as we planned,
    40  we ran into trouble migrating the code of Business Logic Layer. As a result, it
    41  was impossible for the only personnel at that time (myself) to maintain the Go
    42  system while ensuring the logic transfer to the C service framework.
    43  
    44  Therefore, I made the decision to stay with Go system (probably the wisest one
    45  I had to make), and great headway was made soon enough.
    46  
    47  Here are a few tweaks we made and key take-aways:
    48  
    49  - Replace short connections with persistent ones (using a connection pool), to reduce creation of buffers and objects during communication.
    50  - Use Objects and Memory pools appropriately, to reduce the load on the GC.
    51  
    52  .image qihoo/image00.png
    53  
    54  - Use a Task Pool, a mechanism with a group of long-lived goroutines consuming global task or message queues sent by connection goroutines, to replace short-lived goroutines.
    55  
    56  - Monitor and control goroutine numbers in the program. The lack of control can cause unbearable burden on the GC, imposed by surges in goroutines due to uninhibited acceptance of external requests, as RPC invocations sent to inner servers may block goroutines recently created.
    57  
    58  - Remember to add [[http://golang.org/pkg/net/#Conn][read and write deadlines]] to connections when under a mobile network; otherwise, it may lead to goroutine blockage. Apply it properly and with caution when under a LAN network, otherwise your RPC communication efficiency will be hurt.
    59  
    60  - Use Pipeline (under Full Duplex feature of TCP) to enhance the communication efficiency of RPC framework.
    61  
    62  As a result, we successfully launched three iterations of our architecture, and two iterations of our RPC framework even with limited human resources. This can all attributed to the development convenience of Go. Below you can find the up-to-date system architecture:
    63  
    64  .image qihoo/image01.png
    65  
    66  The continuous improvement journey can be illustrated by a table:
    67  
    68  .image qihoo/table.png
    69  
    70  Also, no temporary release of memory or system reboot is required after these
    71  optimizations.
    72  
    73  What’s more exciting is we developed an on-line real-time Visibility Platform
    74  for profiling Go programs. We can now easily access and diagnose the system
    75  status, pinning down any potential risks. Here is a screen shot of the system
    76  in action:
    77  
    78  .image qihoo/image02.png
    79  
    80  .image qihoo/image03.png
    81  
    82  The great thing about this platform is that we can actually simulate the
    83  connection and behavior of millions of online users, by applying the
    84  Distributed Stress Test Tool (also built using Go), and observe all real-time
    85  visualized data. This allows us to evaluate the effectiveness of any
    86  optimization and preclude problems by identifying system bottlenecks.
    87  
    88  Almost every possible system optimization has been practiced so far. And we
    89  look forward to more good news from the GC team so that we could be further
    90  relieved from heavy development work. I guess our experience may also grow
    91  obsolete one day, as Go continues to evolve.
    92  
    93  This is why I want to conclude my sharing by extending my sincere appreciation
    94  to the opportunity to attend [[http://gopherchina.org/][Gopher China]].
    95  It was a gala for us to learn, to share and for offering a window showcasing
    96  Go’s popularity and prosperity in China. Many other teams within Qihoo have
    97  already either got to know Go, or tried to use Go.
    98  
    99  I am convinced that many more Chinese Internet firms will join us in
   100  re-creating their system in Go and the Go team's efforts will benefit more
   101  developers and enterprises in the foreseeable future.