Close

Shared Database Pattern

Microservices typically have their own database in order to be decoupled from other services. We talked about this earlier. It’s the Database per Service pattern. But what if we use a single database that is shared by multiple services. This is the shared database pattern where each service can freely access data owned by other services using local ACID transactions.

For example, the OrderService can use the following ACID transaction ensure that a new order will not violate the customer’s credit limit.

BEGIN TRANSACTION
…
SELECT ORDER_TOTAL
 FROM ORDERS WHERE CUSTOMER_ID = ?
…
SELECT CREDIT_LIMIT
FROM CUSTOMERS WHERE CUSTOMER_ID = ?
…
INSERT INTO ORDERS …
…
COMMIT TRANSACTION

The database will guarantee that the credit limit will not be exceeded even when simultaneous transactions attempt to create orders for the same customer.

The benefits of this pattern are:

  • A developer uses familiar and straightforward ACID transactions to enforce data consistency
  • A single database is simpler to operate

The drawbacks of this pattern are:

  • Development time coupling – a developer working on, for example, the OrderService will need to coordinate schema changes with the developers of other services that access the same tables. This coupling and additional coordination will slow down development.
  • Runtime coupling – because all services access the same database they can potentially interfere with one another. For example, if long running CustomerService transaction holds a lock on the ORDER table then the OrderService will be blocked.
  • Single database might not satisfy the data storage and access requirements of all services.

In the next post we will dive deeper into runtime coupling and isolation levels.

Source: Pattern Language for Micro Services