← Back to Guide
Chapter 07

7. Data Layer

Where knowledge lives: Databases, caches, and storage.

The Data Layer

Services need to store state. In a modern architecture, we don't just dump everything into one giant SQL database. We use the right tool for the job.

Data Layer Diagram

Types of Data Stores

  1. Relational Databases (SQL):

    • Examples: PostgreSQL, MySQL.
    • Use Case: Structured data with strict relationships (e.g., Users, Orders, Financial transactions).
    • HA Strategy: Master-Slave replication. Writes go to Master, reads can be spread across Slaves.
  2. NoSQL Databases:

    • Examples: MongoDB, Cassandra.
    • Use Case: Unstructured data, massive scale, high write throughput (e.g., Social media feeds, IoT sensor data).
  3. In-Memory Caches:

    • Examples: Redis, Memcached.
    • Use Case: Storing frequently accessed data in RAM for sub-millisecond access (e.g., Session tokens, Leaderboards).

Polyglot Persistence

This approach of using multiple database technologies within a single system is called Polyglot Persistence. It allows each service to be highly optimized for its specific data access patterns.