SlideShare a Scribd company logo
Practices for Database Design in Microservices
Architecture
Database in Microservices
using PostgreSQL
Sandip Basnet
About me
● Backend Developer.
● Using PostgreSQL for the last 4 years.
● Designing and building scalable and robust backend solutions.
● Believe in identifying issues and finding creative solutions that deliver tangible
results.
● Contact:
○ Email: mondyfy@gmail.com
○ LinkedIn: https://www.linkedin.com/in/sandipbasnet
Introduction
- Microservices architecture is an approach to building large, complex systems as a
collection of small, independent services.
- Each microservice has its own responsibility and can be developed, deployed, and
scaled independently.
- Databases are a critical component of microservices architecture, as they store and
manage data that is used by multiple services.
Problems
● Designing databases for microservices architecture is challenging because there
are many different ways to organize data and manage transactions.
● Some common challenges include:
○ Preserving ACID
○ Managing data consistency across multiple services.
■ Strong
■ Eventual
○ Ensuring fault tolerance.
○ Maintaining scalability as the system grows.
Approaches
● Architecture:
○ Multi-schema
● Maintain Isolation:
○ CQRS
● Manage transactions:
○ SAGA
Shared database/schema
● Services share the same database instance.
● Reduces the complexity of managing multiple databases, but it can also lead to
coupling between services.
● To avoid coupling, it's important to use clear boundaries and APIs between
services that share the same database.
Database per service
● Each microservice having its own dedicated database.
● Greater scalability, fault tolerance, and isolation between services.
● However, it can lead to increased complexity in managing and coordinating data
across multiple services.
Schema per service
● Each microservice has its own database schema.
● Different from the shared schema -> multiple microservices share the same
database schema.
● Provides improved scalability, easy maintenance, and better data isolation
between services.
CQRS (Command Query Responsibility Segregation)
● Separates read and write
operations into different
components.
● Improves performance and
scalability by allowing each
component to scale
independently.
● Microservices can implement
CQRS by using different
databases or database schemas
for reads and writes.
Saga Pattern
● Way to manage data consistency across microservices in distributed transaction
scenarios
● There are two types of patterns:
○ Choreography - each microservice publishes events to trigger the next step in the transaction.
○ Orchestration - a central component coordinates the transaction between microservices.
Saga - Choreographer
● Each microservice knows its
own part in the transaction and
publishes events to trigger the
next step.
● Provides greater autonomy and
scalability for each microservice.
● However, it can be more
difficult to coordinate and
manage transactions across
multiple services.
Saga - Orchestrator
● A central component (the
orchestrator) coordinates the
transaction between microservices.
● Provides greater control and
consistency over the transaction.
● However, it can lead to increased
complexity and coupling between
services.
Implementation - success
Implementation - failure
Designing a microservices database schema
● When designing a microservices database schema with PostgreSQL, it's important
to consider the data access patterns and the relationships between different
services.
● Some key design principles include normalizing the data to minimize duplication,
using foreign keys to enforce relationships, and using indexing to optimize
performance.
● It's also important to consider the impact of schema changes on other
microservices that may depend on the data.
Challenges and considerations
● Managing data consistency across multiple databases,
● Implementing proper security measures, and
● Ensuring proper monitoring and management of the databases.
● Cost
● Scalability
Practices for using PostgreSQL in microservices
● Use a separate database/schema for each microservice to ensure isolation and
scalability.
● Implement CQRS to separate read and write operations and improve
performance.
● Use the SAGA pattern to manage data consistency across multiple microservices.
● Use JSON data types to handle complex data structures.
● Use PostgreSQL's support for ACID transactions to ensure data consistency and
reliability.
Why PostgreSQL for microservices
● ACID compliance.
● Robust feature sets
○ Support for JSON data types.
○ Ability to handle complex queries.
○ Object-oriented
● SQL Standard
● Open source development & Community
Summary
● Designing a database for microservices architecture using PostgreSQL can be a
complex task, but by following best practices and design principles, it can lead to
a robust and scalable system.
● By using separate databases for each microservice, implementing CQRS and
SAGA patterns, and using PostgreSQL's advanced features, developers can build a
reliable and scalable microservices.
Thank you for your time and attention!
I hope this session has been informative and helpful.
@mondyfy
References
● PostgreSQL as a Microservice: https://tapoueh.org/blog/2021/06/postgresql-as-a-microservice/
● Monolith to Microservices: Refactoring Relational Database
https://programmaticponderings.com/2022/04/14/monolith-to-microservices-refactoring-relational-
databases%EF%BF%BC/
● PostgreSQL in a microservice World: https://momjian.us/main/writings/pgsql/microservices.pdf
● Microservice Architecture: https://github.com/codefarm0/Microservices-Architecture
● Benefits: https://www.prisma.io/dataguide/postgresql/benefits-of-postgresql#object-oriented-database-
features

