E-Commerce with Django at Scale: Effective Performance Lessons Learned

Video in TIB AV-Portal: E-Commerce with Django at Scale: Effective Performance Lessons Learned

Formal Metadata

E-Commerce with Django at Scale: Effective Performance Lessons Learned
Title of Series
Part Number
Number of Parts
CC Attribution 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal purpose as long as the work is attributed to the author in the manner specified by the author or licensor.
Release Date

Content Metadata

Subject Area
I'll take you through the most effective performance lessons we've learned and show you how you can implement them (with example code). TWO-PASS CACHING WITH CLASS-BASED VIEWS By far, this is one of the most effective performance optimizations we have done in terms of HTTP response time. Using class-based views, we are able to do two-pass caching. On the first pass of the view, we render everything that's not specific to the user. No AJAX calls needed to get user specific content on the page. I'll show you how. DATA CACHING STRATEGY I'll review how we use multiple levels of data caching to greatly improve the amount of time it takes to rebuild the entire cache. DB READ REPLICAS FOR PERFORMANCE / CUSTOM BACKEND FOR FAILOVER Read replica databases are great for performance. You've set up a few read replicas and implemented a fancy new database router which sends read queries to the read replicas (round robin) for any data that doesn't need to be up-to-the-millisecond fresh (e.g. blog posts, product descriptions). You're sitting back and relishing in the improved performance when one of your database read replicas goes offline. Now what? I'll show you how we implemented a custom database backend to handle this gracefully. MIGRATIONS RULES This is less of a performance optimization and more of a set of rules we try to stick to. I'll review some snafus we've had and how we avoided future production issues while keeping the site at 99% uptime.
Adventure game Mathematics Scaling (geometry) Computer animation Length
Human migration Strategy game Computer configuration Multiplication sign Operator (mathematics) Website Database Bit Reading (process) Partition (number theory) Front and back ends
Point (geometry) Meeting/Interview Gender Cuboid Database Router (computing) Front and back ends
Implementation Mechanism design Computer animation Query language Multiplication sign Website Fault-tolerant system Reading (process)
Web page Implementation Sequel Multiplication sign Gender Database Machine code Vector potential Connected space Front and back ends Meeting/Interview Website Error message
Axiom of choice Implementation Sequel Patch (Unix) Multiplication sign Hidden Markov model Set (mathematics) Device driver Mass Event horizon Field (computer science) Front and back ends 2 (number) Product (business) Computer configuration Software testing Error message Exception handling Social class Addition Multiplication Wrapper (data mining) Gender Database Machine code Instance (computer science) Connected space Category of being Query language Order (biology) Finite-state machine Object (grammar) Reading (process) Active contour model
Area Aliasing Trail Computer configuration Control flow Database Parameter (computer programming) Reading (process) Form (programming) Connected space Usability Exception handling
Point (geometry) Web page Scheduling (computing) Building Multiplication Multiplication sign Expression Sheaf (mathematics) Database Machine code Mereology Product (business) Category of being Cache (computing) Message passing Mechanism design Process (computing) Computer animation Strategy game Meeting/Interview Personal digital assistant Website Object (grammar) Social class
Revision control Web page Cache (computing) Functional (mathematics) Uniform resource locator Server (computing) State of matter Cuboid
Web page Server (computing) Meeting/Interview Hacker (term) Website Vector potential
Hacker (term) Computer configuration Personal digital assistant Gender Plastikkarte Template (C++)
Web page Email Structural load Sheaf (mathematics) Data storage device Content (media) Bit Template (C++) Cache (computing) Category of being Arithmetic mean Latent heat Personal digital assistant Object (grammar)
Web page Dataflow Pairwise comparison Context awareness Forcing (mathematics) Counting Variable (mathematics) Template (C++) Product (business) Number Cache (computing) Message passing Word Latent heat Process (computing) Blog Artistic rendering Volumenvisualisierung Resultant Social class Form (programming)
Web page Implementation Context awareness Functional (mathematics) Building Randomization Standard deviation Token ring Multiplication sign Moment (mathematics) Variable (mathematics) System call Template (C++) Message passing Latent heat String (computer science) Volumenvisualisierung Resultant Freezing
Web page Implementation Message passing Computer animation Token ring Template (C++) Form (programming) Product (business)
Point (geometry) Slide rule Token ring Gender Water vapor Line (geometry) Open set Variable (mathematics) Template (C++) Message passing Logic String (computer science) Software testing Quicksort Error message Address space Social class
Web page Presentation of a group Token ring Chemical equation Multiplication sign Projective plane Rollback (data management) Sheaf (mathematics) Mereology Power (physics) Product (business) Template (C++) Human migration Cache (computing) Type theory Programmer (hardware) Integrated development environment Personal digital assistant Volumenvisualisierung Website Integer Table (information) Poisson process
I have more of the planned and the and the and and the and the and the ch that you have a length of so my name is an adventure and the stock is e-commerce with changes scale
effective performance was learned so I'm going to take you through some of things we learned while building are e-commerce site at undercover tourist guffaw topics for you database read replicas and fell over the other caching strategy to pass rendering with class-based views and migration rules galactic over there and just a little bit of time so quickly I 1st that with database 3 replicas and failover I'm going to show you how we can implement a simple custom database backend allows for read replica failover so
widely read replicas of my and important I had that 1 of the easiest ways a skill you database read operations of course there are other options shouting partitioning
and if you have tons of money you can just pay local and everything all magically work so we can get
out of the box is the look before we implement a custom database backend gender has a functional example in the documentation and you can use it as a great starting point for getting read replicas working you can get up and running quickly by URI ruckus the databases setting and by using jingles built in database routers
so this is a great example of an and this again from the documentation it's it's extremely simple straightforward uh you may have seen this before if you haven't please go changing as documentation is great but there is a problem with this implementation it could be kind of a big 1 this is a support so just because you have read
replicas doesn't mean you have 100 per cent uptime or you have on a person up time for your sites is 1 and then goes down some of your queries might fail the read replicas a use for scaling but they can also be a great fault tolerance mechanisms so
if 1 of you replicas were to go down some the people bilinear site and couldn't potential customers are now going to see an error page at best were loading bar until connection time the worst and either way you're going to end up losing potential customers so let's go through
the implementation and I should note that this regard database backend is only 4 genders or M and of 3rd party or ends with a school coming and I'm also going to be rest referencing my sequel here the should also work proposed PostgreSQL but before we look at the code
HMM going to warn you that this is using monkey patching in order to accomplish filling over from read replica connection errors why well we don't really need to you but it's the most reasonable option the other choice would be to implement a full database back by the way the best way I found it possible to recover from a database connection failure is inside the database backend unfortunately there's no way in the database to handle a connection air also genuine for chili sauce references to the database on private properties of the or em instance objects and these references are used in multiple places 1 ever reconnect the database so the best place to recover from a database connection failure is inside the dairy expected this is an overview of the code we're
going to use the implement back and and what's what the so in order for data recognizer back and we need to implement the mn database wrapper class says repeated backing-off genders implementation we can this important news like it's the next we need a reference to the get new connection method so color whenever a customer methods called after reference to the original method we can write with a custom method that handles the Republic of failure that's the multi because the method is very simple straightforward we call the original method entrap any connection errors the moment you'll see which is re raising the exception and the high cost American will be exception handling before we get into that when you add a few custom settings to a databases so here's an overview notice we're using our my sequel back engine so 1st cousins I need is for the dual purpose 1st this is where we can identify read replicas that have the same mass a database we're trying to find another 1 in the event of a failure In addition there is also a master database that will use the father read replicas of the snake saying it is not for a custom field but it's still very important in order to fill within a reasonable amount of time for a connection timeout we needed to find a connection time of of the database for gender is my C Quebec and this gets passed directly to the database driver and note this is a connection timeout not at query time abuse 5 seconds here which is actually what we use in production for read replicas but please test and see if you need something different so going back to the implementation let's drill down into our method we need to do something better industry raising so let's implement the exception handling 1st thing we wanna do is make sure you're working with a read replica that has over support to check for a Phil over Mr. setting
bring that backs against the as I said is only on the read replicas of we want sport feel for
next will be having this exception from Romanian this exception from another fell over so we grab the database areas of and we also optional keyword argument and the reason this is the database selling dictionary alias key this may not be also the 1st failure to keep track of previous failures with the optional keyword arguments and on the current of and is the heart of a filter this is very simple but let's step through quickly will go through databases we're looking for a database has not previously failed and we wanna read replica for the same master database if we find a usable re replica so the new DB variable and if we don't find usable read replica will fall back to the master database occasion up from the ask you this means I didn't had a break on the form and we're also we've got in the database use so this over other the host in the connection parameters and we can get in connection I should note that you may also need to replace support username etc. password because you do different data have different values for the so
now universal and this is your site well everybody can be a potential customer and so that's how we implemented automatically replica Philibert with the custom database back in case you're handed out very fast and you scribble all that down the code will be online and you have so did caching
chosen why while and the database is expensive cash in expressive and not having the database multiple times for the same data is even better the so for the caching strategy 1st we need a database the a database populating the shared data objects in a data objects and again objects also populated by shared data objects reviews by populated by assured that objects and the objects and finally showed objects the objects and views are stored in the cache the the we also need a way to refresh cash so we can use a scheduling mechanism trading such as a cron job to automatically refresh the frequently used it objects so that's a dedication strategy a dynamic code for this section but this is an important stepping stone for the next section the main take here is you wanna make sure you're building your data objects or view models using shared data objects or you should be models whenever possible for example if you sell products that appear on different pages of your website you might have a page that lists parts only for a certain category or you might have a page was all products and so with this caching strategies can 1 make sure that your caching each product data objects for the model which will be a assured the object of you that point and then you can use that cash object on both of those views so to pass running of class
use 1st as we did with the other topic that significant OK we got out we can get out of the box of Genghis caching of users Django is simple and the examples in the documentation he said the cache server with the caches setting and then you have to the function in the URL Conf with cash page or use decorative version there some this is
what it would look like and this is directly from the documentation so I'm not going to go through it but you decide you want implement this because you want your state to be very fast so
you 1st potential customer Mr. hacker can comes your site logged on and everything works basically
perfectly and it's really fast on page reload I can't about society because you do the server they're still happy everything's coming from cash so
the next potential customer visit your site and the while still welcome Newman
hacker can of this person is devastated by a website and thank you so john risks so good luck getting a potential customer and the credit card debt but the good news is gender as a solution
for this actually to put the 1st 1 is very hatters yet another simple decorator and each user's views will be cash separately the 2nd option is template fragment caching make use of cash template tag and you specify parameter such as a user ID and everything was were perfectly so this is what the case could look
like but again this is from János documentation so I'm not going to review its so that's a very good
what's the problem that we did caching for user and making a specified sections of a templates the categories the problem is if the many or most of your views of specific content the leader lots of cache storage which means the solution is not infinitely scalable I do realize a catching a signal is a very small footprint there's another problem as well with the solution you know your views will need to be rendered and cash for each user so if you have view that a random a fast means each user has to wait 10 seconds for that year and they're probably saying well I had just shows we should use a multi-layered catching solution so why using Jena's built-in support will be good enough if you did should that object so large or if you have many of them to look for pairs they can still take a while for all these objects the load and the template around so if you want to process so they'll be a lot of template a and a new way of pages to be rendered very quickly in all cases so the next step after we've decided that you know we're we're going is very headers or the cash template tag I is that we can actually do a little bit better i'm because in for using all those living a lot of objects the cash they could still take a while for review surrender so we can do is use to pass rendering uh is is what i've called that's so how the next section is actually of fully right so the to better the idea
is very simple let's take a look at the process flow 1st we will let the data in our view will render the template on the 1st pass and this is going to be all the data is not specifically tied to a user this past lots of temple taste surrender and could take a while for example something that would be rendered in this view would be blog comments products so next will catch the 1st result and finally render the 2nd pass this past will render all the specific data and it will run a much more quickly in comparison to the 1st pass for example this would include the number of items in your shopping cart or the person's name so this is a layered approach to content rendering and caching so then 1 the next person this is the page the only thing you need to do is load the 1st render from cash and render the user-specific contact this will be extremely fast because there only be 1 cash and a few temple taste surrender so let's take a look at how we can implement this 1st we got very
simple class view which inherits from class called cash view that will define a moment and we've also got a couple extra methods here that will be used by the cache pair class so it's still get those forces get 1st first-pass context of ours this is where it will implement all the context variables for example as you normally think of them but you know non user-specific data again blog comments products anything not specific to the person viewing the page next we get get second-pass context words was be anything specific to the person viewing the page practice shopping cart count or user-specific message there's also 1 very other important item the specific to everyone is going to submit a form the CIA of
token you may be thinking we don't need to worry about that because Django handles of for us then you're right but we do need to be specific to each person viewing the page so I show you how we handle that in a moment + build up the cash you implementation we
the imports from standard Django functionality and you can see this at the top and going to remove those so we can focus on the cash to you so main function for the home user called to the random method under cash you so let's break that up the and the 1st thing in the random method is a call to the render first-pass some going to drop at the home view and analysts plot the render 1st pass the but it was breakdown was going on in there so 1st we trial of the rendered template from cash if we can let it will build a building template we need the 1st context variables and these are the non user-specific context variables for the view then we render the template to string using János render 2 string method you may notice here were not using a request context and this means you won't be able to render sister of tokens and I mention this a couple times now not come back to the moment so Sarah the temple to the cash and we return a first-pass template so they can run our method we need to do the 2nd pass like we did with the 1st pass render only the context variables and these will be the user specific context variables next we turn the result of the 1st rendered into a template this'll be by of the magic of the two-pass templates will go into more detail on this in a moment then or under the singer template notice freezing request context here this will last disruptive concern finally we return the 2nd question the so it's going back to the sister of
tokens nodes using implementation going to expose the magic behind its listing a quick look at a simple Jenga template the
so here's a simple template that has a welcome message for the user is viewing the page the simple form where they can put a quantity to purchase our products unfortunately if we under this through a cash you say well a lot of warnings like
this but and see sort of tokens will be rendered the reason we get a serious error is very straightforward so as a quick look back the cash The class remember what I mentioned
that the reader to string method wouldn't render our sister of tokens this is this is what so let's go back to the HTML template and address the issue so remember we're going to render this template twice so the 1st pass water under these lines which have non user-specific data and for the 2nd we wanna under these lines which are the user-specific data and of course the sister after so how can we accomplish that the to watch closely In ammunition works in and speed of 50 slides you'll see that this will be our new first-pass template so what's going on here the solution is that will render the first-pass template to become the 2nd pass template but using genders open blocked close block open variable includes variable template tags unless the magic then we render our 1st test template and will end up with this the so then we can render their as the 2nd template at this point the only trouble logic which remains in a template is a 2nd pass template variables and the CS RF token so this
way we rendered summed second-pass template and we get 2 very important things 1st we can render the sister of token and more importantly will be a the render the template extremely quickly because we only need 1 caches and agenda template only has a few template takes the parts so as superstring attempt with class-based views migration rules this is a quick 1 also raise your hand if you've never had to take down your production site during migrations for deeper OK I don't have anything for you section so hopefully owing to its power so these are some simple rules we put in place those so the 1 we deploy production in a multiserver environment we can do it while keeping a mind without spewing bunch there's this may seem like common sense 1 so you started but I think so 1st don't go from a more precise the less precise value type for example don't go from a double to float or to an integer instead make a new column with the new value type and copy the values from the existing home 2nd don't rename columns the tables instead make a new column a table and copy the data from the existing cable into the new 1 and last but not least don't do we counted tables there's no instead on this 1 I just don't delete them but you can delete them eventually obviously knowledge of various to fill up and run out of space but wait a few versions out and then delete the encasing the rollback or case of the poisson so that's it thank you appreciate time so that people here have been helpful again the the Colombian get home I I can give you income find me but the user name is any Pinchot this my 1st initial my last name Engelbart's RT up there feel free to check it out it's working very simple jingle projects that shows all the code from from this presentation and 1 quick notify me but we are looking for a Python programmer so if you are looking for a company that self-sustained and fun work with check undercover tourists . com and look at our careers page this so here I thank the half of it even if I have a balance