What is the FARM stack?
Stacks are sets of technologies that cover different parts of a modern web app, blended and well integrated. The right stack will enable you to satisfy certain criteria while building a web application, with considerably less effort and in less time than building it from scratch.
First, let’s see what you need to build a functional web application:
- An operating system: Usually, this is Unix/Linux-based.
- A storage layer: A SQL or NoSQL database. In this book, we’ll use MongoDB.
- A web server: Apache and NGINX are quite popular, but we will talk about Python solutions for FastAPI, such as Uvicorn or Hypercorn.
- A development environment: Node.js/JavaScript, .NET, Java, or Python.
Optionally, and often, you could also add a frontend library or framework (such as Vue.js, Angular, React, or Svelte) since the vast majority of web development companies benefit from adopting one in terms of consistency, development speed, and compliance with standards. In addition, user expectations have shifted over time. There are unsaid standards for what logins, buttons, menus, and other website elements should look like, and how they function. Using a framework will make your application more consistent with the modern web and go a long way toward user satisfaction.
The most famous stacks are as follows:
- MERN: MongoDB + Express.js + React + Node.js (MERN) is probably one of the most popular stacks today. Developers can be comfortable and never leave JavaScript, except when they need to write some style sheets. With the addition of React Native for mobile apps and something such as Electron.js for desktop apps, a product can encompass virtually every platform while relying solely on JavaScript.
- MEAN: MongoDB + Express.js + Angular.js + Node.js (MEAN) is similar to the previously mentioned MERN, with Angular.js managing the frontend in a more structured Model–View–Controller (MVC) way.
- LAMP: Linux + Apache + MySQL + PHP (LAMP) is probably the first stack acronym to gain popularity and one of the most widely used in the past 20 years. It is still quite popular today.
The first two stacks run on the Node.js platform (a server-run JavaScript V8 engine) and have a web framework in common. Although Express.js is the most popular, there are excellent alternatives in the Node.js universe, such as Koa.js, Fastify.js, or some more structured ones such as Nest.js.
Since this is a Python book, we will also go through some important Python frameworks. The top three most popular frameworks for Python developers are Django, Flask, and FastAPI. Using the Django web framework and the excellent Django REST Framework (DRF) for building REST APIs in a modern and logical way is very popular. Django itself is very mature and well known among Python developers. It also has an admin site, the possibility of customizing and serializing REST responses, the option to choose between functional and class-based views, and more.
FastAPI, on the other hand, is a relative newcomer. First released in December 2018, this alternative, lightweight framework was fast to gain advocates. Almost immediately, these advocates had created a new acronym for FastAPI within the tech stack—FARM.
Let’s understand what FARM stands for:
- FA stands for FastAPI—in technology years, a brand-new Python web framework
- R stands for React, the most popular UI library
- M denotes the data layer—MongoDB, which is the most popular NoSQL database available today
Figure 1.1 provides a high-level overview of the integrations between the constituent parts involved in the FARM stack:
Figure 1.1: FARM stack with its components
As you can see in the preceding diagram, the FARM stack is composed of three layers:
- The user performs an action using the client, which, in our case, will be based on React—this ultimately creates a bundle of HTML, Cascading Style Sheets (CSS), and JavaScript.
- This user action (a mouse click, a form submit, or some other event) then triggers an HTTP request (such as
GET
,POST
,PUT
, or another HTTP verb with a payload). - Finally, this request gets processed by the REST API service (FastAPI).
The Python part is centered around FastAPI and optional dependencies and is served by Uvicorn—a fast Python-based server. The backend is responsible for dispatching the appropriate database calls to MongoDB using various commands and queries (such as findOne
, find
, create
, update
, and more) and using the MongoDB aggregation framework. The results obtained from the database are interpreted by FastAPI through the Python driver of choice (Motor), converted from BSON into appropriate Python data structures, and finally, output from the REST API server in the form of plain JSON. If you use Motor, which is an asynchronous Python driver for MongoDB, these calls will be handled asynchronously.
Finally, returning to the diagram in Figure 1.1 and the arrow labeled JSON, the data is fed to the UI where it is handled by React and used to update the interface, render the necessary components, and synchronize the UI with React’s virtual DOM tree.
The next few sections will talk about the motivations behind the birth of the FARM stack. Why these technologies and, more importantly, why these technologies together? You will get a detailed introduction to each component and the features that make it a good fit in more detail. After a brief introduction to the benefits of the stack as a whole, the sections will provide a high-level overview of each choice and underline the benefits that it can provide to a modern web development workflow.