Becoming Big

Here are your options when growing your codebase or scaling your application.

Read the Source.

Flask started in part to demonstrate how to build your own framework on top ofexisting well-used tools Werkzeug (WSGI) and Jinja (templating), and as itdeveloped, it became useful to a wide audience. As you grow your codebase,don’t just use Flask – understand it. Read the source. Flask’s code iswritten to be read; its documentation is published so you can use its internalAPIs. Flask sticks to documented APIs in upstream libraries, and documents itsinternal utilities so that you can find the hook points needed for yourproject.

Hook. Extend.

The API docs are full of available overrides, hook points, andSignals. You can provide custom classes for things like the request andresponse objects. Dig deeper on the APIs you use, and look for thecustomizations which are available out of the box in a Flask release. Look forways in which your project can be refactored into a collection of utilities andFlask extensions. Explore the many extensions in the community, and look for patterns tobuild your own extensions if you do not find the tools you need.

Subclass.

The Flask class has many methods designed for subclassing. Youcan quickly add or customize behavior by subclassing Flask (seethe linked method docs) and using that subclass wherever you instantiate anapplication class. This works well with Application Factories.See Subclassing Flask for an example.

Wrap with middleware.

The Application Dispatching chapter shows in detail how to apply middleware. Youcan introduce WSGI middleware to wrap your Flask instances and introduce fixesand changes at the layer between your Flask application and your HTTPserver. Werkzeug includes several middlewares.

Fork.

If none of the above options work, fork Flask. The majority of code of Flaskis within Werkzeug and Jinja2. These libraries do the majority of the work.Flask is just the paste that glues those together. For every project there isthe point where the underlying framework gets in the way (due to assumptionsthe original developers had). This is natural because if this would not be thecase, the framework would be a very complex system to begin with which causes asteep learning curve and a lot of user frustration.

This is not unique to Flask. Many people use patched and modifiedversions of their framework to counter shortcomings. This idea is alsoreflected in the license of Flask. You don’t have to contribute anychanges back if you decide to modify the framework.

The downside of forking is of course that Flask extensions will mostlikely break because the new framework has a different import name.Furthermore integrating upstream changes can be a complex process,depending on the number of changes. Because of that, forking should bethe very last resort.

Scale like a pro.

For many web applications the complexity of the code is less an issue thanthe scaling for the number of users or data entries expected. Flask byitself is only limited in terms of scaling by your application code, thedata store you want to use and the Python implementation and webserver youare running on.

Scaling well means for example that if you double the amount of serversyou get about twice the performance. Scaling bad means that if you add anew server the application won’t perform any better or would not evensupport a second server.

There is only one limiting factor regarding scaling in Flask which arethe context local proxies. They depend on context which in Flask isdefined as being either a thread, process or greenlet. If your serveruses some kind of concurrency that is not based on threads or greenlets,Flask will no longer be able to support these global proxies. However themajority of servers are using either threads, greenlets or separateprocesses to achieve concurrency which are all methods well supported bythe underlying Werkzeug library.

Discuss with the community.

The Flask developers keep the framework accessible to users with codebases bigand small. If you find an obstacle in your way, caused by Flask, don’t hesitateto contact the developers on the mailing list or IRC channel. The best way forthe Flask and Flask extension developers to improve the tools for largerapplications is getting feedback from users.