There are complimentary books at the end of the article

The authors introduce

Zhi-peng zhang, senior Java enthusiasts, deep in the field of online education, personal blog: https://blog.52itstyle.com.

Editor’s note

In the previous article, “Knowing this Architectural evolution Map, Building a Web Site from Scratch is Not hard!” This new article was created after some students left comments in the hope that they could share their specific knowledge about micro-services and distribution. Before reading this article, make sure you have a reasonable network background and are familiar with Linux. Browsing should take 3-5 minutes.

I. Distributed

Xiao Ma is running an online shopping website called TT Cat, which has modules such as commodity management, order management, user management, payment management and shopping cart. Each module is deployed to an independent cloud service host.

Now, programmer Xiao Ming is browsing TT cat and wants to buy an awesome cherry mechanical keyboard to improve his work efficiency. So he opens the TT cat home page, searches for products, views details and reviews, adds shopping cart, places orders, pays and so on. Xiao Ming finished the shopping smoothly and spent a lot of money.

But how does the system perform this sequence of operations, as shown in the picture below, is intricate (I’ll ignore some details). Invisible to the user, but the entire ordering process is walking between the network.

TT cat distributes all function modules in different places, and finally completes a series of user requests, which is probably a distributed system.

Second, micro services

Bloggers see microservices as an architecture and also within the distributed category. How micro is micro? In distributed systems, microservices place greater emphasis on single responsibility, lightweight communication (HTTP), independence, and process isolation. Well, there’s nothing more to say. Practice is the key, and I suggest you learn more about Spring-Cloud related microservice components.

TT Cat, which holds some activities every year, such as singles’ Day (Singles’ Day), which is a girl’s favorite day, will be flooded with users in the dead of night, which may destroy a certain service.

At this time, the problem came to the user order timeout, or directly 500 errors, how to solve?

Load balancing cluster

How could something like this happen at such an important event? In fact, Ma dad bought several servers in advance, and engineers have already deployed multiple copies of each business function module.

Each module of the same function forms a group and is managed as a single system. When a girl places an order, she actually has a relationship with a cluster group, but the system will ensure that she only has a relationship with one of them. The cluster group has its own scheduling algorithm, so don’t worry about not having a relationship with the girl.

For example, if you lived in ancient times, you were 18, unmarried, rich, handsome, and in need of a personal physiological solution. Therefore, you came to the legendary fengyue field, cough, this ancient but legal. At this time the procuress or big teapot came to greet you, if there is no special requirements, you will be brought into a house, there is a prostitute woman……

Wind a turn, there is no flash blind their programmer ten thousand titanium dog eye. You can think of a madam as a load balancer, with a built-in scheduling algorithm, and a prostitute as one of the set groups.

Okay, so, ellipsis, you might be wondering, what do we use for load balancers in a production environment?

  • Hardware F5 with deep pockets

  • Use DNS load balancing without cost

  • Using LVS is technically awesome

  • Nginx is the only option for small, miserable startups

Of course, there are more than one load balancer, so if you are interested, Google it.

“On knowledge and Action” said: know what it is, know what it is, simply say how these load balancers walk in the network, friends who have learned the network are probably clear about the seven-layer network model.

Let’s start with a picture to remind you of the basics of college.

Is there any instant classroom book feeling, not satisfying? Here’s another TCP/IP five-tier model.

In each layer are working different equipment, such as rich, not poor state-owned enterprises use F5 work in 4-7 layer, general Internet enterprises use LVS work in the transmission layer, the most widely used Nginx work in the application layer.

Finally, let’s talk about DNS load balancing. Although DNS is the most primitive and simplest method, the control of DNS load balancing is in the hands of DNS service providers. NDS has multi-level resolution, the problem of caching A records, and the website itself can not do more management. This has led to the general use of small and medium-sized companies.

Of course, if you are strong enough, DNS load balancing is also a good choice. The following figure is part of the information obtained by detecting the A record of TT cat domain name, for reference only and self-understanding.

High availability cluster

