Practical Microservices
Build Event-Driven Architectures with Event Sourcing and CQRS
by Ethan Garofolo
MVC and CRUD make software easier to write, but harder to change.
Microservice-based architectures can help even the smallest of projects
remain agile in the long term, but most tutorials meander in theory or
completely miss the point of what it means to be microservice based.
Roll up your sleeves with real projects and learn the most important
concepts of evented architectures. You’ll have your own deployable,
testable project and a direction for where to go next.
Much ink has been spilled on the topic of microservices, but all of this
writing fails to accurately identify what makes a system a monolith,
define what microservices are, or give complete, practical examples, so
you’re probably left thinking they have nothing to offer you. You don’t
have to be at Google or Facebook scale to benefit from a
microservice-based architecture. Microservices will keep even small and
medium teams productive by keeping the pieces of your system focused and
decoupled.
Discover the basics of message-based architectures, render the same
state in different shapes to fit the task at hand, and learn what it is
that makes something a monolith (it has nothing to do with how many
machines you deploy to). Conserve resources by performing background
jobs with microservices. Deploy specialized microservices for
registration, authentication, payment processing, e-mail, and more. Tune
your services by defining appropriate service boundaries. Deploy your
services effectively for continuous integration. Master debugging
techniques that work across different services. You’ll finish with a
deployable system and skills you can apply to your current project.
Add the responsiveness and flexibility of microservices to your project,
no matter what the size or complexity.
What You Need
While the principles of this book transcend programming language, the
code examples are in Node.js because JavaScript, for better or worse, is
widely read. You’ll use PostgreSQL for data storage, so familiarity with
it is a plus. The books does provide Docker images to make working with
PostgreSQL a bit easier, but extensive Docker knowledge is not required.
Resources
Releases:
- P1.0 2020/04/08
- B8.0 2020/03/24
- B7.0 2020/02/14
- B6.0 2019/12/23
- Introduction
- Fundamentals
- You Have a New Project
excerpt
- Kicking Off Video Tutorials
- Building the Bones
- Mounting Middleware
- Injecting Dependencies
- Taking the Server for a Spin and Starting the Database
- Serving the Home Page
- Connecting the Home Page Application
- Mounting the Home Application into Express
- Building the Record Views Application
- Recording State Changes
- Charting a New Course
- What You’ve Done So Far
- Writing Messages
- Unmasking the Monolith
- Trying to Compress Water
- Extracting “Microservices”
- Defining Services
- Getting Components to Do Things
- Representing Messages in Code
excerpt
- Naming Messages
- Storing State as Events
- Storing Messages in Streams
- Defining Component Boundaries
- Recording Video Views
- Writing Your First Message
- (Re)configuring the Record-Viewings Application
- Hanging a Lantern
- What You’ve Done So Far
- Putting Data in a Message Store
- Defining Requirements
- Fleshing Out Message Structure
- Surveying Message DB
- Scaffolding the Message Store Code
- Connecting to Message DB
- Writing write
- Adding Optimistic Concurrency Control to Our Writes
- “Can’t Kafka Do All of This?”
- What You’ve Done So Far
- Projecting Data into Useful Shapes
- Handling Events
- (Re)Introducing the RDBMS
- Writing Your First Aggregator
- Handling Asynchronous Messages
excerpt
- Getting Idempotent with It
- Connecting to the Live Message Flow
- Configuring the Aggregator
- Having the Home Page Application Use the New View Data
- Coming to Terms with Data Duplication
- What You’ve Done So Far
- Subscribing to the Message Store
- Sketching the Subscription Process
- Managing the Current Read Position
- Fetching and Processing Batches of Messages
- Orchestrating the Subscription
- Reading the Last Message in a Stream
- Reading a Stream’s Messages
- Adding the Read Functions to the Message Store’s Interface
- Starting the Server
- What You’ve Done So Far
- Fleshing Out the System
- Registering Users
- Discovering Domain Messages
- Starting with the Business Process
- Translating the Business Processes into Events and Commands
- Fleshing Out the Identity Messages
- Examples from Other Domains
- Adding Registration to Our System
- Turning Registration Requests into Commands
- Superficially Validating User Input
- Ensuring Uniqueness of Email Addresses
- Finishing the Application
- Validating Eventually Consistent Data
- Coping with Trade-Offs
- What You’ve Done So Far
- Implementing Your First Component
- Fetching a Stream’s Current State
- Joining the “I Wrote a Microservice” Club
- Wiring the Identity Component into the System
- Disambiguating “Projections” and “Replaying”
- Taking It Further
- What You’ve Done So Far
- Authenticating Users
- Aggregating Registered Events
- Discovering the Authentication Events and Commands
- Letting Users in the Door
- Using Third-Party Authentication
- What You’ve Done So Far
- Adding an Email Component
- Discovering the Email Component Messages
- Addressing Idempotence
- Adding the Component
- Sending the Email
- Running the Component
- Adding Email to the Registration Process
- Recording Registration Emails
- Making the Message Store Aware of Origin Streams
- Revisiting Idempotence
- Orchestrating Components vs. Choreographing Components
- What You’ve Done So Far
- Performing Background Jobs with Microservices
- Accidental Complexity
- Use Case #1: Sending Emails
- Use Case #2: Transcoding Videos
- Describing the Creators Portal
- Aggregating Is Also for Other Teams
- Building the Video Publishing Component
- Accepting Potential Duplication
- What You’ve Done So Far
- Building Async-Aware User Interfaces
- Defining Video Metadata Messages
- Responding to Users When the Response Isn’t
Immediately Available
- Adding Validation to a Component
- Aggregating Naming Results
- Applying Naming Events to the Creators Portal View Data
- Justifying Our UI Decision
- What You’ve Done So Far
- Tools of the Trade
- Deploying Components
- Creating the Heroku “App”
- Configuring the “App”
- Installing Message DB
- Deploying the System
- Distributing the System
- Deploying Databases
- What You’ve Done So Far
- Debugging Components
- Priming the Database with Example Data
- Introducing the Admin Portal
- Creating Users
- Wiring the Users View into the Admin Portal
- Hooking the Admin Portal into the Rest of the System
- Inspecting the Results So Far
- Thinking Through the Expected Flow
- Correlators Gonna…Correlate?
- Imagining Our Way to Good System Monitoring
- Starting from the Beginning
- Viewing Messages by Stream
- Augmenting the Message Store for $any and $all
- What You’ve Done So Far
- Testing in a Microservices Architecture
- Revisiting the Fundamentals
- Writing Tests for Autonomous Components
- Writing Tests for Message-Writing Applications
- Keeping It Simple
- Dropping Testing?
- What You’ve Done So Far
- Continuing the Journey
- Handling Concurrency
- Snapshotting
- Changing the Message Contract
- Using Different Programming Languages
- Making Use of Monoliths
- What You’ve Done So Far
Author
Ethan’s love of computer programming began at age 4 when his dad taught
him to make the family TI-99/4A beep. For the past 12 years Ethan has
worked professionally up and down the abstraction hierarchy, from junior
developer up to platform architect, finally settling into
microservice-based architectures for the past 3 years.