Files
system-design-101/data/guides/database-middleware.md
Kamran Ahmed ee4b7305a2 Adds ByteByteGo guides and links (#106)
This PR adds all the guides from [Visual
Guides](https://bytebytego.com/guides/) section on bytebytego to the
repository with proper links.

- [x] Markdown files for guides and categories are placed inside
`data/guides` and `data/categories`
- [x] Guide links in readme are auto-generated using
`scripts/readme.ts`. Everytime you run the script `npm run
update-readme`, it reads the categories and guides from the above
mentioned folders, generate production links for guides and categories
and populate the table of content in the readme. This ensures that any
future guides and categories will automatically get added to the readme.
- [x] Sorting inside the readme matches the actual category and guides
sorting on production
2025-03-31 22:16:44 -07:00

2.1 KiB
Raw Permalink Blame History

title, description, image, createdAt, draft, categories, tags
title description image createdAt draft categories tags
Database Middleware Explore database middleware for transparent routing and simplified code. https://assets.bytebytego.com/diagrams/0276-middleware-png.png 2024-02-25 false
software-architecture
Database Routing
Proxy

There are two common ways to implement the read replica pattern:

  1. Embed the routing logic in the application code (explained in the last post).
  2. Use database middleware.

We focus on option 2 here. The middleware provides transparent routing between the application and database servers. We can customize the routing logic based on difficult rules such as user, schema, statement, etc.

The diagram above illustrates the setup:

  1. When Alice places an order on amazon, the request is sent to Order Service.
  2. Order Service does not directly interact with the database. Instead, it sends database queries to the database middleware.
  3. The database middleware routes writes to the primary database. Data is replicated to two replicas.
  4. Alice views the order details (read). The request is sent through the middleware.
  5. Alice views the recent order history (read). The request is sent through the middleware.

The database middleware acts as a proxy between the application and databases. It uses standard MySQL network protocol for communication.

Pros

  • Simplified application code. The application doesnt need to be aware of the database topology and manage access to the database directly.
  • Better compatibility. The middleware uses the MySQL network protocol. Any MySQL compatible client can connect to the middleware easily. This makes database migration easier.

Cons

  • Increased system complexity. A database middleware is a complex system. Since all database queries go through the middleware, it usually requires a high availability setup to avoid a single point of failure.
  • Additional middleware layer means additional network latency. Therefore, this layer requires excellent performance.