Since it is a cluster, it cannot have a single point of failure. If you pay attention to cloud services, you may come across the following terms: “dual-system hot backup”, “geo-centric” and so on.

Dual-system hot backup is a form of high availability. As shown in the figure above, we have two load balancing nodes in the production environment. The primary node is active and the other node is standby. As for the two centers and three centers, the picture below may make you understand more clearly. The picture comes from the Internet.

Elastic clouds

In order to prepare for double Eleven, Xiao Ma ge purchased a large number of servers, but after the activity, the usual user access can not meet the server’s customer reception capacity, resulting in a large number of servers in the window period.

This is also great, can not idle ah, smart little Horse brother a pat head, set up TT cloud team. Through years of efforts to develop pay-per-view cloud, elastic IP, shared bandwidth and other products for small and medium-sized enterprises.

Failover

Xiaoming students feel this keyboard is good, happily click to buy the button, suddenly jumped to the landing page.

What the hell? I’m out of my pants, and this is what you show me? The average user will probably see no problem and just log in again. But Ming, being a serious programmer, wanted to find out what was going on.

After careful data analysis, Xiao Ming came to the following conclusions:

Xiao Ming thought that the service he ordered had hung up and the request was sent to another service. But why did he jump to the login page? As a programmer, Xiao Ming clearly knows that services are divided into stateful and stateless. Although our NORMAL HTTP request is stateless, the user status is generally determined through cookie or session.

Here, you see the officer should understand what is a ghost. Taking Tomcat as an example, our user information is stored in the session, and the session is stored in Tomcat memory. The browser authenticates the server with the JSESSIONID in the cookie.

However, the server hung up and the order was sent to another service, so Ming could no longer find his session.

Xiao Ming feedback the problem to TT Cat, Xiao Ma saw that it was ok, the cluster has done more than this, so quickly asked engineers to come up with a solution.

The engineers eventually came up with two solutions:

  • Server user state replication (high cost, hardware and software support, delay, and risk of failure)

  • Unified storage of user status (I don’t talk, I just laugh)

Eventually, the engineers switched to a second solution, using Redis to store user status data.

Knowledge supplement

Recently, I contacted and used the load balancing SLB of Ali Cloud, and had a general understanding of the load balancing implementation of TT cat. The following architecture implementation comes from TT cat.

Load balancing in cluster deployment, session synchronization is implemented to eliminate single point of failure of servers, improve redundancy, and ensure service stability. Ali Cloud currently provides layer 4 (TCP and UDP) and layer 7 (HTTP and HTTPS) load balancing services.

  • The fourth layer adopts open source software LVS (Linux Virtual Server) + Keepalived to achieve load balancing.

  • Layer 7 uses Tengine for load balancing.

As shown in the figure below, the four-tier load balancing in each region is actually run by multiple LVS machines deployed into an LVS cluster. Cluster deployment ensures the availability, stability, and scalability of load balancing services under abnormal conditions.

Each LVS in the LVS cluster has a session. Multicast packets are synchronized to other LVS in the cluster to achieve session synchronization. As shown in the figure below, after the client sends three packets to the server, session A established on LVS1 starts to synchronize to the other LVS machines. The solid line represents the existing connection, and the dotted line represents the flow to a functioning machine, LVS2, when LVS1 fails or is being maintained. Therefore, the load balancing cluster supports hot upgrade and is transparent to users in case of machine faults and cluster maintenance without affecting user services.

Not enough? More wonderful technology dry goods

2017 Gdevops guangzhou station!

I invite you to join me on November 24th

Books to the

Leave resonating insights in the comments section of dbaPlus on wechat and become the most liked one at 12 noon the next day after this article is published. You can receive the following book

Special thanks to blogview for providing book sponsorship for the event.

Recent hot articles:

Two hours! An emergency SQL optimization caused by a collection of permission types

A preliminary study on building distributed real-time processing application based on Storm

It is not difficult to build a Web site from scratch by understanding this architecture evolution diagram!

How to make HDFS more efficient by storing data in layers based on how hot or cold it is?

Ctrip ticket ElasticSearch cluster operation and maintenance tame

The door to a new world opened