bimals.net
Fantasy Premier League Data Dashboard with FastAPI, Postgres, Redis and Nextjs
Overview
I don’t like the way Premier League presents the players data for Fantasy PL. So for personal use, I’ve created a dashboard heavily inspired by NBA stats. Here I have tried to go through the features I have implemented so far using tech stacks such as Fast API, Redis and Postgres.
While the main goal was to create a dashboard to play FPL, I’ve taken the performance into considerations and tried to optimize it more than I usually would in a personal project.
Key Features and Workflow
Each tech stack use will have a separate post explaining the implementation in details.
Fetching Data from the FPL APIs
I have used Python scripts to grab data from two main Fantasy Premier League APIs: FPL Bootstrap API and Player Detail API.
Validating the Data with Pydantic
Once I have the data, I then used Pydantic to validate and serialize it as per my need because the APIs return lots of keys that I didn’t want to use at all. This step also saves a lot of headaches by catching any issues early and making sure the data is properly structured.
Caching with Redis
After the data was validated, I compared it with what was already stored in Redis. The goal was to reduce unnecessary database actions and keep everything fast and efficient. If the new data matched what was in Redis, I skipped updating the database altogether.
If the data was new or updated (or if the cache was missing), I updated the Redis cache and saved the new data to the database. This way, I’m not writing the same data over and over, which helps with performance.
Avoiding Redundant Database Writes in PostgreSQL
After knowing whether or not I have new data, interaction with PostgreSQL comes in. To avoid unnecessary writes, I looped through each player’s data to check if it already existed in the database. If the player data was already there and unchanged, I skipped the insert. If it didn’t exist or had changed, I proceeded with upserting the data.
Connecting the Backend to the Frontend with FastAPI
To tie it all together, I used FastAPI to create the API endpoints for my data with the help of SQLAlchemy so that my Next.js frontend can call.
Next.js Frontend
On the frontend, I am using Next.js App Router to render the data pulled in from the FastAPI backend.
Deployment
I have deployed the project locally using Kubernetes.
Github backend: https://github.com/bimalpaudels/fpl_backend
Github frontend: https://github.com/bimalpaudels/fpl-frontend