Files
notifications-admin/app/notify_client/organisations_api_client.py

55 lines
1.7 KiB
Python
Raw Normal View History

Add Redis cache between admin and API Most of the time spent by the admin app to generate a page is spent waiting for the API. This is slow for three reasons: 1. Talking to the API means going out to the internet, then through nginx, the Flask app, SQLAlchemy, down to the database, and then serialising the result to JSON and making it into a HTTP response 2. Each call to the API is synchronous, therefore if a page needs 3 API calls to render then the second API call won’t be made until the first has finished, and the third won’t start until the second has finished 3. Every request for a service page in the admin app makes a minimum of two requests to the API (`GET /service/…` and `GET /user/…`) Hitting the database will always be the slowest part of an app like Notify. But this slowness is exacerbated by 2. and 3. Conversely every speedup made to 1. is multiplied by 2. and 3. So this pull request aims to make 1. a _lot_ faster by taking nginx, Flask, SQLAlchemy and the database out of the equation. It replaces them with Redis, which as an in-memory key/value store is a lot faster than Postgres. There is still the overhead of going across the network to talk to Redis, but the net improvement is vast. This commit only caches the `GET /service` response, but is written in such a way that we can easily expand to caching other responses down the line. The tradeoff here is that our code is more complex, and we risk introducing edge cases where a cache becomes stale. The mitigations against this are: - invalidating all caches after 24h so a stale cache doesn’t remain around indefinitely - being careful when we add new stuff to the service response --- Some indicative numbers, based on: - `GET http://localhost:6012/services/<service_id>/template/<template_id>` - with the admin app running locally - talking to Redis running locally - also talking to the API running locally, itself talking to a local Postgres instance - times measured with Chrome web inspector, average of 10 requests ╲ | No cache | Cache service | Cache service and user | Cache service, user and template -- | -- | -- | -- | -- **Request time** | 136ms | 97ms | 73ms | 37ms **Improvement** | 0% | 41% | 88% | 265% --- Estimates of how much storage this requires: - Services: 1,942 on production × 2kb = 4Mb - Users: 4,534 on production × 2kb = 9Mb - Templates: 7,079 on production × 4kb = 28Mb
2018-04-06 13:37:49 +01:00
from app.notify_client import NotifyAdminAPIClient, _attach_current_user, cache
2018-02-08 12:18:37 +00:00
class OrganisationsClient(NotifyAdminAPIClient):
def get_organisations(self):
return self.get(url='/organisations')
def get_organisation(self, org_id):
return self.get(url='/organisations/{}'.format(org_id))
def create_organisation(self, name):
data = {
"name": name
}
return self.post(url="/organisations", data=data)
2018-03-06 17:12:31 +00:00
def update_organisation_name(self, org_id, name):
2018-02-08 12:18:37 +00:00
data = {
"name": name
}
return self.post(url="/organisations/{}".format(org_id), data=data)
2018-02-12 09:26:13 +00:00
def get_service_organisation(self, service_id):
return self.get(url="/service/{}/organisation".format(service_id))
@cache.delete('service-{service_id}')
2018-02-13 14:49:03 +00:00
def update_service_organisation(self, service_id, org_id):
2018-02-12 09:26:13 +00:00
data = {
'service_id': service_id
}
return self.post(
2018-02-13 14:49:03 +00:00
url="/organisations/{}/service".format(org_id),
2018-02-12 09:26:13 +00:00
data=data
)
2018-02-13 14:49:03 +00:00
def get_organisation_services(self, org_id):
return self.get(url="/organisations/{}/services".format(org_id))
2018-02-19 16:53:29 +00:00
def remove_user_from_organisation(self, org_id, user_id):
endpoint = '/organisations/{}/users/{}'.format(
org_id=org_id,
user_id=user_id)
data = _attach_current_user({})
return self.delete(endpoint, data)
2018-03-06 17:12:31 +00:00
def is_organisation_name_unique(self, org_id, name):
return self.get(
url="/organisations/unique",
params={"org_id": org_id, "name": name}
)["result"]
organisations_client = OrganisationsClient()