{"id":76010,"date":"2020-01-20T12:51:30","date_gmt":"2020-01-20T07:21:30","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=76010"},"modified":"2024-04-12T14:17:44","modified_gmt":"2024-04-12T08:47:44","slug":"scaling-2","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/scaling-2\/","title":{"rendered":"Scaling"},"content":{"rendered":"<p>Now that you know how to get Django running on a single server, let\u2019s look at how you can scale out a Django installation. This section walks through how a site might scale from a single server to a large-scale cluster that could serve millions of hits an hour.<\/p>\n<p>It\u2019s important to note, however, that nearly every large site is large in different ways, so scaling is anything but a one-size-fits-all operation. The following coverage should suffice to show the general principle, and whenever possible we\u2019ll try to point out where different choices could be made. First off, we\u2019ll make a pretty big assumption and exclusively talk about scaling under Apache and mod_python. Though we know of a number of successful medium- to large-scale FastCGI deployments, we\u2019re much more familiar with Apache.<\/p>\n<p><strong>Running on a Single Server<\/strong> &#8211; Most sites start out running on a single server, with an architecture that looks something like Figure 20-1.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-67860\" src=\"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/image029.png\" alt=\"image029\" width=\"129\" height=\"227\"><\/p>\n<p>Figure 20-1: a single server Django setup.<\/p>\n<p>&nbsp;<\/p>\n<p>This works just fine for small- to medium-sized sites, and it\u2019s relatively cheap \u2014 you can put together a single-server site designed for Django for well under $3,000. However, as traffic increases you\u2019ll quickly run into <em>resource contention<\/em> between the different pieces of software. Database servers and Web servers <em>love<\/em> to have the entire server to themselves, so when run on the same server they often end up \u201cfighting\u201d over the same resources (RAM, CPU) that they\u2019d prefer to monopolize. This is solved easily by moving the database server to a second machine.<\/p>\n<p><strong>Separating Out the Database Server<\/strong><\/p>\n<p class=\"VSKILLbodytext\"><span lang=\"EN-US\">As far as Django is concerned, the process of separating out the database server is extremely easy: you\u2019ll simply need to change the DATABASE_HOST setting to the IP or DNS name of your database server. It\u2019s probably a good idea to use the IP if at all possible, as relying on DNS for the connection between your Web server and database server isn\u2019t recommended. With a separate database server, <\/span><span lang=\"EN-US\">our architecture now looks like Figure 13-2.<\/span><\/p>\n<p class=\"VSKILLbodytext\"><span lang=\"EN-US\">&nbsp;<\/span><\/p>\n<p class=\"VSKILLbodytext\"><span lang=\"EN-US\">Here we\u2019re starting to move into what\u2019s usually called n-tier architecture. Don\u2019t be scared by the buzzword \u2013 it just refers to the fact that different tiers of the Web stack get separated out onto different physical machines.<\/span><\/p>\n<p class=\"VSKILLbodytext\"><span lang=\"EN-US\">&nbsp;<\/span><span lang=\"EN-US\">At this point, if you anticipate ever needing to grow beyond a single database server, it\u2019s probably a good idea to start thinking about connection pooling and\/or database replication. Unfortunately, there\u2019s not nearly enough space to do those topics justice in this book, so you\u2019ll need to consult your database\u2019s documentation and\/or community for more information.<\/span><\/p>\n<p class=\"VSKILLbodytext\"><span lang=\"EN-US\">&nbsp;<\/span><\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-67861\" src=\"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/image031.png\" alt=\"image031\" width=\"128\" height=\"281\"><\/strong><\/p>\n<p class=\"VSKILLbodytext\" style=\"text-align: center;\" align=\"center\"><span lang=\"EN-US\">Figure : Moving the database onto a dedicated server.<\/span><\/p>\n<p><strong>Running a Separate Media Server<\/strong><\/p>\n<p>We still have a big problem left over from the single-server setup: the serving of media from the same box that handles dynamic content. Those two activities perform best under different circumstances, and by smashing them together on the same box you end up with neither performing particularly well. So the next step is to separate out the media \u2014 that is, anything <em>not<\/em> generated by a Django view \u2014 onto a dedicated server.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-67862\" src=\"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/image033-1.png\" alt=\"image033\" width=\"263\" height=\"263\"><\/p>\n<p>Figure 20-3: Separating out the media server.<\/p>\n<p>&nbsp;<\/p>\n<p>Ideally, this media server should run a stripped-down Web server optimized for static media delivery. lighttpd and tux (http:\/\/www.djangoproject.com\/r\/tux\/) are both excellent choices here, but a heavily stripped down Apache could work, too. For sites heavy in static content (photos, videos, etc.), moving to a separate media server is doubly important and should likely be the <em>first<\/em> step in scaling up.<\/p>\n<p>This step can be slightly tricky, however. Django\u2019s admin needs to be able to write uploaded media to the media server (the MEDIA_ROOT setting controls where this media is written). If media lives on another server, however, you\u2019ll need to arrange a way for that write to happen across the network.<\/p>\n<p><strong>Implementing Load Balancing and Redundancy<\/strong><\/p>\n<p>At this point, we\u2019ve broken things down as much as possible. This three-server setup should handle a very large amount of traffic \u2014 we served around 10 million hits a day from an architecture of this sort \u2014 so if you grow further, you\u2019ll need to start adding redundancy.<\/p>\n<p>This is a good thing, actually. One glance at Figure 20-3 shows you that if even a single one of your three servers fails, you\u2019ll bring down your entire site. So as you add redundant servers, not only do you increase capacity, but you also increase reliability.<\/p>\n<p class=\"VSKILLbodytext\"><span lang=\"EN-US\">It\u2019s relatively easy to get multiple copies of a Django site running on different hardware \u2013 just copy all the code onto multiple machines, and start Apache on all of them. However, you\u2019ll need another piece of software to distribute traffic over your multiple servers: a load balancer.<\/span><\/p>\n<p class=\"VSKILLbodytext\"><span lang=\"EN-US\">&nbsp;<\/span><span lang=\"EN-US\">You can buy expensive and proprietary hardware load balancers, but there are a few high-quality open source software load balancers out there. Apache\u2019s mod_proxy is one option, but we\u2019ve found Perlbal to be fantastic. It\u2019s a load balancer and reverse proxy written by the same folks who wrote memcached.<\/span><\/p>\n<p>With the Web servers now clustered, our evolving architecture starts to look more complex, as shown in Figure 20-4.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-67863\" src=\"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/image035-1.png\" alt=\"image035\" width=\"362\" height=\"362\"><\/p>\n<p>Figure 20-4: A load-balanced, redundant server setup.<\/p>\n<p>&nbsp;<\/p>\n<p>Notice that in the diagram the Web servers are referred to as a \u201ccluster\u201d to indicate that the number of servers is basically variable. Once you have a load balancer out front, you can easily add and remove back-end Web servers without a second of downtime.<\/p>\n<p><strong>Going Big<\/strong><\/p>\n<p>At this point, the next few steps are pretty much derivatives of the last one:<\/p>\n<ul>\n<li>As you need more database performance, you\u2019ll need to add replicated database servers. MySQL includes built-in replication; PostgreSQL users should look into Slony (http:\/\/www.djangoproject.com\/r\/slony\/) and pgpool (http:\/\/www.djangoproject.com\/r\/pgpool\/) for replication and connection pooling, respectively.<\/li>\n<li>If the single load balancer isn\u2019t enough, you can add more load balancer machines out front and distribute among them using round-robin DNS.<\/li>\n<li>If a single media server doesn\u2019t suffice, you can add more media servers and distribute the load with your load-balancing cluster.<\/li>\n<li>If you need more cache storage, you can add dedicated cache servers.<\/li>\n<li>At any stage, if a cluster isn\u2019t performing well, you can add more servers to the cluster.<\/li>\n<li>After a few of these iterations, a large-scale architecture might look like Figure 20-5.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-67859\" src=\"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/image037.png\" alt=\"image037\" width=\"569\" height=\"353\"><\/p>\n<p>Figure 20-5. An example large-scale Django setup.<\/p>\n<p>&nbsp;<\/p>\n<p>Though we\u2019ve shown only two or three servers at each level, there\u2019s no fundamental limit to how many you can add.<\/p>\n\n\n<p><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/certified-django-developer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Back to Tutorial<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now that you know how to get Django running on a single server, let\u2019s look at how you can scale out a Django installation. This section walks through how a site might scale from a single server to a large-scale cluster that could serve millions of hits an hour. It\u2019s important to note, however, that&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8655],"tags":[2678],"class_list":["post-76010","page","type-page","status-publish","hentry","category-django-web-development","tag-scaling"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Scaling - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/scaling-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Scaling - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Now that you know how to get Django running on a single server, let\u2019s look at how you can scale out a Django installation. This section walks through how a site might scale from a single server to a large-scale cluster that could serve millions of hits an hour. It\u2019s important to note, however, that...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/scaling-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:47:44+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/image029.png\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/scaling-2\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/scaling-2\/\",\"name\":\"Scaling - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/scaling-2\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/scaling-2\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/image029.png\",\"datePublished\":\"2020-01-20T07:21:30+00:00\",\"dateModified\":\"2024-04-12T08:47:44+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/scaling-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/scaling-2\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/scaling-2\/#primaryimage\",\"url\":\"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/image029.png\",\"contentUrl\":\"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/image029.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/scaling-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scaling\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Scaling - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/scaling-2\/","og_locale":"en_US","og_type":"article","og_title":"Scaling - Tutorial","og_description":"Now that you know how to get Django running on a single server, let\u2019s look at how you can scale out a Django installation. This section walks through how a site might scale from a single server to a large-scale cluster that could serve millions of hits an hour. It\u2019s important to note, however, that...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/scaling-2\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:47:44+00:00","og_image":[{"url":"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/image029.png","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/scaling-2\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/scaling-2\/","name":"Scaling - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/scaling-2\/#primaryimage"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/scaling-2\/#primaryimage"},"thumbnailUrl":"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/image029.png","datePublished":"2020-01-20T07:21:30+00:00","dateModified":"2024-04-12T08:47:44+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/scaling-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/scaling-2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/scaling-2\/#primaryimage","url":"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/image029.png","contentUrl":"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/image029.png"},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/scaling-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Scaling"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/76010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=76010"}],"version-history":[{"count":4,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/76010\/revisions"}],"predecessor-version":[{"id":83481,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/76010\/revisions\/83481"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=76010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=76010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=76010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}