← Back to Apache Course | Chapter 7: Advanced | Lesson 2 of 4

Load Balancing क्या है

जब एक single backend server सारा traffic handle नहीं कर सकता (या आप redundancy चाहते हैं अगर एक down हो जाए), Apache सिर्फ एक को proxy करने की बजाय requests को कई backend servers में फैला सकता है -- यही load balancing है।

mod_proxy_balancer

Load balancing mod_proxy पर बनता है, mod_proxy_balancer plus एक specific balancing algorithm module (default round-robin approach के लिए mod_lbmethod_byrequests) add करते हुए। Ubuntu पर: sudo a2enmod proxy proxy_http proxy_balancer lbmethod_byrequests।

एक Balancer Cluster Define करना

एक <Proxy "balancer://mycluster"> block backend servers को BalancerMember lines की तरह list करता है, और ProxyPass "/" "balancer://mycluster/" traffic को एक fixed address की बजाय cluster पर भेजता है। Apache फिर आने वाली requests को हर listed member में distribute कर देता है।

Balancing Methods

byrequests (default) requests को round-robin फैलाता है, optionally हर BalancerMember पर एक loadfactor से weighted। bytraffic request count की बजाय transfer हुए bytes से balance करता है। bybusyness नई requests को उस backend पर भेजता है जिसकी अभी सबसे कम active requests हों, जो uneven request processing times वाले backends को plain round-robin से बेहतर handle करता है।

Session Persistence और Health Checks

अगर application session data को (Redis जैसे shared store की बजाय) एक single server पर memory में store करता है, किसी given user की requests को same backend पर landing करते रहना ज़रूरी है -- BalancerMember इसके लिए route-based "sticky sessions" support करता है। BalancerMember ... retry=60 Apache को fail होते backend को requests भेजना automatically रोकने भी देता है, दिए गए seconds के बाद इसे दोबारा try करते हुए।

Note: जहाँ भी possible हो, application backends को stateless रखें (session data Redis जैसे shared store में, in-process memory में नहीं) -- यह sticky sessions की ज़रूरत पूरी तरह हटा देता है और load balancing को simpler और ज़्यादा resilient बना देता है।

उदाहरण: A simple two-backend load balancer

apacheconf
<Proxy "balancer://mycluster">
    BalancerMember "http://127.0.0.1:8001"
    BalancerMember "http://127.0.0.1:8002"
</Proxy>

ProxyPass "/" "balancer://mycluster/"
ProxyPassReverse "/" "balancer://mycluster/"
{# Flagged by hand after confirming a runner can't handle this example (a shell command / go.mod file stored as a TopicExample, a language feature the configured runner version doesn't support, or output that blows a runner's sandbox limit) -- see TopicExample.norun. Never render the run button for these, regardless of language, since it would just fail at execute_code (or worse, hang the Judge0 queue on a submission that can never finish cleanly). #}

⚠️ This example can't run in the browser editor. Try it in your own local environment instead.

{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. local memory में session state रखने वाले backends में, sticky sessions के बिना, load balance करना -- जब भी request अलग backend पर land करे users log out हो जाते हैं या data खो देते हैं।
  2. mod_proxy_balancer और lbmethod module enable करना भूल जाना, ताकि ProxyPass में balancer:// addresses बस resolve ही न हों।
  3. कोई retry/health-check behavior configure न करना, ताकि Apache traffic का एक हिस्सा उस backend को भेजता रहे जो पहले से down है।
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.