How design a web app that performs API-calls in a way that scales well up to tens of users?

54 Views Asked by At

I have deployed a streamlit application with azure kubernetes that regularly fetches data from a web API and displays various plotly figures. In order to speed up fetching data I implemented a simple threaded API-call method, using 4 threads to make 14 API calls. With this method I can get all the data in about 10 seconds, and have the graphs update every 11 seconds.

This is good enough if there are one or two users of the application, but it does not scale very well if I 10-20 users visit the page. It slows down the data fetching considerably, to the point where the dashbord no longer serves its purpose very well.

I assume this is because each user spins up their own instance of the streamlit application, but is forced to share the resources with all other users. When 10 users try to run a 4-threaded application, I guess it is doomed to fail?

Would it be possible to design the application such that only a single instance of it runs somewhere. And each user that visits gets served just a view of the already-running application? Or is there another way of making the application scalable?

The application skeleton is essentially this:

import requests
import streamlit as st
import plotly.graph_objects as go
from multiprocessing.pool import ThreadPool

def make_urls(*args):
    pass

def get_data(urls):
    pass

def make_figs(data):
    pass

urls = make_urls(*args)
while True:
    with ThreadPool(4) as p:
        data = p.map(get_data, urls)
    make_figs(data)
0

There are 0 best solutions below