MANNING Josiah L. Carlson FOREWORD BY Salvatore Sanfilippo IN ACTION www.it-ebooks.info Redis in Action www.it-ebooks.info www.it-ebooks.info Redis in Action JOSIAH L. CARLSON MANNING Shelter Island www.it-ebooks.info 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.com ©2013 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. Development editor: Elizabeth Lexleigh 20 Baldwin Road Technical proofreaders: James Philips, Kevin Chang, PO Box 261 and Nicholas Lindgren Shelter Island, NY 11964 Java translator: Eric Van Dewoestine Copyeditor: Benjamin Berg Proofreader: Katie Tennant Typesetter: Gordan Salinovic Cover 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 www.it-ebooks.info To my dear wife, See Luan, and to our baby girl, Mikela www.it-ebooks.info www.it-ebooks.info vii brief contents PART 1 GETTING STARTED . 1 1 ■ Getting to know Redis 3 2 ■ Anatomy of a Redis web application 24 PART 2 CORE CONCEPTS 37 3 ■ Commands in Redis 39 4 ■ Keeping data safe and ensuring performance 63 5 ■ Using Redis for application support 90 6 ■ Application components in Redis 110 7 ■ Search-based applications 154 8 ■ Building a simple social network 185 PART 3 NEXT STEPS . 207 9 ■ Reducing memory use 209 10 ■ Scaling Redis 228 11 ■ Scripting Redis with Lua 249 www.it-ebooks.info www.it-ebooks.info ix contents foreword xv preface xvii acknowledgments xix about this book xxi about the cover illustration xxv PART 1 GETTING STARTED 1 1 Getting to know Redis 3 1.1 What is Redis? 4 Redis compared to other databases and software 4 ■ Other features 6 ■ Why Redis? 6 1.2 What Redis data structures look like 7 Strings in Redis 9 ■ Lists in Redis 10 ■ Sets in Redis 11 Hashes in Redis 12 ■ Sorted sets in Redis 13 1.3 Hello Redis 15 Voting on articles 15 ■ Posting and fetching articles 19 Grouping articles 20 1.4 Getting help 22 1.5 Summary 22 www.it-ebooks.info [...]... listings from the Manning website, www.manning.com/RedisinAction If you would like to see translations into other programming languages or would like to browse the Python source code online, you can find the source code in the GitHub repository, https://github.com/josiahcarlson/ redis- in- action Author Online The purchase of Redis in Action includes free access to a private web forum run by Manning... have a computer with Redis, Python, and the redis- py library installed so that you can try everything out while reading REMINDER ABOUT INSTALLING REDIS AND PYTHON Before you continue, you’ll want to install Redis and Python Again, quick and dirty installation instructions for both Redis and Python can be found in appendix A Even quicker and dirtier instructions for Debian-based Linux distributions are... 77 4.4 Redis transactions 76 ■ Replacing a failed 78 Defining users and their inventory 79 Listing items in the marketplace 80 Purchasing items 82 ■ ■ 4.5 Non-transactional pipelines www.it-ebooks.info 84 xi CONTENTS 4.6 4.7 5 Performance considerations 87 Summary 89 Using Redis for application support 90 5.1 Logging to Redis Recent logs 5.2 91 ■ 91 Common logs 92 Counters and statistics 93 Storing counters... I’m finishing up now I’ve spent a full year of evenings and weekends producing a book to help others understand and utilize one of the most interesting technologies I’ve come across—more interesting than almost anything I’ve run into since the day I sat down at my family’s first computer, 20 years ago this past Christmas My only regret in all of this is not having had the foresight to invent Redis in. .. www.it-ebooks.info ABOUT THIS BOOK xxiii In chapter 6, I introduce components that are very useful when dealing with growing applications, including autocomplete, locking, task queues, messaging, and even file distribution Through chapter 7, I introduce and deeply examine a class of search-based problems and solutions that can change the way you think about data querying and filtering Chapter 8 goes in depth into... network, and includes implementations for the entire back end, including a streaming API Chapter 9 discusses the major techniques for reducing memory use when using Redis at scale, including structure sharding and the use of short structures Chapter 10 discusses the horizontal sharding and slaving of Redis to offer greater performance and access to more memory when a single Redis server can’t sustain your... 232 Handling shard configuration 233 connection decorator 234 10.3 Scaling complex queries Scaling search query volume 236 Scaling a social network 241 10.4 11 Creating a server-sharded ■ 236 ■ Scaling search index size 236 Summary 248 Scripting Redis with Lua 249 11.1 Adding functionality without writing C Loading Lua scripts into Redis 250 message 252 11.2 ■ 250 Creating a new status Rewriting locks... 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 SALVATORE SANFILIPPO CREATOR OF REDIS www.it-ebooks.info... point of this book, which is solving problems with Redis, and not “look at this cool stuff we can do with Python.” For this section, we’ll use a redis- cli console to interact with Redis Let’s get started with the first and simplest structure available in Redis: STRINGs 1.2.1 Strings in Redis In Redis, STRINGs are similar to strings that Key name Type of value we see in other languages or other key-value... teamed up with Redis author Salvatore Sanfilippo to produce a one-hour introduction for using Spring with Redis available at http://www.springsource.org/ spring-data /redis 1.1 What is Redis? When I say that Redis is a database, I’m only telling a partial truth Redis is a very fast non-relational database that stores a mapping of keys to five different types of values Redis supports in- memory persistent . MANNING Josiah L. Carlson FOREWORD BY Salvatore Sanfilippo IN ACTION www.it-ebooks.info Redis in Action www.it-ebooks.info www.it-ebooks.info Redis in Action JOSIAH L. CARLSON MANNING Shelter. is Redis? 4 Redis compared to other databases and software 4 ■ Other features 6 ■ Why Redis? 6 1.2 What Redis data structures look like 7 Strings in Redis 9 ■ Lists in Redis 10 ■ Sets in Redis. 11 Hashes in Redis 12 ■ Sorted sets in Redis 13 1.3 Hello Redis 15 Voting on articles 15 ■ Posting and fetching articles 19 Grouping articles 20 1.4 Getting help 22 1.5 Summary 22 www.it-ebooks.info CONTENTS x 2