Cover....1
Title Page....2
Copyright and Credit....3
Dedicated....4
Contributors....5
Table of Contents....8
Preface....12
Chapter 1: Concurrency – A High-Level Overview....18
Technical Requirements....18
Concurrency and parallelism....18
Shared memory versus message passing....25
Atomicity, race, deadlocks, and starvation....28
Summary....38
Question....38
Further reading....38
Chapter 2: Go Concurrency Primitives....40
Technical Requirements....40
Goroutines....40
Channels....47
Mutex....61
Wait groups....66
Condition variables....69
Summary....73
Questions....73
Chapter 3: The Go Memory Model....74
Why a memory model is necessary....74
The happened-before relationship between memory operations....75
Synchronization characteristics of Go concurrency primitives....78
Package initialization....78
Goroutines....79
Channels....80
Mutexes....82
Atomic memory operations....83
Map, Once, and WaitGroup....83
Summary....85
Further reading....85
Chapter 4: Some Well-Known Concurrency Problems....86
Technical Requirements....86
The producer-consumer problem....86
The dining philosophers problem....90
Rate limiting....96
Summary....102
Chapter 5: Worker Pools and Pipelines....104
Technical Requirements....104
Worker pools....104
Pipelines, fan-out, and fan-in....110
Asynchronous pipeline....113
Fan-out/fan-in....115
Fan-in with ordering....120
Summary....125
Questions....125
Chapter 6: Error Handling....126
Error handling....126
Pipelines....132
Servers....133
Panics....134
Summary....137
Chapter 7: Timers and Tickers....138
Technical Requirements....138
Timers – running something later....138
Tickers – running something periodically....141
Heartbeats....143
Summary....145
Chapter 8: Handling Requests Concurrently....146
Technical Requirements....146
The context, cancelations, and timeouts....147
Backend services....152
Distributing work and collecting results....157
Semaphores – limiting concurrency....160
Streaming data....161
Dealing with multiple streams....171
Summary....176
Chapter 9: Atomic Memory Operations....178
Technical Requirements....178
Memory guarantees....178
Compare and swap....180
Practical uses of atomics....181
Counters....182
Heartbeat and progress meter....182
Cancellations....185
Detecting change....186
Summary....187
Chapter 10: Troubleshooting Concurrency Issues....188
Technical Requirements....188
Reading stack traces....189
Detecting failures and healing....194
Debugging anomalies....201
Summary....203
Further reading....203
Index....204
Other Books You May Enjoy....209
The Go language has been gaining momentum due to its treatment of concurrency as a core language feature, making concurrent programming more accessible than ever. However, concurrency is still an inherently difficult skill to master, since it requires the development of the right mindset to decompose problems into concurrent components correctly. This book will guide you in deepening your understanding of concurrency and show you how to make the most of its advantages.
You'll start by learning what guarantees are offered by the language when running concurrent programs. Through multiple examples, you will see how to use this information to develop concurrent algorithms that run without data races and complete successfully. You'll also find out all you need to know about multiple common concurrency patterns, such as worker pools, asynchronous pipelines, fan-in/fan-out, scheduling periodic or future tasks, and error and panic handling in goroutines.
The central theme of this book is to give you, the developer, an understanding of why concurrent programs behave the way they do, and how they can be used to build correct programs that work the same way in all platforms.
By the time you finish the final chapter, you'll be able to develop, analyze, and troubleshoot concurrent algorithms written in Go.
If you are a developer with basic knowledge of Go and are looking to gain expertise in highly concurrent backend application development, then this book is for you. Intermediate Go developers who want to make their backend systems more robust and scalable will also find plenty of useful information. Prior exposure Go is a prerequisite.