Building a scalable Flask API can be a daunting task, especially with low latency. However, with the right tools and architecture, it’s possible to create a fast and reliable API that can handle millions of requests per second.
In this tutorial, we’ll build a Flask API with Redis caching in Docker, a powerful combination that allows us to achieve high performance and scalability.
Redis is an open-source in-memory data structure store that can be used as a database, cache, and message broker.
Docker is a containerization platform that allows us to package our application and dependencies into a lightweight container that can be easily deployed and scaled.
Before we get started, make sure you have the following installed on your system:
- Python 3.7 or higher
Setting up Redis
Redis can be installed on your system or run as a Docker container. In this tutorial, we’ll use the latter approach, as it allows us to easily set up a Redis instance that can be shared across multiple containers.
First, create a new directory for your project and navigate to it in your terminal. Then, create a
docker-compose.yml file with the following contents:
This file defines a single service called
redis that uses the official Redis Docker image and exposes port
6379, which is the default Redis port.
To start the Redis container, run the following command in your terminal:
docker-compose up -d redis
This command will download the Redis image if it’s not already present on your system and start a new Redis container in the background.
Setting up Flask and Flask-Caching
Now that Redis is up and running, let’s create a new Flask project and add caching support using Flask-Caching.
Create a new directory for your project and navigate to it in your terminal. Then, create a new virtual environment and activate it:
python3 -m venv env
Create a new file called
app.py with the following contents:
This code sets up a new Flask app and configures Flask-Caching to use Redis as the cache backend. The
get_data() function returns a simple JSON response and is decorated with
@cache.cached(), which tells Flask-Caching to cache the response for 120 seconds (you can increase or decrease the cache timeout as per your requirement).
Building a Docker container
Now that we have created our Flask API with Redis caching, let’s build a Docker container for our application. This will make it easier to deploy our application to different environments.
First, we need to create a Dockerfile. A Dockerfile is a script that contains all the commands needed to build a Docker image. Here is an example of a Dockerfile for our Flask API with Redis caching:
Let’s go over what each line in this Dockerfile does:
FROM python:3.9-slim-buster: This line sets the base image for our Docker image. We are using the official Python 3.9 slim image, which is a smaller version of the full Python image.
WORKDIR /app: This line sets the working directory to
/app, which is where we will store our application code in the container.
COPY requirements.txt .: This line copies the
requirements.txtfile from our local machine to the container's
RUN pip install -r requirements.txt: This line installs the Python dependencies listed in the
COPY . .: This line copies the rest of our application code into the container's
EXPOSE 5000: This line tells Docker to expose port 5000, which is the port our Flask app will run on.
CMD [ "python", "app.py" ]: This line tells Docker to run the
app.pyfile using the Python interpreter when the container starts.
Once we have created our Dockerfile, we can build our Docker image using the
docker build command. In the same directory as our Dockerfile, run the following command:
docker build -t flask-redis-caching .
This will build a Docker image with the tag
Running the Docker container
Now that we have built our Docker image, we can run our Flask API with Redis caching in a Docker container. To do this, run the following command:
docker run -p 5000:5000 flask-redis-caching
This will start a Docker container with our Flask app running on port 5000. We are using the
-p flag to map port 5000 in the container to port 5000 on our local machine.
In this article, we have learned how to create a Flask API with Redis caching and run it in a Docker container. Redis caching can greatly improve the performance of the Flask app by reducing the number of database queries needed to generate a response. Docker makes it easy to deploy the application to various environments by packaging the app and its dependencies into a single container.
You can find an example implementation for this article at https://github.com/Shikha-code36/Flask-RedisCaching-in-Docker .