Taming the Database

Harry Cummings, Softwire

Taming the Database

  • Maintainability
  • Testability
  • Performance

Motivation

  • Implementation
  • Deployment
  • Architecture

Overview

Implementation

Testing

  • Runs in-process
  • Can run in-memory
  • Available via NuGet

Lightweight Databases

  • Runs in-process/memory
  • Compatibility modes
  • Available via Maven

SQLite

H2

  • Requires a file
  • Close-ish to SQL Server
  • Now in end-of-life

SQL Server Compact

  • Requires installation
  • More heavyweight
  • Very close to SQL Server

SQL Server Express

  • Fast!
  • Easy to isolate
  • Can't really test...
    • Persistence
    • Transactionality
    • Complex querying

Testing approaches

  • Fast but not unit-test-fast
  • Easy to isolate
  • Reasonably realistic
    • May be subtle differences
    • ORMs can help with this

Faking the library

In-process DB

  • Slow
  • Very realistic
  • Large setup overhead
  • Isolation requires care

Separate instance

  • Slow
  • Completely realistic
  • Isolation is challenging

Shared instance

Testing Approaches

Faking the library

In-process DB

Isolated real DB

Shared database

Deployment

Change management

Recreate and seed

  • Easy to autogenerate
  • Data-destroying

Incremental migration

  • Hard to autogenerate
  • Data preserving?
  • Up/down vs. forward only

Deployment

Breaking changes

Database first

  • Deploy back-compatible DB change
  • Deploy application
  • Remove old DB functionality

Application first

  • Deploy back-compatible app change
  • Deploy database
  • Remove old app functionality

Architecture

Architecture

  • Attempts to achieve separation of concerns
  • Actually makes SRP difficult
  • Can make the database the foundation
  • Encourages using the DB as an integration point

Presentation

Logic

Data

Traditional 3-layer architecture

Architecture

  • Puts business logic at the core of the application
  • Treats the database as a collaborator
  • Discourages leaky persistence abstraction
  • Makes APIs the integration point

Ports and Adapters ('Hexagonal')

Adaptors

Ports

Core

Architecture

  • True separation of persistence concerns
  • Events are immutable, other data transient
  • Aids performance and scalability
  • Reduces need for schema migration
  • Very flexible and loosely coupled

Event Sourcing / Stream Processing 

Application

Raw

Aggr.

Summary

ORMs

Tests

Change

Structure

Moderation

Use cases

Performance

Variety

Isolation

Realism

Compatibility

Existing data

Data flow

App core

Taming the Database

?

Taming the Database

By hgcummings

Taming the Database

SDD Conf 2015

  • 2,877