More Related Content

PDF
Distributed SQL Databases Deconstructed
PPTX
How we solved Real-time User Segmentation using HBase
PDF
OSMC 2021 | Introduction into OpenSearch
PDF
Event-Driven Microservices With NATS Streaming
PDF
Best Practices for ETL with Apache NiFi on Kubernetes - Albert Lewandowski, G...
PDF
A Thorough Comparison of Delta Lake, Iceberg and Hudi
PPTX
Data ops in practice
PDF
How Uber scaled its Real Time Infrastructure to Trillion events per day
Distributed SQL Databases Deconstructed
How we solved Real-time User Segmentation using HBase
OSMC 2021 | Introduction into OpenSearch
Event-Driven Microservices With NATS Streaming
Best Practices for ETL with Apache NiFi on Kubernetes - Albert Lewandowski, G...
A Thorough Comparison of Delta Lake, Iceberg and Hudi
Data ops in practice
How Uber scaled its Real Time Infrastructure to Trillion events per day

What's hot (20)

PDF
PostgreSQL Tutorial For Beginners | Edureka
PPTX
Understanding REST APIs in 5 Simple Steps
PDF
Using ClickHouse for Experimentation
PDF
Making Data Timelier and More Reliable with Lakehouse Technology
PPTX
Data Pipelines with Kafka Connect
PDF
What is new in PostgreSQL 14?
PPTX
kafka
KEY
Protocol Buffers and Hadoop at Twitter
PDF
Linux tuning to improve PostgreSQL performance
PPTX
Kubernetes for Beginners: An Introductory Guide
PDF
Uber: Kafka Consumer Proxy
PPTX
Soap vs rest
PDF
PostgreSQL Tutorial for Beginners | Edureka
PDF
Introduction to elasticsearch
 
PDF
Delta from a Data Engineer's Perspective
PPT
MySQL Atchitecture and Concepts
PDF
GraphQL Fundamentals
PDF
Kafka for Microservices – You absolutely need Avro Schemas! | Gerardo Gutierr...
ODP
Presto
ODP
Microservices with Apache Camel
PostgreSQL Tutorial For Beginners | Edureka
Understanding REST APIs in 5 Simple Steps
Using ClickHouse for Experimentation
Making Data Timelier and More Reliable with Lakehouse Technology
Data Pipelines with Kafka Connect
What is new in PostgreSQL 14?
kafka
Protocol Buffers and Hadoop at Twitter
Linux tuning to improve PostgreSQL performance
Kubernetes for Beginners: An Introductory Guide
Uber: Kafka Consumer Proxy
Soap vs rest
PostgreSQL Tutorial for Beginners | Edureka
Introduction to elasticsearch
 
Delta from a Data Engineer's Perspective
MySQL Atchitecture and Concepts
GraphQL Fundamentals
Kafka for Microservices – You absolutely need Avro Schemas! | Gerardo Gutierr...
Presto
Microservices with Apache Camel
Ad

Similar to Database in Microservices - (2nd PostgreSQL Conference Nepal 2023) (20)

