Reactive Design Patterns
ROLAND KUHN
WITH BRIAN HANAFEE
AND JAMIE ALLEN
FOREWORD BY JONAS BONÉR
M A N N I N G
SHELTER ISLAND
For online information and ordering of this and other Manning books, please visit
www.manning.com. The publisher offers discounts on this book when ordered in quantity.
For more information, please contact
Special Sales Department
Manning Publications Co.
20 Baldwin Road
PO Box 761
Shelter Island, NY 11964
Email: orders@manning.com
©2017 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in
any form or by means electronic, mechanical, photocopying, or otherwise, without prior written
permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in the book, and Manning
Publications was aware of a trademark claim, the designations have been printed in initial caps
or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to have
the books we publish printed on acid-free paper, and we exert our best efforts to that end.
Recognizing also our responsibility to conserve the resources of our planet, Manning books
are printed on paper that is at least 15 percent recycled and processed without the use of
elemental chlorine.
Manning Publications Co.
20 Baldwin Road
PO Box 761
Shelter Island, NY 11964
Development editor: Jennifer Stout
Technical development editor: Brian Hanafee
Project editors: Tiffany Taylor and Janet Vail
Line editor: Ben Kovitz
Copyeditor: Tiffany Taylor
Proofreader: Katie Tennant
Technical proofreader: Thomas Lockney
Typesetter: Dottie Marsico
Cover designer: Leslie Haimes
ISBN 9781617291807
Printed in the United States of America
1 2 3 4 5 6 70 8 9 10 – EBM – 22 21 20 19 18 17
To my children
— Roland
brief contents
PART 1 INTRODUCTION................................................................ 1
1 ■ Why Reactive? 3
2 ■ A walk-through of the Reactive Manifesto 12
3 ■ Tools of the trade 39
PART 2 THE PHILOSOPHY IN A NUTSHELL ................................... 65
4 ■ Message passing 67
5 ■ Location transparency 81
6 ■ Divide and conquer 91
7 ■ Principled failure handling 100
8 ■ Delimited consistency 105
9 ■ Nondeterminism by need 113
10 ■ Message flow 120
PART 3 PATTERNS .................................................................... 125
11 ■ Testing reactive applications 127
12 ■ Fault tolerance and recovery patterns 162
13 ■ Replication patterns 184
14 ■ Resource-management patterns 220
15 ■ Message flow patterns 255
16 ■ Flow control patterns 294
17 ■ State management and persistence patterns 311
v
contents
foreword xv
preface
xvii
acknowledgments
about this book
about the authors
xix
xxi
xxiv
PART 1
INTRODUCTION.................................................1
1 Why Reactive?
3
1.1 The anatomy of a Reactive application 4
1.2 Coping with load 5
1.3 Coping with failures 7
1.4 Making the system responsive 8
1.5 Avoiding the ball of mud 10
1.6
1.7
Integrating nonreactive components 10
Summary 11
2 A walk-through of the Reactive Manifesto
2.1 Reacting to users 12
12
Understanding the traditional approach 13 ■ Analyzing
latency with a shared resource 15 ■ Limiting maximum
latency with a queue 16
vii