using nginx as http load balancer

Introduction

Load balancing across multiple application instances is a commonly used technique for optimizing resource utilization, maximizing throughput, reducing latency, and ensuring fault-tolerant configurations.

It is possible to use nginx as a very efficient HTTP load balancer to distribute traffic to several application servers and to improve performance, scalability and reliability of web applications with nginx.

Load balancing methods

The following load balancing mechanisms (or methods) are supported in nginx:

  • round-robin — requests to the application servers are distributed in a round-robin fashion,
  • least-connected — next request is assigned to the server with the least number of active connections,
  • ip-hash — a hash-function is used to determine what server should be selected for the next request (based on the client’s IP address).

Default load balancing configuration

The simplest configuration for load balancing with nginx may look like the following:

http {
    upstream myapp1 {
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
        }
    }
}

In the example above, there are 3 instances of the same application running on srv1-srv3. When the load balancing method is not specifically configured, it defaults to round-robin. All requests are proxied to the server group myapp1, and nginx applies HTTP load balancing to distribute the requests. Continue reading “using nginx as http load balancer”

sql server 分区表

IF EXISTS(
       SELECT 1
       FROM   sys.databases AS d
       WHERE  d.name = 'Test_1'
   )
    DROP DATABASE Test_1
 GO
 CREATE DATABASE [Test_1] ON PRIMARY(
                                        NAME
                                        =
                                        N'Test_1',
                                        FILENAME
                                        =
                                        N'E:\Database\Sharding\Test_1.mdf',
                                        SIZE
                                        =
                                        10240KB,
                                        MAXSIZE
                                        =
                                        UNLIMITED,
                                        FILEGROWTH
                                        =
                                        1024Kb
                                    ),
 FILEGROUP [Test_A](
                       NAME = N'Test_A',
                       FILENAME = 'E:\Database\Sharding\Test_A.ndf',
                       SIZE = 10240kb,
                       MAXSIZE = UNLIMITED,
                       FILEGROWTH = 1024kB
                   ),
 FILEGROUP [Test_B](
                       NAME = N'Test_B',
                       FILENAME = 'E:\Database\Sharding\Test_B.ndf',
                       SIZE = 10240kb,
                       MAXSIZE = UNLIMITED,
                       FILEGROWTH = 1024kB
                   )
 LOG ON (
            NAME = N'Test_log',
            FILENAME = 'E:\Database\Sharding\Test_Log.ldf',
            SIZE = 10240kb,
            MAXSIZE = UNLIMITED,
            FILEGROWTH = 1024kB
        ) COLLATE Chinese_PRC_CI_AS

GO
 USE [Test_1]

IF EXISTS(
       SELECT 1
       FROM   sys.partition_functions AS pf
       WHERE  pf.name = 'TEST_PART'
   )
    DROP PARTITION FUNCTION [TEST_PART]
 GO
 -- set a partiton condition
 CREATE PARTITION FUNCTION TEST_PART(INT) AS RANGE LEFT FOR VALUES(2)
 GO
IF EXISTS(
       SELECT 1
       FROM   sys.partition_schemes AS ps
       WHERE  ps.name = 'TEST_SCH'
   )
    DROP PARTITION SCHEME [TEST_SCH]
 GO
 --set a scheme for table using.
 CREATE PARTITION SCHEME TEST_SCH
 AS PARTITION [TEST_PART] TO (Test_A, Test_B)
 GO
IF OBJECT_ID('STUDENT', 'U') IS NOT NULL
    DROP TABLE STUDENT
 GO
CREATE TABLE STUDENT
(
	Id        INT IDENTITY(1, 1) NOT NULL,
	NAME      VARCHAR(10) NULL,
	CLASS     INT NULL,
	GRADE     INT NULL
) ON TEST_SCH(Id) -- set scheme
 GO
INSERT INTO STUDENT
  (
    -- Id -- this column value is auto-generated
    NAME,
    CLASS,
    GRADE
  )
VALUES
  (
    'A',
    1,
    1
  );
INSERT INTO STUDENT
  (
    -- Id -- this column value is auto-generated
    NAME,
    CLASS,
    GRADE
  )
VALUES
  (
    'B',
    2,
    2
  );
INSERT INTO STUDENT
  (
    -- Id -- this column value is auto-generated
    NAME,
    CLASS,
    GRADE
  )
VALUES
  (
    'C',
    3,
    3
  );
INSERT INTO STUDENT
  (
    -- Id -- this column value is auto-generated
    NAME,
    CLASS,
    GRADE
  )
VALUES
  (
    'D',
    4,
    4
  )
 GO