Understanding Web server and Database load balancing and reverse proxy _ server Other

Source: Internet
Author: User
Tags database load balancing http request nslookup command linux mint

But if the site has an average of more than 200 requests per second, then the problem is: This is already the best Web server, what should I do? The same scenario applies to the database. To solve this problem, we need to understand the principle of "load balancing".

How Web servers do load Balancing

The most important way to do load balancing for Web servers is DNS redirection and reverse proxy, and the other way it works is similar.

We ping Baidu many times, we will find that the IP of the reply will be different, for example, the result of the first time is:

Copy Code code as follows:

Ping baidu.com [220.181.111.86] has 32 bytes of data:
Reply from 220.181.111.86: Byte =32 time =27ms ttl=51
Reply from 220.181.111.86: Byte =32 time =27ms ttl=51
Reply from 220.181.111.86: Byte =32 time =27ms ttl=51

After a second ping, the result may be changed:
Copy Code code as follows:

Ping baidu.com [220.181.111.85] has 32 bytes of data:
Reply from 220.181.111.85: Byte =32 time =27ms ttl=51
Reply from 220.181.111.85: Byte =32 time =27ms ttl=51
Reply from 220.181.111.85: Byte =32 time =29ms ttl=51

Use the nslookup command to see multiple IP and baidu.com correspondence. Here is the use of DNS redirection technology, the principle is simple: The DNS server to save a domain name corresponding to multiple IP, the client issued a DNS request, the DNS server to send IP back to the client according to the algorithm, sent back to the general is a collection of IP addresses, but each time the order is different, The first IP is 201.11.11.1, the second first may be 201.11.11.2, the client is using the first ip--simply, the IP of the domain name that the client obtains each time may be different. Different IPs correspond to different Web servers, but the content of these Web servers should be the same.

We understand the reverse proxy from the following figure:



The client sends an HTTP request message to the reverse proxy (if the site has a domain name, domain name IP is a reverse proxy server extranet IP, the reverse proxy will send the request message to a Web server randomly, the Web server will send the HTTP response message to the reverse proxy, the reverse proxy to return this message to the client. Since this is so simple, we can begin to implement a simple reverse proxy.

Install Apache and nginx servers under Linux Mint 15 and create a file index.html under the Apache 80-port document root, which reads as follows:

Copy Code code as follows:

<title>index</title>
<body>
</body>

Create a file index.html under Nginx's 8080-port document root, which reads as follows:
Copy Code code as follows:

<title>index</title>
<body>
</body>

Create the source file simple_reverse_proxy.py, which reads as follows:
Copy Code code as follows:

#!/usr/bin/python
#-*-encoding:utf8-*-
'''
This is a simple reverse proxy server
'''
Import Basehttpserver
Import Urllib2
HOST_NAME = ' 127.0.0.1 '
Port_number = 8081 #端口
Server_url= (' http://127.0.0.1:80 ', ' http://127.0.0.1:8080 ')
Server_choice = 0
Class MyHandler (Basehttpserver.basehttprequesthandler):
def do_get (s):
"" "Response to a GET request" ""
Global Server_choice
url = server_url[server_choice]
Print URL
Server_choice = (server_choice + 1)% 2
headers = {' user-agent ': ' mozilla/4.0 ' (compatible; MSIE 5.5; Windows NT) '}
Try
req = Urllib2. Request (URL, None, headers)
Response = Urllib2.urlopen (req)
html = Response.read ()
#print html
S.send_response (200);
S.send_header ("Content-type", "text/html")
S.end_headers ()
S.wfile.write (HTML)
Except
S.send_response (404);
S.send_header ("Content-type", "text/html")
S.end_headers ()
S.wfile.write (' if __name__ = = ' __main__ ':
Server_class = Basehttpserver.httpserver
httpd = Server_class ((host_name, port_number), MyHandler)
Try
Httpd.serve_forever ()
Except Keyboardinterrupt:
Pass
Httpd.server_close ()

Start Apache, Nginx, and run simple_reverse_proxy.py. We open the http://127.0.0.1:8081 in the browser and we can see:


Refresh to see:



And simple_reverse_proxy.py will have the following information output:
Copy Code code as follows:

Bash >>/simple_reverse_proxy.py
Http://127.0.0.1:80
127.0.0.1--[05/sep/2013 19:25:02] "get/http/1.1" 200-
http://127.0.0.1:8080
127.0.0.1--[05/sep/2013 19:25:43] "get/http/1.1" 200-

Of course, there are already a lot of good reverse proxy servers in the open source world, such as Nginx.

As long as you understand the principle of reverse proxy, more complex architecture is easy to implement.

Load Balancing of databases

For a large web site, a database system will certainly encounter the inability to afford a large number of read requests, write requests. So how do we achieve high concurrent read and write requests through load balancing?

One of the best ways to do this is to read and write a separate read-write request to a database server, send a write request to one (or more) database server, send a read request to another server (or servers), which can significantly improve response time. But one of the difficulties is that you have to keep the data in multiple database servers consistent, and you don't have to worry that many database systems have implemented this functionality. The following is an example of a schema:

In fact, there is a problem with writing a conflict, imagine the following scenario:

The system is used to store user registration information for a Web site that does not allow the user name to be the same and the user name is the only primary key, so transaction processing must be involved in a single database schema. Now in this load-balanced database schema, user A registers a user named Xiaoming with the write request assigned to DB server 1, while User B also registers the username xiaoming, and if the write request is assigned to DB Server1, no problem occurs. But what if it's assigned to DB server 2? Two DB server holds the same user information for different user names! The solution is simple, the allocation of write requests can not be a random algorithm, should use hash map, such as the registered username first letter x, write requests assigned to the DB Server2, all other write requests are assigned to DB server 1.

Another problem is that this architecture provides a great deal of flexibility for development applications, that is, this architecture does not apply to some ORM frameworks, and the solution is to add a layer--"database broker" to the architecture. For MySQL, for example, there is a solution like MySQL proxy.

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.