Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 10 of 10 · Django Deployment & DevOps
Interview question

How do you scale a Django application horizontally, and what state must move out of the app servers? Django एप्लिकेशन को हॉरिज़ॉन्टली कैसे स्केल करें, और कौन-सी स्टेट को ऐप सर्वर्स से बाहर ले जाना चाहिए?

Answer

Horizontal scaling means running multiple identical Django application instances behind a load balancer instead of making one server bigger, allowing traffic to be distributed and instances to be added or removed based on load. For this to work correctly, application servers must be stateless — any request could be routed to any instance, so nothing critical can be stored only in one instance's local memory or disk.

This means sessions must be stored in a shared backend (database or Redis, not the default local-memory cache backend) rather than per-instance memory, uploaded media files must go to shared object storage (S3 or equivalent) rather than local disk, and any in-process cache must be replaced with a shared cache like Redis so all instances see consistent cached data. Sticky sessions at the load balancer are sometimes used as a workaround but reintroduce the same scaling and failover problems statelessness is meant to avoid.

SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
CACHES = {'default': {'BACKEND': 'django_redis.cache.RedisCache', 'LOCATION': 'redis://redis:6379/1'}}
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

हॉरिज़ॉन्टल स्केलिंग का मतलब है एक सर्वर को बड़ा बनाने के बजाय लोड बैलेंसर के पीछे कई समान Django एप्लिकेशन इंस्टेंस चलाना। इसके सही ढंग से काम करने के लिए, एप्लिकेशन सर्वर्स को स्टेटलेस होना चाहिए।

इसका मतलब है कि सेशन्स को एक साझा बैकएंड (डेटाबेस या Redis) में स्टोर किया जाना चाहिए, अपलोड की गई मीडिया फाइलें साझा ऑब्जेक्ट स्टोरेज (S3) में जानी चाहिए, और कोई भी इन-प्रोसेस कैश Redis जैसे साझा कैश से बदला जाना चाहिए।

SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
CACHES = {'default': {'BACKEND': 'django_redis.cache.RedisCache', 'LOCATION': 'redis://redis:6379/1'}}
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

Was this answer clear?