Build a Data Analytics Platform With Flask, SQL, and Redis

In this article, I’ll walk through the development of a Flask-based web application that interacts with an SQL Server database to analyze population data. The application allows users to query population ranges, fetch counties by state, and retrieve states within specific population ranges. I shall also discuss how to integrate Redis for caching query results to improve performance.

Why Flask, SQL Server, and Redis?

Flask is a lightweight and flexible Python web framework that is perfect for building small to medium-sized web applications. It provides the necessary tools to create RESTful APIs, render dynamic HTML templates, and interact with databases. On the other hand, SQL Server is a robust relational database management system (RDBMS) that is widely used in enterprise applications. Combining Flask with SQL Server allows us to build a powerful application for data analysis and visualization.

To further enhance performance, we’ll integrate Redis, an in-memory data store, to cache frequently accessed query results. This reduces the load on the database and speeds up response times for repeated queries.

Application Overview

Our Flask application performs the following tasks:

  1. Query population ranges. Users can specify a year and population range to get counts of states falling within those ranges.
  2. Fetch counties by state. Users can input a state code to retrieve a list of counties.
  3. Retrieve states by population range. Users can specify a population range and year to get a list of states within that range.
  4. Note. To test, feel free to create your own schema in the database and insert sample data as needed based on the following APIs shared using SQL queries. Also, the HTML pages that are used here can be basic table design that grabs the returned data from the Flask app code and display the results.

Let’s dive into the implementation details.

Setting Up the Flask Application

1. Prerequisites

Before starting, ensure you have the following installed through your terminal root (commands compatible with MacOS):

  • Python 3.x 
  • Flask (pip install flask)
  • SQLAlchemy (pip install sqlalchemy)
  • PyODBC (pip install pyodbc)
  • Redis (pip install redis

2. Database Connection

We use SQLAlchemy to connect to the SQL Server database. Here’s how the connection can be configured:

Python

 

This connection string uses the ODBC Driver for SQL Server and includes parameters for encryption and timeout.

3. Redis Configuration

Redis is used to cache query results. Here’s how to set up the Redis connection:

Python

 

4. Implementing the Application Routes

Home Page Route

The home page route renders the main page of the application:

Python

 

Population Range Query With Redis Caching

This route handles queries for population ranges. It first checks if the result is cached in Redis. If not, it queries the database and caches the result for future use:

Python

 

Fetch Counties by State With Redis Caching

This route retrieves counties for a given state code. It also uses Redis to cache the results:

Python

 

Retrieve States by Population Range With Redis Caching

This route fetches states within a specified population range and caches the results:

Python

 

Performance Comparison: SQL Server vs. Redis

Query Type Redis Fetch Time SQL Execution Time
Population Range Query (Cached) 0.002 seconds 0.000 seconds
Population Range Query (Fresh) 0.002 seconds 1.342 seconds

Key takeaway: Redis reduces execution time from ~1.3 seconds to ~0.002 seconds, making queries 650x faster!

How Redis Improves Performance

Redis is an in-memory data store that acts as a caching layer between the application and the database. Here’s how it works in our application:

  • Cache key. A unique key is generated for each query based on its parameters.
  • Cache check. Before executing a database query, the application checks if the result is already cached in Redis.
  • Cache hit. If the result is found in Redis, it is returned immediately, avoiding a database query.
  • Cache miss. If the result is not found, the query is executed, and the result is cached in Redis for future use.
  • Cache expiry. Cached results are set to expire after a specified time (e.g., 1 hour) to ensure data freshness.

By caching frequently accessed query results, Redis significantly reduces the load on the database and improves response times for repeated queries.

Conclusion

In this article, we built a Flask application that interacts with a SQL Server database to analyze population data. We integrated Redis to cache query results, improving performance and reducing database load. By following best practices, you can extend this application to handle more complex queries and scale it for production use.

Link: The source code of this full application can be found on GitHub.

Source:
https://dzone.com/articles/build-data-analytics-platform-flask-sql-redis