Building Very Fast App Backends with Falcon Web Framework on PyPy

By Raushan Raj, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

In this article, we are going to quickly build and deploy a blazing fast REST API in Python web framework using Falcon. While walking through this article you will first learn the basics of Falcon framework, installing it on Alibaba Cloud Elastic Compute Service (ECS), building a Rest API for the app and finally deploying the app on the cloud.

Introduction: What Is Falcon?

Falcon is a minimalist WSGI library for building speedy web APIs and app backends. When it comes to building HTTP APIs, other frameworks weigh you down with tons of dependencies and unnecessary abstractions. Falcon cuts to the chase with a clean design that embraces HTTP and the REST architectural style.

The first thing that comes to your mind is probably “Why should I use Falcon?” Well, Falcon is as powerful as other python’s frameworks like a flask, Django, bottle, web.py, and it is fast, extensible, reliable, and encourages RESTful style. As per benchmarks, Falcon is 21 times faster on pypy than flask and Django. Organizations like LinkedIn, Leadpages, Wargaming, and Rackspace are already using it in their applications.

Image for post
Image for post

Installing Falcon on Alibaba Cloud ECS

Installing Falcon on Alibaba Elastic Compute Service (ECS) is as simple as installing it in your PC.

Provisioning Ubuntu 16.04 on Alibaba ECS

You can refer the steps to provisioning your server using this tutorial Setup up Ubuntu Server on Alibaba_Cloud.

Installing pypy3 and dependencies on Ubuntu 16.04

“If you want your code to run faster, you should probably just use PyPy.??Guido van Rossum

  1. Speed: Python programs often run faster on PyPy. (What is a JIT compiler?)
  2. Memory usage: memory-hungry Python programs (several hundreds of MBs or more) might end up taking less space than they do in CPython.
  3. Compatibility: PyPy is highly compatible with existing python code. It supports cffi and can run popular python libraries like twisted and Django.
  4. Stackless: PyPy comes by default with support for stackless mode, providing micro-threads for massive concurrency.

Once pypy3 is installed we have to install python dependencies compatible with pypy3’s pip.

Install virtualenvwrapper.

Append following line to .bashrc or .profile.

It’s better to install python dependencies in virtualenv.

Installing Mongodb on Ubuntu 16.04

MongoDB is an open-source document database that provides high performance, high availability, and automatic scaling.

Building REST API

We are going to create a book project where the user can create a book, update book and fetch book by id from MongoDB.

Directory structure for falcon app

Image for post
Image for post

book_project/book/ has following python files.

models : In models we used mongoengine ORM. Any class that inherits Document class of mongoengine describes the equivalent collection in MongoDB database. We have created a resource Book with the field name, ISBN, and author. Mongoengine will autogenerate unique id for each object of the resource.

api : Book resource have enpoint methods on_get, on_post. Usage of each method is described in code comments below.

api.py source code is a below.

GET request endpoint to get a book given id : http://<host>:<port>/<book_id>

Image for post
Image for post

POST request endpoint to create a book with data : http://<host>:<port>/<book_id> ,

Image for post
Image for post

app: This is the entry point for HTTP requests, It contains routing to resources based on HTTP path and other configurations

connect(, host=, port=), this line will automatically create a connection to the mongodb database with given credentials. The connection will be globally use throughout the scope of an app.

app = application = falcon.API(), this line will create application instances that gunicorn (web application server) will autofind and act as an wsgi app.

books = BookResource(), this line will create and instance of the resource.

app.add_route(‘/book/{book_id}’, books) and app.add_route(‘/books/’, books), will route the http path and method to the respective methods of the resource.

Deploying Web App with Gunicorn and Nginx

Gunicorn

Gunicorn ‘Green Unicorn’ is a Python WSGI HTTP Server for UNIX. It’s a pre-fork worker model. The Gunicorn server is broadly compatible with various web frameworks, simply implemented, light on server resources, and fairly speedy._

In simple terms, gunicorn understands HTTP requests and make it understandable to python applications by converting HTTP bytes to python objects.

Run gunicorn on your ubuntu machine using following commands.

Nginx

NGINX is open source software for web serving, reverse proxying, caching, load balancing, media streaming, and more. It started out as a web server designed for maximum performance and stability. In addition to its HTTP server capabilities, NGINX can also function as a proxy server for email (IMAP, POP3, and SMTP) and a reverse proxy and load balancer for HTTP, TCP, and UDP servers.

Describing in Simple terms, nginx is a software that receives HTTP request bytes and sends HTTP response bytes to the client. Nginx is responsible to forward HTTP request bytes to python application server(gunicorn).

sudo vim falcon_nginx.conf

Summary

In order to develop and quickly deploy a REST API, Falcon proves to be one of the best alternatives. We need to install pypy3, falcon, MongoDB, gunicorn, Nginx and other dependencies on Alibaba Cloud’s Elastic Compute Service (ECS) Ubuntu Server. API syntax for falcon is totally based on REST architecture and is very easy to develop with proper routing, model structure, and views. Falcon is a scalable solution and can be easily deployed on any WSGI servers like gunicorn, uwsgi, and waitress. Adding Nginx on top of gunicorn server will make microservices scalable, distributed and secure.

Reference:https://www.alibabacloud.com/blog/building-very-fast-app-backends-with-falcon-web-framework-on-pypy_594282?spm=a2c41.12436841.0.0

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store