Redis in Action
Josiah L. Carlson
Download from Wow! eBook
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 Department Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email:
orders@manning.com2013 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 261Shelter Island, NY 11964 |
Development editor: Elizabeth LexleighTechnical proofreaders: James Philips, Kevin Chang, and Nicholas LindgrenJava translator: Eric Van DewoestineCopyeditor: Benjamin BergProofreader: Katie TennantTypesetter: Gordan SalinovicCover designer: Marija Tudor
ISBN 9781935182054
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 MAL 18 17 16 15 14 13
Dedication
To my dear wife, See Luan, and to our baby girl, Mikela
Brief Table of Contents
Table of Contents
Foreword
Redis was created about three years ago for practical reasons: basically, I was trying to do the impossible with an on-disk SQL database. I was handling a large write-heavy load with the only hardware I was able to afforda little virtualized instance.
My problem was conceptually simple: my server was receiving a stream of page views from multiple websites using a small JavaScript tracker. I needed to store the latest n page views for every site and show them in real time to users connected to a web interface, while maintaining a small history.
With a peak load of a few thousand page views per second, whatever my database schema was, and whatever trade-offs I was willing to pick, there was no way for my SQL store to handle the load with such poor hardware. My inability to upgrade the hardware for cost concerns coupled with the feeling that to handle a capped list of values shouldnt have been so hard, after all, gave me the idea of creating a throw-away prototype of an in-memory data store that could handle lists as a native data type, with constant-time pop and push operations on both sides of the lists. To make a long story short, the concept worked, I rewrote the first prototype using the C language, added a fork-based persistence feature, and Redis was born.
Fast-forward to the present. After three years, the project has evolved in significant ways. We have a more robust system now, and with Redis 2.6 just released and the major work in progress being cluster and HA features, Redis is entering its maturity period. One of the most remarkable advancements in the Redis ecosystem, in my opinion, is its community of users and contributors, from the redis.io website to the Redis Google Group. Stemming from the GitHub issues system, there are thousands of people involved in the project, writing client libraries, contributing fixes, and helping other users.
Redis is still a community project: its BSD licensed. There are no closed source add-ons or enhanced versions you need to pay for. The reference documentation is as accurate as possible, and its extremely easy to get help and get in touch with Redis developers or experts.
Redis started in a pragmatic way, with a programmer who needed to get things done and couldnt find the right tool for the tasks at hand. This is why I think a theoretical book wouldnt serve Redis well, and why I like Redis in Action: its a book for people that want to get things done. It doesnt limit itself to a sterile description of the API; Redis features and data types are explored in depth using compelling examples.
At the same time, Redis in Action comes from the Redis community, and more specifically from someone who, before publishing this book, has already helped hundreds of Redis users in many different waysfrom schema designs to hardware latency issues. The Redis Group is full of Josiahs advice and contributions.
The fact that system operation topics are also covered is a big plus. The reality is that most people need to both develop the application software and handle the deployment of the server. And Id say that you need to understand system operations and the fundamental limits of the hardware and the system software youre using in order to write an application that makes the best use of both.
The result of these efforts is a book that will get you into Redis in a direct way, pointing your attention in the right directions to avoid common pitfalls. I think Redis in Action is a great addition to the Redis ecosystem and will be welcomed by the community of Redis users.
S ALVATORE S ANFILIPPO
C REATOR OF R EDIS
Preface
In March of 2010 I began working in Beverly Hills with Chris Testa, a friend Id met while at Google in Santa Monica. He had hired me to be the architect of a small startup that he was team lead/director for; I was to be the research branch.
While talking one afternoon about how to solve an unrelated problem, Chris mentioned Redis as a database that I might find interesting (given my education in theoretical computer science). Several weeks later, after using and patching Redis for our purposes, I started participating on the mailing list, offering advice and a patch or two.
As time went on, I used Redis for a wider variety of projects at our startup: searching, an ad targeting engine, a Twitter analytics engine, and many pieces to connect the different parts of our infrastructure. Each project forced me to learn more about Redis. And as I saw others on the mailing list using Redis, asking questions, I couldnt help but offer more and more advice (my all-time favorite was actually a job-search problem, which became ), becoming one of the most prolific posters on the Redis mailing list.
In late September 2011, while on my honeymoon in Paris, I received a call from a Manning Publications acquisitions editor named Michael Stephens. I didnt receive the call immediately, because my phone doesnt work outside the United States. And due to bugs in my phones firmware, I didnt even receive the message until the second week of October.
When I finally got the message and spoke to Michael, I learned that someone at Manning had decided that it was about time to publish Redis in Action. After reading the relevant mailing lists and asking around for suggestions as to who should write the book, my name came up. Luckily, Manning was still taking book proposals when I called.
After a few weeks of discussions and a few book proposal revisions (primarily resulting from farming several dozen of my past Redis mailing list advice posts), Manning accepted my proposal, and I started writing. Its now around 17 months since I first spoke with Michael, and