← Back to Apache Course | Chapter 5: Performance | Lesson 1 of 4

KeepAlive सेटिंग

KeepAlive एक browser को same server के लिए कई requests के लिए एक single connection reuse करने देता है, हर single file के लिए बिल्कुल नया connection खोलने की बजाय -- जो dozens images, scripts, और stylesheets load करने वाले page पर बहुत मायने रखता है।

Connections Reuse करना क्यों मायने रखता है

एक TCP connection खोलने (और, HTTPS के लिए, इसके ऊपर एक TLS handshake पूरा करने) की असली overhead होती है। 40 assets वाला एक page जो हर एक के लिए एक नया connection खोलता उसे यह cost 40 बार pay करनी पड़ती। KeepAlive एक response के बाद connection को खुला रखता है ताकि अगली request इसे तुरंत reuse कर सके।

Directives

KeepAlive On feature को on कर देता है (modern Apache में यह default से on है)। MaxKeepAliveRequests cap करता है कि एक connection पर कितनी requests serve की जा सकती हैं इससे पहले कि इसे close किया जाए (0 का मतलब unlimited है)। KeepAliveTimeout set करता है कि एक idle connection पर एक और request के लिए Apache कितने seconds wait करे इससे पहले कि इसे close करे।

Trade-off

एक लंबा KeepAliveTimeout एक browser को page के कई assets में connections reuse करने में मदद करता है, लेकिन हर kept-alive connection एक worker/thread को busy रखता है, भले ही यह कुछ भी न कर रहा हो। एक busy server पर, बहुत लंबा timeout high traffic में available workers खत्म कर सकता है; बहुत छोटा value शुरू में KeepAlive का पूरा मतलब ही खत्म कर देता है।

Note: 2-5 seconds के बीच का KeepAliveTimeout ज़्यादातर sites के लिए एक common, sensible starting point है -- page के बाकी assets पकड़ने के लिए काफी लंबा, बिना ज़रूरत connections खुली रखने के लिए काफी छोटा।

यह MPM के साथ कैसे Interact करता है

एक लंबा KeepAliveTimeout कितना costly है यह इस पर बहुत depend करता है कि कौन सा Multi-Processing Module (MPM) इस्तेमाल में है -- अगले lesson में covered। prefork MPM हर kept-alive connection के लिए एक पूरी process बांध देता है, जिससे लंबा timeout costly हो जाता है; event को specifically keep-alive connections को कहीं ज़्यादा efficiently handle करने के लिए design किया गया था।

उदाहरण: A sensible KeepAlive configuration

apacheconf
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
{# 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. एक busy server पर KeepAliveTimeout बहुत ज़्यादा (30+ seconds) set करना, idle connections पर workers बांधते हुए और server actually कितने concurrent visitors serve कर सकता है यह कम करते हुए।
  2. चीज़ों को "simplify" करने के लिए KeepAlive को पूरी तरह disable करना, जो आमतौर पर real-world page loads को धीमा कर देता है क्योंकि अब हर asset को अपना नया connection चाहिए।
  3. यह consider किए बिना KeepAliveTimeout tune करना कि कौन सा MPM active है -- same value की cost prefork बनाम event के under बहुत अलग होती है।
🔒

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.