1
Architecture Patterns with Python
Enabling Test-Driven Development, Domain-Driven Design, and
Event-Driven Microservices
Harry Percival and Bob Gregory
2
Architecture Patterns with Python
by
Harry Percival and Bob Gregory
Copyright © 2020 Harry Percival and Bob Gregory. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol,
CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use.
Online editions are also available for most titles (http://oreilly.com). For more
information, contact our corporate/institutional sales department: 800-998-9938 or
corporate@oreilly.com.
Acquisitions Editor: Ryan Shaw
Development Editor: Corbin Collins
Production Editor: Katherine Tozer
Copyeditor: Sharon Wilkey
Proofreader: Arthur Johnson
Indexer: Ellen Troutman-Zaig
Interior Designer: David Futato
Cover Designer: Karen Montgomery
Illustrator: Rebecca Demarest
March 2020: First Edition
Revision History for the First Edition
• 2020-03-05: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781492052203 for release details.
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Architecture
Patterns
with Python, the cover image, and related trade dress are trademarks of
O’Reilly Media, Inc.
The views expressed in this work are those of the authors and do not represent the
publisher’s
views. While the publisher and the authors have used good faith efforts to
ensure that the information and instructions contained in this work are accurate, the
publisher
and the authors disclaim all responsibility for errors or omissions, including
3
without limitation responsibility for damages resulting from the use of or reliance on
this work. Use of the information and instructions contained in this work is at your
own risk. If any code samples or other technology this work contains or describes is
subject to open source licenses or the intellectual property rights of others, it is your
responsibility to ensure that your use thereof complies with such licenses and/or
rights.
978-1-492-05220-3
[LSI]
4
Preface
You may be wondering who we are and why we wrote this book.
At the end of Harry’s last book, Test-Driven Development with Python (O’Reilly), he
found himself asking a bunch of questions about architecture, such as, What’s the best
way of structuring your application so that it’s easy to test? More specifically, so that
your core business logic is covered by unit tests, and so that you minimize the number
of integration and end-to-end tests you need? He made vague references to
“Hexagonal Architecture” and “Ports and Adapters” and “Functional Core,
Imperative Shell,” but if he was honest, he’d have to admit that these weren’t things
he really understood or had done in practice.
And then he was lucky enough to run into Bob, who has the answers to all these
questions.
Bob ended up a software architect because nobody else on his team was doing it. He
turned out to be pretty bad at it, but he was lucky enough to run into Ian Cooper, who
taught him new ways of writing and thinking about code.
Managing Complexity, Solving Business Problems
We both work for MADE.com, a European ecommerce company that sells furniture
online; there, we apply the techniques in this book to build distributed systems that
model real-world business problems. Our example domain is the first system Bob
built
for MADE, and this book is an attempt to write down all the stuff we have to
teach new programmers when they join one of our teams.
MADE.com operates a global supply chain of freight partners and manufacturers. To
keep costs low, we try to optimize the delivery of stock to our warehouses so that we
don’t have unsold goods lying around the place.
Ideally, the sofa that you want to buy will arrive in port on the very day that you
decide to buy it, and we’ll ship it straight to your house without ever storing it.
Getting the timing right is a tricky balancing act when goods take three months to
arrive by container ship. Along the way, things get broken or water damaged, storms
cause unexpected delays, logistics partners mishandle goods, paperwork goes
missing, customers change their minds and amend their orders, and so on.
We solve those problems by building intelligent software representing the kinds of
operations taking place in the real world so that we can automate as much of the
business
as possible.
5