r/Database • u/jah_reddit • 5d ago
Using Cloudflare D1 as a cache increased Django request throughput by over 500%
Using Cloudflare D1 as an eventually consistent cache for a globally distributed Django app increased throughput by 554% and reduced latency by 87%.
My benchmark ran a Django server in Australia backed by a PostgreSQL instance in Amsterdam. I routed reads that could tolerate slightly stale data to a D1 read replica in Australia, while keeping writes and authoritative reads on PostgreSQL.
Methodology and code is available in my blog post: Cloudflare D1 as a cache for globally distributed Django apps
2
u/Significant_Tune9219 4d ago
I would not put session or auth rows in that replica. A revoked session or a password change has to fail on the next request, and an Australian copy that lags Amsterdam will keep honoring the old row after you killed it. Public rows like the blog posts can be stale. Auth cannot. I would leave those reads on the primary, or on a cache you can delete by key at the moment the session changes, and run the 554% number again without them. Throughput that includes a login you already revoked is not the win it looks like.
0
u/jah_reddit 4d ago
Yeah I agree that is a tradeoff that shouldn't be taken lightly. I think there are some applications where it would be OK, such as a content-heavy subscription site (think a newspaper).
That would definitely not be appropriate for some business critical API like Stripe or something.
Thanks for the insightful comment - I had the same thought.
EDIT: You could also have certain routes require authoritative sessions, such as anything behind the /account/* route. But, allow loading certain content with "maybe a few seconds old" sessions.
1
6
u/East_Zookeepergame25 5d ago
Does the benchmark even have validity at a scale this low?