Maximum results with minimum cost - that’s ideal of all business and software development processes. How to get such a results in project with tight deadline and big performance expectations? Unfortunately there is no easy way: quality of software needs time and money, but .. choice of technologies, tools, solutions can be critical for product lifecycle. We can improve total time and cost of development and maintenance by choosing appropriate solutions.
The basis of effective software development is deep research and experience built on previous projects and also mistakes. In our firm we carry out systematic research of technologies dedicated web and mobile development. We create internal projects and test to detect potential problems. Today I wont to present you short review of verified RESTful solutions for Python which really speed up development time.
Technology research and benchmarks
The picture below presents a benchmark of peak JSON response per second for many technologies also Python. There are 3 Python approaches with the following results:
- Django-stripped (Django without context processors and middlewares) – 13 269 per sec
- Flask – 11 506 per sec
- Django – 7122 per sec
We analyzed this benchmark and we can offer few simple practice for improve this results thanks to:
- database optimalization for PostgreSQL – using pgBouncer and pgPool for connection pool problem,
- rendering template with Jinja,
- using ujson (ultra json) instead standard serialization library.
The picture below presents comparison performance of using PostgreSQL + Django and pgBouncer+pgPool+jinja templates created by Askthepony.com (look there for more). In sum using this simple practice we can threefold increase performance.
Let’s get it started!
Assumptions of simple, example application: web platform and mobile application on iPhone/iPad. Web application written in Django has information about members of Python Group newsletter, but from mobile application level it’s possible to add new and view existing members. API is a “overloaded bottleneck”, so we have to implement efficient RESTful webservice (this example is of course a big grain of salt ;) )
- Firstly, simple model of Member based on SQLAlchemy:
- Method to handle the APIs requests:
Class MethodView is recognizing each REST methods based on generic dispatch request method.
It was simple and quick, but we can tune this. </li>
- 17 lines source code of full application, 4 lines for configuration, 2 lines implementing RESTful API and the same result like solution above: </ol># More features Flask RESTless library
- specifiy data to REST methods, including/excluding fields of models:
- pre/post proccessors:
example request URL:
Scripts above was amazing huh? But the main concern is how it works in real project with more requirements and development is dictated by real needs no pretty theory..</br> The following features are useful in real cases:
In conclusion using micro framework Flask with RESTless library is good combination. Our company have experience with Flask based REST and it’s very convenient solutions to existing applications.
Assuming that app architecture where Flask responsible for API, Django app for web user interface, iOS and Android app for mobile interactions, we can say more about Flask advantages:
- very good performance,
- simple and fast prototyping and extensions of API,
- separation API server from Django web application,
- simple to learn for uninitiated in Flask development,
- working with gunicorn (it possible to have Django and Flask on one server),
- as default is SQLAlchemy ORM.
- distributed documentation of Flask (many, many plugins),
- more efford is needed to logic structure of application,
- lack of automatically generated admin panel which is very useful for filtering results and generating raports,
- no possibilities to use helpers methods and libraries written to handle some buissness logic from Django.
What about Django REST solutions?
It’s many available libraries for REST like: django-rest-framework, django-tastypie, django-piston (not maintained already) and more. I focuse on the first solution, because it’s based on good patterns, very good maintained, with quick support, documentation on good level and reasonable approach.
On the beginning few words of explanation are needed. Name of django-rest-framework is dictated by the ability to quickly and easy find for developers. But.. it’s not correct terminology, because this library is based on wider concept than REST – Hypermedia driven APIs. I skip definition in this blog entry, but more information you can find here and also in books.
Django-rest-framework provide tool to design and develop browsable APIs based on HTML.
Django-rest-framework is very advanced tools and below you can find only the most basic and simple realization of the defined problem in 4 quick steps:
- Model definition:
- Serializer – class describing rules for model and how should it be represented in API:
- View on Class Based Views architecture:
This solutions is also very quick and simple.
Advantages of this solution are:
- more advanced and extensible tool,
- implementation according to needs, more ways to implement the same feature,
- possibility of use Django classes and methods or own helpers from web user interface application,
- fast for automatic testing (simple implementation of tests based on Django TestCase).
- weak support of nested objects, in consequence problems with iOS Restkit integration,
- lower performance than Flask (Django is big machinery, but on Dabapps blog you can find useful advices on profiling django-rest-framework and benchmarks)
In summary, development of API can be really fast and simple what is very beneficial for project. in retrospect and developed projects I personally prefer django-rest-framework for smooth cooperation with Django. API project has clean and logic file structure, because of framework patterns. In one place I have full Django power and all my helpers. I can test my API in easy way. But Flask is also very good choice, even better for overhead systems.
Presentation with condensed informations: