Elixir in Action
Saa Juri
Copyright
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 DepartmentManning Publications Co.20 Baldwin RoadPO Box 761Shelter Island, NY 11964Email:
orders@manning.com2015 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 Mannings 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 RoadPO Box 761Shelter Island, NY 11964 | Development editor: Karen MillerTechnical development editor: Andrew GibsonCopyeditor: Tiffany TaylorProofreader: Barbara MireckiTechnical proofreader: Riza FahmiTypesetter: Dottie MarsicoCover designer: Marija Tudor |
ISBN 9781617292019
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 EBM 20 19 18 17 16 15
Dedication
To Renata, my wonderful wife
Brief Table of Contents
Table of Contents
Preface
In 2010, I was given the task of implementing a system to transmit frequent updates to a few thousand connected users in near real time. My company at the time was mostly using Ruby on Rails. But for such a highly concurrent challenge, I needed something more suitable. Following the suggestion of my CTO, I looked into Erlang, read some material, made a prototype, and performed a load test. I was impressed with the initial results and moved on to implement the whole thing in Erlang. A few months later, the system was shipped, and its been doing its job ever since.
As time passed, I began to increasingly appreciate Erlang and the way it helped me manage such a complex system. Gradually, I came to prefer Erlang over the technologies I had used previously. I began to evangelize the language, first in my company and then at local events, then, finally, at the end of 2012, I started the blog The Erlangelist (http://theerlangelist.com), where I aim to showcase the advantages of Erlang to programmers from OO backgrounds.
Because Erlang is an unusual language, I began experimenting with Elixir, hoping it would help me explain the beauty of Erlang in a way that would resonate with OO programmers. Despite the fact that it was at an early stage of development (at the time, it was at version 0.8), I was immediately impressed with Elixirs maturity and the way it integrated with Erlang. Soon, I started using Elixir to develop new features for my Erlang-based system.
A few months later, I was contacted by Michael Stephens of Manning, who asked me if I was interested in writing a book about Elixir. At the time, two Elixir books were already in the making. After some consideration, I decided there was space for another book that would approach the topic from a different angle. In the proposal for this book, I wrote, Essentially, I am proposing a book which I would have personally found useful when I started learning Erlang a couple of years ago. In other words, this book aims to bring programmers new to Elixir and Erlang to the point where they can develop complex systems on their own. In the process, I tried to recollect the mistakes I had made in Erlang, most of which were due to my OO background and inexperience with the language. This book tries to point you in the right direction so you dont end up making those same mistakes.
Almost two years later, Im excited the work is finally done. I hope youll enjoy reading the book, learn a lot from it, and have a chance to apply your new knowledge in your work!
Acknowledgments
Writing this book required a significant investment of my time, so, above all, I want to thank my wife Renata for her endless support and patience during those long hours and busy weekends.
Id like to thank Manning for giving me the chance to write this book. In particular, thanks to Michael Stephens for making the initial contact, to Marjan Bace for giving me a chance to write this book, to Bert Bates for setting me in the right direction, to Karen Miller for keeping me on track, to Aleksandar Dragosavljevic for managing the review process, to Kevin Sullivan for overseeing production, to Tiffany Taylor for transforming my English into proper English, and to Candace Gillhoolley and Ana Romac for promoting the book.
The material in this book has been significantly improved thanks to great feedback from reviewers and early readers. Above all, I wish to thank Andrew Gibson for useful feedback, great insights, and for rescuing me when I got stuck at the last hurdle. Id also like to thank Alexei Sholik and Peter Minten, who provided excellent immediate technical feedback during the writing process.
A big thank you to all the technical reviewers: Al Rahimi, Alan Lenton, Alexey Galliulin, Christopher Bailey, Christopher Haupt, Heather Campbell, Jeroen Benckhuijsen, Kosmas Chatzimichalis, Mark Ryall, Mohsen Mostafa Jokar, Tom Geudens, Tomer Elmalem, and Ved Antani. Special thanks to Riza Fahmi who reviewed the manuscript carefully one last time shortly before it went into production.
I also wish to thank all the readers who bought and read the MEAP (Manning Early Access Program) version and provided useful comments. Thank you for taking the time to read my ramblings and for providing insightful feedback.
The people who gave us Elixir and Erlang, including the original inventors, core team members, and contributors, deserve a special mention. Thank you for creating such great products, which make my job easier and more fun. Finally, special thanks to all the members of the Elixir community; this is the nicest and friendliest developer community Ive ever seen!
About this Book
Elixir is a modern functional programming language for building large-scale scalable, distributed, fault-tolerant systems for the Erlang virtual machine. Although the language is compelling in its own right, arguably its biggest advantage is that it targets the Erlang platform.
Erlang was made to help developers deal with the challenge of high availability. Originally, the product was intended for development of telecommunication systems; but today its used in all kinds of domains, such as collaboration tools, online payment systems, real-time bidding systems, database servers, and multiplayer online games, to name only a few examples. If youre developing a system that must provide service to a multitude of users around the world, you want that system to function continuously, without noticeable downtime, regardless of any software or hardware problems that occur at runtime. Otherwise, significant and frequent outages will leave end users unhappy, and ultimately they may seek alternative solutions. A system with frequent downtime is unreliable and unusable, and thus fails to fulfill its intended purpose. Therefore, high availability becomes an increasingly important propertyand Erlang can help you achieve that.