django Architecture - Connection Management

  Рет қаралды 40,917

Hussein Nasser

Hussein Nasser

Жыл бұрын

In this video I explore Django’s architecture especially related to backend connection management and persistent connections.
docs.djangoproject.com/en/4.1...
Fundamentals of Networking for Effective Backends udemy course (link redirects to udemy with coupon)
network.husseinnasser.com
Fundamentals of Database Engineering udemy course (link redirects to udemy with coupon)
database.husseinnasser.com
Introduction to NGINX (link redirects to udemy with coupon)
nginx.husseinnasser.com
Python on the Backend (link redirects to udemy with coupon)
python.husseinnasser.com
Become a Member on KZbin
/ @hnasr
Buy me a coffee if you liked this
www.buymeacoffee.com/hnasr
Arabic Software Engineering Channel
/ @husseinnasser
🔥 Members Only Content
• Members-only videos
🏭 Backend Engineering Videos in Order
backend.husseinnasser.com
💾 Database Engineering Videos
• Database Engineering
🎙️Listen to the Backend Engineering Podcast
husseinnasser.com/podcast
Gears and tools used on the Channel (affiliates)
🖼️ Slides and Thumbnail Design
Canva
partner.canva.com/c/2766475/6...
Stay Awesome,
Hussein

Пікірлер: 72
@phantomrex1348
@phantomrex1348 Жыл бұрын
I feel like Hussain is one of the only KZbinrs that actually puts hours into his work. Keep up the good job
@muhammadahsan5676
@muhammadahsan5676 Жыл бұрын
I agree
@MartinsTalbergs
@MartinsTalbergs Жыл бұрын
Feel ya, men must do work to be men
@luuc
@luuc Жыл бұрын
'hours' is peanuts when it comes to content creation lol
@fadious_padious2711
@fadious_padious2711 Жыл бұрын
I feel like he is also one of the few tech KZbinrs who knows his stuff inside and out. There are definitely others but the majority of tech channels are new grads reading snippets out of the documentation.
@samydriss5223
@samydriss5223 Жыл бұрын
Hussein, I love and appreciate all the content you put out. You truly are a rare breed of teacher. I can't express my gratitude enough as well as how much you have taught and have helped me throughout my career. I always learn something new when I watch your videos, take your courses, listen to your podcasts or read your articles. You have this great ability to go very deep into the technology stack without confusing people. Really, not all heroes wear capes. Also, are we seeing a teaser for a Udemy course here? 👀 Salam from 🇩🇿
@abiiranathan
@abiiranathan Жыл бұрын
I believe the problem is not with Django per say but python's threading model. Yes you guessed right, the GIL. Also to avoid data races, Django has to execute the queries synchronously. Even using async frameworks like django channel, you must wrap your async coroutines with synchronous helpers. Hello GIL
@pleli
@pleli Жыл бұрын
I'm guessing multithearding in python deserve a video of its own. Django usually used behind a wsgi server (nginx, gunicorn etc.) In a way you will have multiple processes with single thread on each process.
@isurucumaranathunga
@isurucumaranathunga Жыл бұрын
Hussain I don't hv words to say, these stuff are absolutely important and these are the actual engineering stuff. I love this content and really appreciate your effort on these videos. There stuff are really important for everyone specially for fresh graduates like me. Again thank you so much. Watching and noting down your videos is my new hobby. Thanks again.
@SsaliJonathan
@SsaliJonathan Жыл бұрын
Thanks for making this video Hussein.
@MakeItStik
@MakeItStik Жыл бұрын
Really informative video. Make more like these !! We love it.
@aashayamballi
@aashayamballi Жыл бұрын
Django is ❤ Thank you Hussein for talking about Django and covering about this topic...
@matrixtoogood5601
@matrixtoogood5601 Жыл бұрын
Thank you for this amazing video! I would love to see a video on frontend listening models.
@_overide
@_overide Жыл бұрын
As always, great video. Thanks you so much, learned few new things!
@KostasPanagias
@KostasPanagias 4 ай бұрын
Great content! Very well explained! Thanks for all the effort you put into this.
@mustaphab32
@mustaphab32 Жыл бұрын
Great stuff! it think it worthes mentioning that ORM only lately started being async in a very recent version, which means the thread will keep the request waiting and any other calculation as-well! we d love to see how to do parallel processing in python and especially Js and Python!
@malekalhourani5930
@malekalhourani5930 Жыл бұрын
Hi Hussein, thanks for such content, it is really helpful and help me to evolve. I feel like using your old way of explaining stuff (using a PowerPoint presentation) is much easier to follow and make the topic you are trying to explain much simpler, I hope you return back to use it again, Thanks.
@LOLxUnique
@LOLxUnique Жыл бұрын
Great video from one Hussein to another
@mlsandreas
@mlsandreas Жыл бұрын
Thank you sir!! Awesome video!!
@Ysh.CS_guy
@Ysh.CS_guy Жыл бұрын
Great video 📹 👍 👏
@arcstur
@arcstur Жыл бұрын
Awesome video!
@ujjwalbansal5649
@ujjwalbansal5649 11 ай бұрын
Awsome content.
@rizwanfirdous
@rizwanfirdous Жыл бұрын
I highly appreciate your video and the hardwork behind it. One request to you could you please make a same kind of video for hibernate and tomcat. Please...
@dipjul
@dipjul Жыл бұрын
Very interesting! Can you please make a video on spring/springboot backend architecture?
@DanielTateNZ
@DanielTateNZ Жыл бұрын
Very interesting, I would be interested to hear your opinions on Laravel a popular PHP framework.
@noriking1467
@noriking1467 Жыл бұрын
Is it possible to do spring boot architecture and how project reactor works ? love the storytelling here !
@Dinesh-babu
@Dinesh-babu Жыл бұрын
Greatly informative like every other video. What do you think about netty and its use cases??
@syedazzamzafar596
@syedazzamzafar596 Жыл бұрын
I am beginner django developer. What course from you should I take first to improve my backend knowledge?
@bibekjoshi3522
@bibekjoshi3522 Жыл бұрын
Was looking at something similar and notification for this video came up
@frozeneye100
@frozeneye100 Жыл бұрын
Lol. Love way you say the bad kid dB. Really the orm. I am beginner Dev level but crap. I am forcing myself to do ORM way coz want to learn but just SQL seems so much easier than rubbing ORM into compliance. Really ORM is the hardest and doing Django way. But at times the SQL is way quicker than orm... But yeah learning is cool and your vids even better.
@TheLolEdits
@TheLolEdits Жыл бұрын
fast api next?!
@hxxzxtf
@hxxzxtf 2 ай бұрын
🎯 Key Takeaways for quick navigation: 00:00 *🐍 Django simplifies database connections by using objects instead of writing SQL queries.* 01:22 *💻 HTTP requests to Django connect to the database to execute queries and build responses.* 02:56 *📊 Django establishes database connections upon receiving requests, minimizing connection overhead.* 05:37 *🔄 Persistent connections are favored in modern architectures to minimize connection overhead.* 06:19 *🛠️ Django allows configuring persistent connections with 'connection max age=none' for efficient handling.* 07:00 *📄 Django follows a one connection per thread model, limiting connections to the number of threads.* 09:35 *⛔ Django's single connection per thread model can lead to blocking when multiple clients request database connections simultaneously.* 11:57 *🔀 Scaling Django with multiple threads can improve performance but should align with CPU core recommendations to avoid context switching overhead.* Made with HARPA AI
@vfryhn2
@vfryhn2 Жыл бұрын
Hussein could you please explain how WSGI servers work or why are they necessary for production with the python frameworks and not with other languages like node js or go? I just can't understand why they need that intermediate layer between the python server and the clients
@MaulikParmar210
@MaulikParmar210 Жыл бұрын
Your inbuilt http server isn't a process manager, what wsgi does is it taps into process management and separates application execution. Allows separating environments, application context and much more stuff that is required to invoke application efficiently and securely while keeping resource usage minimum. Implementation may vary depending on what server you are using but simple idea is, it's webservers responsibility to sandbox request response lifecycle and wsgi provides that barebones for any application and allows it to be build on top of already solved problems when you are writing web applications. This is same as (but not equal to i.e. similar to) using express in node, php-fpm, tomcat / websphere or any other application server, they are server appa helping in management and hosting of application itself while keeping them isolated from host system in one wya or other and providing specific standardised API at your application layer where you actually write business logic.
@vfryhn2
@vfryhn2 Жыл бұрын
@@MaulikParmar210 thank you!
@charank7852
@charank7852 Жыл бұрын
Hi Hussein, Thanks for sharing quality content. Can we expect a course on LInux OS fundamentals on udemy as u created for networking ? Please reply. Thanks
@alexsmart2612
@alexsmart2612 Жыл бұрын
Hi Hussein, I don't quite agree with your discussion regarding context-switching near the end of the video. Your typical web-server is IO bound and like you explained the worker threads are just mostly sitting idly waiting for a response from the database. If that is the case, why would context-switching be a significant concern? Even if you have a hundred threads, the context switch only happens when there is an actual response from the db server, right? And when it does happen, the cost of the context switch (< 10 μs) is insignificant compared to typical db response times (at least a few milliseconds).
@101kawsar
@101kawsar Жыл бұрын
Thank you 🥺❤️
@adityajoardar578
@adityajoardar578 Жыл бұрын
I think this is one of the major drawbacks of doing backend processing in django, because if we have to update databases continuously in a background thread, that means we can't use persistent connection as it will keep one of the connections in idle state always
@fartzy
@fartzy Жыл бұрын
So is it not good to use multiple threads? Even though there is context switching wouldn’t still go much faster than blocking I/o? Isn’t that why it exists? GIL or not?
@venkatavineelyalamarthy6958
@venkatavineelyalamarthy6958 Жыл бұрын
Is this video added in Udemy course as well. ? On another note, I have a question, if Python is not concurrent (can't run multiple threads at the same time due to GIL), how does Django handle concurrent API requests? Does it use multiple processes.
@littlepharaoh455
@littlepharaoh455 8 күн бұрын
Well I am here from 2024 and I heard that in python 3.13 you might don't have to use the GIL if you precompiled the library. So will this affect django or not ?? and also can we just use some rust code to improve this. by (for example) rewrite the template engine and maybe the ORM in rust ??
@MohdImran-uq5iv
@MohdImran-uq5iv Жыл бұрын
Thank you Hussain.. I am waiting for your 5th udemy course 😅
@Tekecthegoat
@Tekecthegoat Жыл бұрын
so a single thread only handles on cuncurrent user? Am I missing something?
@holthuizenoemoet591
@holthuizenoemoet591 Жыл бұрын
I would love to see a video on a good multi client per thread model
@mikopiko
@mikopiko Жыл бұрын
Could you do a similar video but for Rails? Would be hella interesting to compare.
@CamaradaArdi
@CamaradaArdi Жыл бұрын
This is my Well actually comment: you have two CPU threads per physical core. Also it's the kernel who does the context switching, not the CPU Also a thread that's waiting for network can safely be idle. As long as it's not doing work. The kernel is smart enough to give it a lower priority. Since read()ing a socket blocks (unless you're doing epoll) you can safely have 100 threads waiting for responses without a significant performance degradation. The problem will be of course if all of those threads are doing work at the same time, where you're absolutely correct.
@yashvarshney8651
@yashvarshney8651 Ай бұрын
yep it is the OS's work to schedule and context switch processes
@andresgutierrez1804
@andresgutierrez1804 Жыл бұрын
What about asgi with one thread how the Django will handle the transactions?
@SachinDolta
@SachinDolta Жыл бұрын
Much love
@riadalimammedov7209
@riadalimammedov7209 Жыл бұрын
Hello Hussein,please record fastApi Architecture analysis.
@illichoskypiotr
@illichoskypiotr Жыл бұрын
Got confused. Isn't WSGI spinning a new forked subprocess every time a new request arrives? How does that relates with this one connection per thread?
@NavidRashik
@NavidRashik Жыл бұрын
On another note is that from 4.1 it support async orm and views so yah async is supported there as well
@mehdi-vl5nn
@mehdi-vl5nn Жыл бұрын
what about async orm (django4+)?
@swapniljena8684
@swapniljena8684 Жыл бұрын
I'm going to post this here as well as on Stackoverflow. So here it goes: I am running Django app using the WSGI server (gunicorn) on Linux. I am using the local memory for caching inside the application. I am using 3 workers (workers are altogether different forked processes that shared different memory spaces) and multi-threads using g-event under each worker. As the memory space is the same for each worker I'm safe having one single cache in one worker. But the load to the workers is balanced by the OS kernel so if the workers have different memory space so, the application cache 'set' by one worker can be made a 'get' by another worker or will it turn into an issue. I can solve this using unified caching using Redis or Memcached but wanted to clear this doubt before implementing another new service.
@RajaKhan-dc4vs
@RajaKhan-dc4vs 4 ай бұрын
you actually are optimising reads. By having a cache pool and pluging it in with your application leverages the cache to fetch responses faster. at some extent mongo or mysql also uses a lru buffer to bring in the recent read pages into memory and thus saving a lot of disk reads as ordered. As per your specification you want to do it in the previous hop by putting a cache layer in between your database layer and your api layer. But you always have to trade latency with cost.
@pythoninarabic3341
@pythoninarabic3341 5 күн бұрын
Great content, but you forget about GIL in python.
@1412-kaito
@1412-kaito Жыл бұрын
So, if in Django there is like 1 client, 1 thread, 1 query, then how does it tackle several (10k like) clients?
@S--xc4rv
@S--xc4rv Жыл бұрын
Qns??? I'm downloading something in my laptop from mobile's network so very interesting thing is happening all the time while downloading. That is , whenever in every 2-3 sec there is spam of 1 sec in which the Mobile network speed goes to less than 0 kb . Why is that happening? And and there is no drop-down speed in laptop. It's normal
@amirhmahmoodi4607
@amirhmahmoodi4607 Жыл бұрын
Hussein, what does happen if you put PgBouncer into this?
@alexsmart2612
@alexsmart2612 Жыл бұрын
Nothing happens. You are still limited by the number of threads, and since django only uses one connection per thread, your thread is still going to sit idly by waiting for a response from the db server. (PgBouncer would have helped with the first problem mentioned in the video, that is django by default creating a new connection for each request if django didn't in fact have a setting to enable persistent connections, but does nothing to help with the second one.)
@vishal240993
@vishal240993 4 ай бұрын
Any solution to this or we need to use db proxy like pgboncer ?
@hnasr
@hnasr 4 ай бұрын
correct pgbouncer proxy helps this.
@yashvarshney8651
@yashvarshney8651 Ай бұрын
use starlette?
@SB-qm5wg
@SB-qm5wg Жыл бұрын
django have sqlite3 api?
@buildmore9829
@buildmore9829 Жыл бұрын
You remind me of Firas Zahabi.. related?
@Fido1hn
@Fido1hn 7 күн бұрын
had to speed it up to 1.75 to make it to the end. A lot could be said quicker
@tobidegnon4409
@tobidegnon4409 Жыл бұрын
I'm sot sure but the way you talk about threads here make me feel that you are actually talking about worker process, not threads, each process having it's own memory, I'm I missing something here?
@uEisenhower
@uEisenhower 3 ай бұрын
Explaining before you criticise, i get you're making point but I'm learning nothing except disadvantages of django, which isn't the title
@himanshukandpal5585
@himanshukandpal5585 3 ай бұрын
The video did explain the architecture to you.
@9899722801
@9899722801 Жыл бұрын
Hussein, Can we do data replication in django? I am very confused about it. Some says yes, some say it cant be
Pinterest moves to HTTP/3
25:06
Hussein Nasser
Рет қаралды 34 М.
Node.js is a serious thing now… (2023)
8:18
Code With Ryan
Рет қаралды 624 М.
1 класс vs 11 класс (неаккуратность)
01:00
БЕРТ
Рет қаралды 1,7 МЛН
КАРМАНЧИК 2 СЕЗОН 5 СЕРИЯ
27:21
Inter Production
Рет қаралды 507 М.
100❤️
00:19
Nonomen ノノメン
Рет қаралды 32 МЛН
Before you move to HTTP/3 ...
11:31
Hussein Nasser
Рет қаралды 15 М.
How CPU Efficient is your App?
19:50
Hussein Nasser
Рет қаралды 13 М.
Docker Networking Crash Course
49:19
Hussein Nasser
Рет қаралды 75 М.
Two-Minute Tech Tuesdays - Cache-Control Header
3:20
Varnish Software
Рет қаралды 4,1 М.
How Django Works | Django Workflow 2023
5:21
Rathan Kumar
Рет қаралды 2 М.
Python or JavaScript - Which One Should YOU Learn?
8:05
Tech With Tim
Рет қаралды 140 М.
HOW TO CODE DJANGO SERVICES? BUSINESS LOGIC IN DJANGO PYTHON
40:57
Andrey Ivanov | Python
Рет қаралды 4,2 М.
The effect of Random UUID on database performance
18:51
Hussein Nasser
Рет қаралды 67 М.
What happens before the Backend gets the Request
51:26
Hussein Nasser
Рет қаралды 45 М.
What Is THE BEST Web Framework In Golang? Why?
14:39
Anthony GG
Рет қаралды 35 М.
The power button can never be pressed!!
0:57
Maker Y
Рет қаралды 51 МЛН
Carregando telefone com carregador cortado
1:01
Andcarli
Рет қаралды 1,6 МЛН
A Comprehensive Guide to Using Zoyya Tools for Photo Editing
0:50
⌨️ Сколько всего у меня клавиатур? #обзор
0:41
Гранатка — про VR и девайсы
Рет қаралды 631 М.