AWS Django user sessions with stickiness load balancing disabled

I am using AWS Elastic Beanstalk with EC2 servers behind an elastic load balancer (ELB).

I have sticky sessions on ELB because this is the only way I can get django user sessions to work correctly. However, during peak traffic, this causes problems because the ELB no longer distributes each incoming request evenly. Usually this overloads 1 server, like mini DDOS.

What I would like to do is use server-side user sessions, where user authentication information is stored in my Redis cache. I tried setting SESSION_ENGINE to a lot of things like:

 SESSION_ENGINE = 'redis_sessions.session' SESSION_ENGINE = 'django.contrib.sessions.backends.cache' SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db' 

Then, when I disconnect sticky sessions, I can’t log in because requests end on different servers where some requests are authenticated and others are not. Those that are not redirect me back to the login page.

Here are some other relevant settings that I have:

 INSTALLED_APPS = ( ..., 'django.contrib.sessions', ..., ) MIDDLEWARE_CLASSES = ( ..., 'djangosecure.middleware.SecurityMiddleware', ..., 'django.contrib.sessions.middleware.SessionMiddleware', ..., 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', ..., ) 

What am I doing wrong? Thank you very much.

+5
source share
1 answer

Are you sure that all your web servers actually connect to the same common instance of redis, for example: on the network, for example, in AWS ElastiCache (and not by default something on their local hosts)?

If you use SESSION_ENGINE = 'django.contrib.sessions.backends.cache' , make sure that the cache that it uses is set to the redis cache that you configured in CACHES , possibly with SESSION_CACHE_ALIAS , if not default .

0
source

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


All Articles