PPT
About microservices on cloud computing services
PPT
ZhuSem.ppt
PPT
Microservices and developer pattern saga
PPTX
How to power microservices with MariaDB
PDF
Micro Services Intro
PDF
Building microservices on azure
PDF
Kenzan: Architecting for Microservices
PDF
MySQL Performance Schema in Action
PDF
High Performance SSRS
PDF
Lagom : Reactive microservice framework
PDF
🐬 The future of MySQL is Postgres 🐘
PPTX
Relational RDBMS : MySQL, PostgreSQL and SQL SERVER
PPTX
Megastore by Google
PDF
'How to build efficient backend based on microservice architecture' by Anton ...
PDF
3170722_BDA_GTU_Study_Material_Presentations_Unit-3_29092021094744AM.pdf
PDF
The future of serverless is STATE!
PDF
Introduction to Microservices Architecture - SECCOMP 2020
PPTX
Microservices, docker , kubernetes and many more
ODP
Monolithic to Microservices Architecture - STM 6
PDF
[WSO2Con Asia 2018] Architecting for Container-native Environments
 
About microservices on cloud computing services
ZhuSem.ppt
Microservices and developer pattern saga
How to power microservices with MariaDB
Micro Services Intro
Building microservices on azure
Kenzan: Architecting for Microservices
MySQL Performance Schema in Action
High Performance SSRS
Lagom : Reactive microservice framework
🐬 The future of MySQL is Postgres 🐘
Relational RDBMS : MySQL, PostgreSQL and SQL SERVER
Megastore by Google
'How to build efficient backend based on microservice architecture' by Anton ...
3170722_BDA_GTU_Study_Material_Presentations_Unit-3_29092021094744AM.pdf
The future of serverless is STATE!
Introduction to Microservices Architecture - SECCOMP 2020
Microservices, docker , kubernetes and many more
Monolithic to Microservices Architecture - STM 6
[WSO2Con Asia 2018] Architecting for Container-native Environments
 
Ad

More from Sandip Basnet (7)

PPTX
Open Source: The Student Advantage
PPTX
Harnessing the Power of GraphQL in WordPress
PPTX
WordPress(The Big Picture)
PPTX
Gopher Vs HTTP
PPTX
Flex (fast lexical analyzer generator )
PDF
Open Source Tools (Open Source alternatives for paid tools/software/utilities)
PDF
Open Source Ascol Community(OSAC) Introduction and Overview
Open Source: The Student Advantage
Harnessing the Power of GraphQL in WordPress
WordPress(The Big Picture)
Gopher Vs HTTP
Flex (fast lexical analyzer generator )
Open Source Tools (Open Source alternatives for paid tools/software/utilities)
Open Source Ascol Community(OSAC) Introduction and Overview

Recently uploaded (20)

PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
System and Network Administration Chapter 2
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
AI in Product Development-omnex systems
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
top salesforce developer skills in 2025.pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Transform Your Business with a Software ERP System
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Digital Strategies for Manufacturing Companies
ManageIQ - Sprint 268 Review - Slide Deck
System and Network Administration Chapter 2
2025 Textile ERP Trends: SAP, Odoo & Oracle
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Wondershare Filmora 15 Crack With Activation Key [2025
Understanding Forklifts - TECH EHS Solution
Softaken Excel to vCard Converter Software.pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development
How to Migrate SBCGlobal Email to Yahoo Easily
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
AI in Product Development-omnex systems
PTS Company Brochure 2025 (1).pdf.......
CHAPTER 2 - PM Management and IT Context
Adobe Illustrator 28.6 Crack My Vision of Vector Design
top salesforce developer skills in 2025.pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Transform Your Business with a Software ERP System
Design an Analysis of Algorithms II-SECS-1021-03
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Digital Strategies for Manufacturing Companies

Database in Microservices - (2nd PostgreSQL Conference Nepal 2023)

  • 1. Practices for Database Design in Microservices Architecture Database in Microservices using PostgreSQL Sandip Basnet
  • 2. About me ● Backend Developer. ● Using PostgreSQL for the last 4 years. ● Designing and building scalable and robust backend solutions. ● Believe in identifying issues and finding creative solutions that deliver tangible results. ● Contact: ○ Email: mondyfy@gmail.com ○ LinkedIn: https://www.linkedin.com/in/sandipbasnet
  • 3. Introduction - Microservices architecture is an approach to building large, complex systems as a collection of small, independent services. - Each microservice has its own responsibility and can be developed, deployed, and scaled independently. - Databases are a critical component of microservices architecture, as they store and manage data that is used by multiple services.
  • 4. Problems ● Designing databases for microservices architecture is challenging because there are many different ways to organize data and manage transactions. ● Some common challenges include: ○ Preserving ACID ○ Managing data consistency across multiple services. ■ Strong ■ Eventual ○ Ensuring fault tolerance. ○ Maintaining scalability as the system grows.
  • 5. Approaches ● Architecture: ○ Multi-schema ● Maintain Isolation: ○ CQRS ● Manage transactions: ○ SAGA
  • 6. Shared database/schema ● Services share the same database instance. ● Reduces the complexity of managing multiple databases, but it can also lead to coupling between services. ● To avoid coupling, it's important to use clear boundaries and APIs between services that share the same database.
  • 7. Database per service ● Each microservice having its own dedicated database. ● Greater scalability, fault tolerance, and isolation between services. ● However, it can lead to increased complexity in managing and coordinating data across multiple services.
  • 8. Schema per service ● Each microservice has its own database schema. ● Different from the shared schema -> multiple microservices share the same database schema. ● Provides improved scalability, easy maintenance, and better data isolation between services.
  • 9. CQRS (Command Query Responsibility Segregation) ● Separates read and write operations into different components. ● Improves performance and scalability by allowing each component to scale independently. ● Microservices can implement CQRS by using different databases or database schemas for reads and writes.
  • 10. Saga Pattern ● Way to manage data consistency across microservices in distributed transaction scenarios ● There are two types of patterns: ○ Choreography - each microservice publishes events to trigger the next step in the transaction. ○ Orchestration - a central component coordinates the transaction between microservices.
  • 11. Saga - Choreographer ● Each microservice knows its own part in the transaction and publishes events to trigger the next step. ● Provides greater autonomy and scalability for each microservice. ● However, it can be more difficult to coordinate and manage transactions across multiple services.
  • 12. Saga - Orchestrator ● A central component (the orchestrator) coordinates the transaction between microservices. ● Provides greater control and consistency over the transaction. ● However, it can lead to increased complexity and coupling between services.
  • 15. Designing a microservices database schema ● When designing a microservices database schema with PostgreSQL, it's important to consider the data access patterns and the relationships between different services. ● Some key design principles include normalizing the data to minimize duplication, using foreign keys to enforce relationships, and using indexing to optimize performance. ● It's also important to consider the impact of schema changes on other microservices that may depend on the data.
  • 16. Challenges and considerations ● Managing data consistency across multiple databases, ● Implementing proper security measures, and ● Ensuring proper monitoring and management of the databases. ● Cost ● Scalability
  • 17. Practices for using PostgreSQL in microservices ● Use a separate database/schema for each microservice to ensure isolation and scalability. ● Implement CQRS to separate read and write operations and improve performance. ● Use the SAGA pattern to manage data consistency across multiple microservices. ● Use JSON data types to handle complex data structures. ● Use PostgreSQL's support for ACID transactions to ensure data consistency and reliability.
  • 18. Why PostgreSQL for microservices ● ACID compliance. ● Robust feature sets ○ Support for JSON data types. ○ Ability to handle complex queries. ○ Object-oriented ● SQL Standard ● Open source development & Community
  • 19. Summary ● Designing a database for microservices architecture using PostgreSQL can be a complex task, but by following best practices and design principles, it can lead to a robust and scalable system. ● By using separate databases for each microservice, implementing CQRS and SAGA patterns, and using PostgreSQL's advanced features, developers can build a reliable and scalable microservices.
  • 20. Thank you for your time and attention! I hope this session has been informative and helpful. @mondyfy
  • 21. References ● PostgreSQL as a Microservice: https://tapoueh.org/blog/2021/06/postgresql-as-a-microservice/ ● Monolith to Microservices: Refactoring Relational Database https://programmaticponderings.com/2022/04/14/monolith-to-microservices-refactoring-relational- databases%EF%BF%BC/ ● PostgreSQL in a microservice World: https://momjian.us/main/writings/pgsql/microservices.pdf ● Microservice Architecture: https://github.com/codefarm0/Microservices-Architecture ● Benefits: https://www.prisma.io/dataguide/postgresql/benefits-of-postgresql#object-oriented-database- features