How to scale a website-My Approach

ELB  ---> Nginx servers ---> one Varnish Server  ---> Return if cache is found.

The problem I met is that /pop_service/{channel_id}/points will get burst hit by users. Thus, the load of the server will increase dramatically.

The solution to this problem is that ELB(Amazon Load Balancing Server) send matched requests to Nginx servers.

upstream backend {
    hash $request_uri consistent;
    server 104.207.149.81;
    server 45.63.83.59;
}

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        location / {
            proxy_pass http://backend;
        }

}
Nginx servers with the above configuration have the ability to route requests based on request URI. AKA, the same URI gets redirects to the same server.


The config file of varnish is the following:


vcl 4.0;

import directors;

backend server1 {
    .host = "104.207.149.81";
    .port = "80";
    .probe = {
        .url = "/";
        .timeout = 1s;
        .interval = 5s;
        .window = 5;
        .threshold = 3;
    }
}

backend server2 {
    .host = "45.63.83.59";
    .port = "80";
    .probe = {
        .url = "/";
        .timeout = 1s;
        .interval = 5s;
        .window = 5;
        .threshold = 3;
    }
}

sub vcl_init {
    new bar = directors.round_robin();
    bar.add_backend(server1);
    bar.add_backend(server2);
}

acl purge {
        "localhost";
        "192.168.55.0"/24;
    #the public ip of our office
    "38.88.216.18";
}

sub vcl_recv {
    # send all traffic to the bar director:
    set req.backend_hint = bar.backend();

    unset req.http.cookie;
    unset req.http.Cache-Control;
    if (req.method == "PURGE") {
    if (!client.ip ~ purge) {
        return(synth(405,"Not allowed."));
    }
    return (purge);
    }
}

sub vcl_backend_response {
      unset beresp.http.cookie;
      unset beresp.http.Set-Cookie;
      unset beresp.http.Cache-Control;
}

sub vcl_deliver {
}

Varnish round-robin requests to backend servers with health check and purge capabilities

Comments

Popular posts from this blog

All the pitfalls I met in the first month