Orientation with Kojo Idrissa
Published October 26, 2026
This video features Leonardo Batista at DjangoCon US 2026 in Chicago, Illinois, USA.
As your Django application grows, the database is often the first major bottleneck. But how do you scale horizontally without introducing massive complexity into your application logic? In this talk, we’ll explore how OneBlinc leveraged Django's native Database Routers to distribute load across an AWS Aurora cluster. We will go beyond the documentation to discuss real-world implementation: managing read/write splitting, handling replication lag, ensuring data consistency in financial transactions. If you want to squeeze every bit of performance out of your cloud database while keeping your Django code clean, this session is for you.
This talk was presented by Leonardo Batista at DjangoCon US 2026 in Chicago, Illinois, USA
https://2026.djangocon.us/talks/scaling-django-mastering-database-routers-with-aws-aurora/
LINKS:
Follow Leonardo Batista 👇
Website: https://www.linkedin.com/in/leonardohenriquebatista/
Automatically transcribed, so expect mistakes in names and technical terms.
Speaker 1: Good morning DjangoCon. Good morning, Chicago. Let's talk about database routers. So first a little bit about me. I'm Leo. I'm from Brazil and I'm a solutions architect and DevSecOps lead at Wamblink. And I've been working with Django for five years. I'm in love with Django since then. Really like it. And it's my third Django con to online and this is my first Django con in person and my first conference as a speaker. So great expectations So let's talk about the
Speaker 1: this issue we we had in 2024. I was supposed to submit this talk last year, but I'll talk about it at the end. So let's first talk about the breaking point of this this situation. So first some context about the the company and the the team. OneBlink is a US-based FinTech. Most of the team is Brazilian, but the company is US-based, working with US market. Our main product is Blink Advance, which is a Cash Advance app. you can find them the the app at Play Store and and Google Play you can can search later
Speaker 1: but Our our company is really small. We have around thirty people total and our tech team is Really really small. We have around ten, ten to twelve, including some some management people. We prefer using uh m managed and uh automated services as our structure is really lean. So we we usually prefer not using some built-in solutions from scratch to avoid maintenance and some some m some maintenance issues.
Speaker 1: And our uh current moment we are growing really fast with already uh reached our operational break even like three months ago so we are in a a good standpoint on the the market so really really good And what are the the tools that we mainly use? Our core stack. So we use Django running on containers at ECS. Our stack is Full AWS. We use Aurora Postgres for the database and we use Redis for cache on Elastic Cache. And what
Speaker 1: was the problem? Back to 2024, our backend was becoming Quite overloaded, mainly on peak hours. At that time we still had a uh legacy product which was payroll loans, and we usually uh uh seen some some peak uh uh uh uh traffic on um mainly fridays which were the the the paydays But with this overload, we we got our backend really, really overloaded and we needed to increase the number of nodes. So when the backend stabilized, we
Speaker 1: reached another problem, which was the database overload. And at the moment we just stopped the the the current tasks and started analyzing the the possibilities for the the database as we we hadn't had any situation like that before. So we evaluated increasing the the database size, we've evaluated some some indexes for for the core tables. discussed the some some approaches on data cleanup, data archiving, but it wasn't the
Speaker 1: the the right moment for for some some movements like archiving for example. So The database is the first bottleneck. Because horizontal scaling for for the backend is really really easy today. So we we can just spawn more nodes and It works really well. Django manages this this traffic in in a really well manner. But the database is not the same. It's not so easy to scale your database
Speaker 1: both horizontally and and and vertically. So the the database is your ceiling for your your traffic and your workload And when you start having this type of issues with database, you see some symptoms. So your app latency increases. the the connections to the database start failing, we you have uh more errors on your API requests. In this case, adding more app instances for your backend Just make the problem worse. And along that you also can see some symptoms on the database side the database metrics.
Speaker 1: So you don't have many margins on database CPU, so you can do some maintenance uh processes like uh vacuum. You see some deadlock issues, some lock contention. We'll see some some reads taking a while to to complete because the the database is overloaded and for this talk as the the AI topic is is uh uh quite common. I I'll I'll bring try to bring some some tips, not big AI slop. But some some AI tips for our experience
Speaker 1: to how you can use AI in in uh in a good way To help you understand and identify and implement a solution like that. So for example, in the symptoms case you can use AI to correlate the the the logs and the metrics from your backend, your database, and all the the usage you have. to to identify the the critical paths, optimize some queries, identify slow queries, and then identify some some indexes you you may implement And why do you do we use Aurora? First
Speaker 1: What's Aurora? Who who here have already heard about or have already worked with Aurora? Good, good. So Aurora is uh a database uh man management system from AWS. It's it runs on RDS. uh platform and it it has two versions one my s MySQL compatible and one PostgreSQL SQL compatible So for both cases, Django doesn't know the difference for for the application, it's basically the same thing. And the the two most important things that
Speaker 1: uh at my standpoint uh Aurora i is is really good i uh on R D S is The compute and storage are decoupled, so you don't need to increase your CPU just because you need more disk, and vice versa. And the the the uh the replica configuration is also really really f easy and really good to to manage So why did we choose aurora? We we choose aurora mostly because of the the reliability. So
Speaker 1: When you have many replicas, reader replicas , if the writer fails, it promotes the one of the read replicas automatically uh to a new writer and this is really really quick less than one minute this only cuts the the connection and switch the the traffic to the new node So it's it's really good and we also use this to scale the database or do maintenance windows Because you can just spawn a new new node with the new configuration and switch the the writer to this new one, then phase out the the old replica Also
Speaker 1: the the Aurora share the the storage across the replicas. So the the rep replicate replication lag is really really low. uh usually around ten to twenty milliseconds. Also the the traffic is load balanced and uh across the the reader instances in and and this is uh uh important thing uh aurora distributes connections not query load So it's important when building the the queries uh across the uh the database instances. And the the core feature that we we will use for the the solution for this talk
Speaker 1: Is the two stable endpoints. So Aurora provides two endpoints, one for for the writer, which is the primary node, and one for the reader instances So you can have a read-only connection separately f separate from the writer node and it enforces the read-only. You don't have to manage the this type of operation with roles and database permissions. But there's a trade-off Aurora is more expensive than a regular post Grizz SQL instance on RDS, usually 20 to 30 percent. So, okay, we talked about
Speaker 1: Aurora, but let's implement the router. First, database routers. Who here have already worked with or at least heard about database routers on Django? Good, good, good. So what a database router does. Uh database router is basically a class that picks the connection Depending on your own rules. You build the rules, you can have some specific for reads, for writes, and it unlocks the databases. property from from settings and you can use many databases for many purposes.
Speaker 1: Effie s said yesterday uh uh uh in in her talk about the the database routers with different flavors of databases, but in this case we will work with the same database with different connections with in a similar but different purpose And database routers need to be registered to database routers property. So they are consulted in order and Each operation may return the database alias for reader and reads and writes and uh the the method may may not be implemented it's optional
Speaker 1: and also if the method returns none it just passes the responsibility to the next router so you can uh chain many routers in your application. One important thing, the database routers are stateless. So It they doesn't they don't manage any transaction or any correlation between the the queries and the other write operations. And do not override any you dot using syntax for the the query sets And database routers basically have four methods: db for write e and db for read. quite of
Speaker 1: uh explicit in the name and it's it's really clear So D B for read specifies which a database alias will be used for reads, the B for write which database aliases will be used for writes Allow relation will specify if a relation between two models is allowed. As Effie said said yesterday, if you try to implement a foreign key between two two models in different databases, this won't work. So in this method you can specify i if the relation is allowed or not.
Speaker 1: And allow migrate. will specify if the migrate operation, the migration command is allowed to this specific alias. So we we've seen some some core definitions about the database routers. Let's talk about the idea. So we were having some some scalability issues and our database was struggling a lot with the the load. But For those who have already worked with Aurora, Aurora has a limitation by design for the business model
Speaker 1: that it is the nodes scale exponentially. So we reached the a moment that the current node size we had for the writer was too too small for our load, but the next size was too big and too expensive for our current needs. So we need to search some alternatives. And we implemented some some indexes, we did some cleaning as I I mentioned, but they wasn't enough. So we got this idea after searching a little about the the endpoints that Aurora provides. And we decided to build a database router
Speaker 1: with multiple databases connection uh connections to the both Aurora endpoints, reader and writer. And basically what we did, we built a default connection, which was the one already in place that points to the writer And we created a new one called read-only, of course, pointing to the read-only endpoint. And then we implemented the router. The router is a really simple class. You you got you people you will you'll see in the in the implementation here. And
Speaker 1: we implemented the the four methods. Here the the two I would I would call the selectors. So d B for read points to read-only connection, db for write points to default connection, which is the the writer. And then we built the valid as I call validators. So allow relation is always true because it's the same database, all objects can relate to any other object And allow migrate only allows migrations to the writer, because reader cannot allow any write operations. So this is the really big implementation,
Speaker 1: really complex, the full database router. And to finish, you just need to add the database router you just built to the database routers property. And that's it. That that's a solution. Not actually. The the main point of this presentation is not to give the the implementation of the router, but actually the the insights of the this process. So Some points that might need some some attention. First, when you do you need to enforce the writer connection?
Speaker 1: So sometimes S read operations although the the replicas exist You need to enforce the writer for many many reasons. So some case in some cases read after write operations. So for example, refresh from D B, some async tasks. triggered right before I write in the database and these tasks need need to read the the the result from the previous operation, some session operations, some redirects. For example, you have a a form that you need to submit a Let's see you you need to enroll a customer to your platform manually.
Speaker 1: You submit the the form and you redirect right after the form to the user's list and you need to ensure that the new user is listed in this user list. So sometimes the the replication lag is not enough And this new user may not appear in the list. So you need to ensure that this this new user is listed right after the redirect. Some locking operations are also required uh to happen in the writer. So any transaction and locking operation needs to be pointed to the writer. If you try to do any
Speaker 1: locking operation to the reader, it will raise an error and you will not be able to complete the operation. So this is an example of locking uh tran uh uh a transaction to uh uh uh the writer so in this case you need to override the the the target for the the operation in each Each step of the the transaction. For example, in this first get here, the the the select for update You need to enforce the the writer here because you need to make sure that the up up to date that
Speaker 1: up-to-date data is retrieved from the database in order to do the The operation correctly. Some other situations that you need to enforce the writer. Some some decisions, some critical decisions that need uh to to have correctness. So for finance area balance and limit checks they need to to happen on the writer. Some get or create operations. that have any constraint some exact counters, rate limits that need to be strict These type of
Speaker 1: operations needs to go to the writer. And of course, any structural change needs to go to the writer. So that's why the migrations need to to go there. So How do you analyze this? It depends on your workload What tolerates the the lag replication is on your side. You need to know what is acceptable and what's not. So the the real work here is to analyze the trade-offs and understand your needs. This this analysis process
Speaker 1: is the most important part for the whole implementation. And for that you may use AI to understand the needs and optimize some queries. for this process and also use the the agents and code reviewer uh tools to identify these uh writer bound operations During the review process and also during the implementation process, because we've had some issues in the past implementing some of these writer bound flows. but without checking if they uh were actually uh writer bound
Speaker 1: and when we sent to production it it failed So this this part in at this part AI is being really helpful. We've uh uh we we've been using this for some months. And Claude I'll have already got some some specific uh paths that needed to be changed before deployment to ensure correctness. And for for the the this implementation there are some cost optimization optimization tips that I I may give to you when working with Aurora and this the solution
Speaker 1: that will help you to to maintain your project. It's not for small projects, it it's uh something bigger, but they they might be really helpful. So first you need to separate the metrics for the writer and the reader. They have completely different behaviors. They they need to be handled completely separate. And the one of the most important parts uh after we implemented this solution was the auto-scaling. Aurora provides some auto-scaling rules that are not charged uh for for the main maintenance and management it just charges you the
Speaker 1: the instance uh hours and the the cloud what cloud watch metrics So you can specify some some rules to trigger a new node depending on your your workload and you can scaling predictably uh for for some specific uh t hours and and cycles So we usually scale a new reader node on payment dates so our uh as our traffic is is bigger than the usual Another tip is to use saving savings plans or reserved instance for the the at least the baseline of your workload
Speaker 1: and leave the on-demand costs only for the auto-scaling. And here is a a comparison a cut comparison for the the both uh aurora uh models the the provision and the serverless but I uh I I won't deep dive in these numbers. I uh the the only rule I can bring is that if you are Time spent uh with the the database online is less than forty percent uh average. The the serverless option is the most indicated and most recommended.
Speaker 1: And if your workload is above 60% of the time , uh up you should go to provision and instances but you can combine them you can have a writer using provisioned instances and use serverless for for the readers when you have uh a variable traffic. And for cost optimization a good Tip is to use the the AI tools to analyze your metrics, understand your workloads, and then identify some possibilities on best allocation of your resources. So maybe AI tools m must suggest
Speaker 1: a different instance family, a different instance size, some Some more and smaller instances for your readers than you have. Many many options And some uh some lessons that we've learned during this this implementation. First, as you you Could see the the router implementation is really simple. The the main work here is on consistency of your data and your application execution. And auto-scaling may save your life, but sometimes it might be tricky. So
Speaker 1: when uh an auto-scaling instance is scaling in Aka being shut down. Uh the the connections pointing to the the instance may be closed unexpectedly. So for this case it's recommended to have shorter timeout configurations for your database connections And another tip I can give for auto-scaling is use the same instance family for across all uh family and size across all your instances, both writers and readers because if you mixed
Speaker 1: uh at most uh uh the size the metrics may may confuse you uh when you you are taking a decision. Because we uh what is 30% of CPU for one instance size is completely different of what is 30% of CPU on another instance size. About the leg leg on Aurora, sometimes i i it's usually m milliseconds But sometimes this isn't enough for your use case, your application, your users, and sometimes it may be m
Speaker 1: far more than that. I've seen some cases during mu maintenance windows and and some high high volume hours reaching the the uh like some seconds for example. So it it depends a lot on your your usage. And a a tip that we learn with this process is that you need to avoid some early optimization. Sometimes you just need to build some some indexes. Uh they fix like 90% of your problem And sometimes you just need one more year or six months in a
Speaker 1: bigger instance size until you figure out your your workload and optimize some stuff. And most important, measure and understand your workload. This is really important. So where we are now on your implementation? We have a a writer and two fixed readers. with some some extra readers being triggered on peak hours on the man and our current focus is on continuous work on identifying and optimizing the the writer bound workloads and also reducing the scaling impacts of connection closing.
Speaker 1: So I presented the the solution, but we need to go further We we can have some some other possibilities with this implementation. So if you don't use AWS, disclaimer, I've not tested this, I've never heard about any of this this solutions I've just searched about so no guarantees. So GCP as my my search return has alloy db, which I've never heard about. But apparently it's the same uh idea of Aurora. GCP users may know about this more than me
Speaker 1: azure has a similar solution but it only offers one reader Instance e it doesn't provide any uh horizontal scaling as Aurora does. And you can build your own solution. Congrats if you do. So Last thing, you can adapt this solution to your needs. So you if you don't use our or if it's not your use case You can even use this database routers approach for
Speaker 1: distinct roads, different co uh connection configurations for different uh queries, uh different uh workload uh operations for long running queries and you can use this is a a good approach. We almost used For uh uh a pilot we we did, which is multi-tenancy. You can use the database routers To route your requests across multiple databases or multiple schemas depending on your tenant. So you can even do sharding by uh by tenant using database routers. So you can have a a context var
Speaker 1: that stores the the current customer and route the the request to the the respective database or schema And one last AI tip, this is a crazy one. Feel free to try it. For the the replication lag. specifically you can even connect a small LLM model to analyze your your metrics and your replication metrics and uh dynamically shift the traffic between the writers and the the readers and the writer to reduce the the replication lag. Completely crazy this idea
Speaker 1: Feel free to use it. But that's it guys. Thank you so much. This QR code points to my my website. My contacts are there Please follow me on on LinkedIn, connect to me. I 'm I'm really happy to talk with any of you. So yeah and quick notice on Andral project which make this amazing illustrations possible So congrats to them too. Thank you
Speaker 2: now we have time for questions. Anyone else?
Speaker 3: So I understand the constraints that you have on needing some of the reads to stay on your right replica. In your application, do you have a feel for like what percentage of your traffic you're able to switch to the readers?
Speaker 1: Not a uh a specific percentage, but for now our our traffic we have like two reader instances and they are like m most of the time sixty seventy percent of usage and we trigger some some other uh replicas they are the same uh the same size uh as the writer and the writer is for now is like uh the baseline is fifteen percent of cpu so the the amount of of read read operations we have is really bigger than the the write operations. And one important thing that I haven't mentioned
Speaker 1: the the talk, one of the the main gains we we you got with this is for the data team because they need to ingest our production database to the their lakehouse solution. So with this auto-scaling, they can pull our database as much as they can because the auto-scaling handles the traffic. according their their demands without uh like degrading the the back end usage. So uh uh most of these uh reader demands are from the data team not actually the the application but also the application.
Speaker 2: Well no more questions. Uh this is a gift from the organization.
Speaker 1: Thank you.
Speaker 2: Please uh
August 24 - 28, 2026
Chicago, Illinois, USA
Note: We understand that names change, people change, and bodies change. We respect each individual's journey and privacy. If you have any concerns about a video or need us to remove content, please don't hesitate to contact us. We will handle your request with care and promptly address any issues.
Published October 26, 2026
Published October 26, 2026
Published October 26, 2026
Published October 26, 2026
Published October 26, 2026
Published October 26, 2026