📜 ⬆️ ⬇️

Shorts - short and funny, or how we prepared the site for the Habraeffect



Namba 0: Introduction

One day, thoughtfully flipping through the bash, I suddenly realized that most quotations are quite long and very often they are just too lazy to read. In this age of universal haste, it takes a long time. Thus, a new format was born: Shorts (from the English word Short - short). Short is a short (from one or two sentences) joke, strictly limited to 255 characters (so that it does not happen).

Just at that moment I wanted to learn programming and was looking for a simple task. I quickly added a website pretty quickly - for a couple of evenings, I told my work colleagues and a couple of acquaintances on IRC about him, and I was about to forget about him when I discovered that in two days 500 people had visited the site, of whom almost a third subscribed RSS. It became clear that people liked the concept. Having slightly licked the site externally, I decided to show it to the Habrasoobshchestvo - Shorts - short and funny , please love and favor.
')
PS Considering that this is not quite a startup (the project is still non-profit, and “startup” is too loud a word for my crafts), the habrasovest acquired over the years of my stay on the site did not allow me to write a non-technical article. Therefore, under the cut you will find an entertaining story about how we prepared Shorts for the Habraeffect.



Namba 1: Resources

Any web project, and especially a startup, always has sharp influx of visitors due to various reasons - publishing an article on a large resource (for example, on Habré), a press release, an advertising campaign, an unexpected mention in the news, and so on. Often (as in the last example) a surge in attendance occurs EXTREMELY ™.

This pushes for an important conclusion - to place a resource with a attendance of 100k unik a day on the initial VPS for 200 rubles, of course, is possible. This will be a big reason for pride in the circle of friends geeks, however, most likely, will lead to the fall of the site at the crucial moment. In general, it is good when the web-production system works in the usual mode for no more than 10% of its capacity. This will allow it to withstand a surge in attendance. For all these reasons, even such a “light” website like Shorts is better to put on a server with a decent amount of power.

Namba 2: FrontEnd and Backend

One of the first commandments of web optimization is to separate static and dynamic content. We will make this a standard solution: Nginx is on the frontend, Apache listens at 127.0.0.1. Nginx gives static content, and if it sees that the request goes to the dynamic one, it sends the request “inside” to the Apache:

server {

    listen     10.0.121.124:80;
    server_name   shortiki.com www.shortiki.com;

    #

    if ($host = 'www.shortiki.com' ) {
      rewrite ^/(.*)$ http://shortiki.com/$1 permanent;
      }

    access_log   /var/log/vhosts/nginx-shortiki.com-access.log main;

    # , . .

    location ~ ^.+\.(html|jpg|jpeg|gif|png|ico|css|js)$ {

    root /usr/home/vhosts/shortiki;
    expires 30d;
    access_log off;

    }

    location / {

    proxy_pass   http://127.0.0.1:8081;
    # ,
    }
}


* This source code was highlighted with Source Code Highlighter.


3: MPM

Apache MPM (Multi-Processing Modules, , - / , ) – prefork worker.

Prefork – , , . Prefork , worker.

Worker , , , . worker , , , , , . , , , , worker.

4: Accept-

Accept- ( ) , , - , HTTP .

FreeBSD, , :

# kldload accf_http

, :

echo 'accf_http_load="YES"' >> /boot/loader.conf

, :

AcceptFilter http httpready

nginx:

listen 10.0.121.124:80 default sndbuf=16k rcvbuf=8k accept_filter=httpready

-:

/usr/local/etc/rc.d/nginx reload
/usr/local/etc/rc.d/apache22 restart


5: Apache

Apache — . , , :

MaxClients – , , . MaxClients = 300, 301 , , , , . , MaxClients, – 300 . MaxClients :

MaxClients = /

Apache RSS top ps.

AllowOverride:

<Directory />
AllowOverride none
</Directory>


* This source code was highlighted with Source Code Highlighter.


, , , .htaccess.

ExtendedStatus ( 1 2 ):

ExtendedStatus Off

FollowSymLinks -, Apache :

<Directory />
Options FollowSymLinks
</Directory>


* This source code was highlighted with Source Code Highlighter.


:

Timeout 10

( ):

<Location />
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml application/x-javascript
</Location>


* This source code was highlighted with Source Code Highlighter.


MinSpareServers MaxSpareServers – , , « ». , , MinSpareServers 2, 2 , . , – , , , , , / .

MinSpareServers 2
MaxSpareServers 8


, 2 , , 8 – , .
:

MinSpareServers 8
MaxSpareServers 32


StartServers – , Apache . StartServers MinSpareServers, MinSpareServers. , :

StartServers 8

MaxRequestPerChild , . , . – , 10000 , 10000 , .

MaxRequestPerChild 3000

KeepAlive – , TCP , . KeepAliveTimeout , .. , .

KeepAlive On
KeepAliveTimeout 5


6: PHP

, php php memcache memcached , . FreeBSD :

cd /usr/ports/databases/pecl-memcache
make install clean

cd /usr/ports/databases/memcached
make install clean


7:

, — . , . SQL , 20 :

SELECT sid, sdate, stext, srating FROM quotes ORDER BY id ASC LIMIT $shortik_first, $shortiks_main

* This source code was highlighted with Source Code Highlighter.


, , :

// Memcached

$mem = new Memcache();
$mem->connect('localhost', 11211);

//

$quotesonpage = '';

//

if ( !$mem->get['s_main'] ) {

// - MySQL...

$connect = @mysql_connect ($server, $user, $pass) or die('Could not connect: ' . mysql_error());
@mysql_select_db("ShoDB");

$query = "SELECT sid, sdate, stext, srating FROM quotes ORDER BY id ASC LIMIT $shortik_first, $shortiks_main";

@mysql_set_charset('utf8',$connect);

$get_smain = mysql_query ($query) or die('Cannot execure query: ' . mysql_error());

$quotesonpage = array();
while ($shortik = mysql_fetch_assoc($get_smain)) {
 $quotesonpage[] = $shortik;
}

$quotesonpage = array_reverse($quotesonpage);

// ... memcache, (1800 ).

$mem->set('quotes', $quotesonpage,MEMCACHE_COMPRESSED,1800);
} else {
$quotesonpage = $mem->get['quotes'];
}


* This source code was highlighted with Source Code Highlighter.


, , MySQL . , , .

8: :)


! , , .

, , , , :)

Source: https://habr.com/ru/post/125973/


All Articles