this document was created with prince, a great way of getting … · 2015. 12. 12. · anatomy of a...

7

Upload: others

Post on 05-Sep-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: This document was created with Prince, a great way of getting … · 2015. 12. 12. · Anatomy of a Riak Node..... 104 What Happens When a Node Joins a Cluster ..... 105 Leaving a
www.princexml.com
Prince - Non-commercial License
This document was created with Prince, a great way of getting web content onto paper.
Page 2: This document was created with Prince, a great way of getting … · 2015. 12. 12. · Anatomy of a Riak Node..... 104 What Happens When a Node Joins a Cluster ..... 105 Leaving a

Riak Handbook

Mathias Meyer

Revision 27e1e7fb

Page 3: This document was created with Prince, a great way of getting … · 2015. 12. 12. · Anatomy of a Riak Node..... 104 What Happens When a Node Joins a Cluster ..... 105 Leaving a

Table of ContentsIntroduction ................................................................................................... 8

Thank You ............................................................................................. 8How to read the book............................................................................. 9Feedback ................................................................................................. 9Code........................................................................................................ 9Changelog .............................................................................................. 9

CAP Theorem.............................................................................................. 11The CAP Theorem is Not Absolute ........................................................ 12Fine-Tuning CAP with Quorums .......................................................... 13N, R, W, Quorums, Oh My!.................................................................... 13How Quorums Affect CAP ..................................................................... 14A Word of CAP Wisdom......................................................................... 15Further Reading ....................................................................................... 15

Eventual Consistency................................................................................... 15Consistency in Quorum-Based Systems ................................................. 16

Consistent Hashing ...................................................................................... 16Sharding and Rehashing........................................................................... 16A Better Way............................................................................................ 17Enter Consistent Hashing ........................................................................ 17Looking up an Object .............................................................................. 19Problems with Consistent Hashing ......................................................... 20Dealing with Overload and Data Loss ..................................................... 21

Amazon's Dynamo....................................................................................... 22Basics......................................................................................................... 22Virtual Nodes ........................................................................................... 22Master-less Cluster ................................................................................... 23Quorum-based Replication ..................................................................... 24Read Repair and Hinted Handoff ............................................................ 24Conflict Resolution using Vector Clocks ................................................ 24Conclusion ............................................................................................... 26

What is Riak?................................................................................................ 27Riak: Dynamo, And Then Some ................................................................. 27Installation .................................................................................................... 28

Installing Riak using Binary Packages ..................................................... 28Talking to Riak............................................................................................. 29

Buckets ..................................................................................................... 29Fetching Objects ...................................................................................... 29Creating Objects ...................................................................................... 30

Page 4: This document was created with Prince, a great way of getting … · 2015. 12. 12. · Anatomy of a Riak Node..... 104 What Happens When a Node Joins a Cluster ..... 105 Leaving a

Object Metadata ....................................................................................... 31Custom Metadata ..................................................................................... 32Linking Objects........................................................................................ 33Walking Links.......................................................................................... 34Walking Nested Links ............................................................................. 35The Anatomy of a Bucket ........................................................................ 36List All Of The Keys................................................................................. 37

How Do I Delete All Keys in a Bucket?............................................... 38How Do I Get the Number of All Keys in a Bucket? .......................... 39

Querying Data ............................................................................................. 39MapReduce............................................................................................... 40MapReduce Basics .................................................................................... 41Mapping Tweet Attributes ...................................................................... 41Using Reduce to Count Tweets .............................................................. 42Re-reducing for Great Good ................................................................... 43Counting all Tweets................................................................................. 44Chaining Reduce Phases .......................................................................... 44Parameterizing MapReduce Queries....................................................... 46Chaining Map Phases ............................................................................... 48MapReduce in a Riak Cluster................................................................... 48Efficiency of Buckets as Inputs................................................................. 50Key Filters................................................................................................. 51Using Riak's Built-in MapReduce Functions.......................................... 53Intermission: Riak's Configuration Files ................................................. 54Errors Running JavaScript MapReduce................................................... 55Deploying Custom JavaScript Functions ................................................ 56Using Erlang for MapReduce .................................................................. 57

Writing Custom Erlang MapReduce Functions ................................. 58On Full-Bucket MapReduce and Key-Filters Performance ................... 61

Querying Data, For Real.............................................................................. 61Riak Search ............................................................................................... 62

Enabling Riak Search ........................................................................... 62Indexing Data ....................................................................................... 62

Indexing from the Command-Line................................................. 63The Anatomy of a Riak Search Document.......................................... 63Querying from the Command-Line ................................................... 64

Other Command-Line Features ...................................................... 64The Riak Search Document Schema ....................................................... 64

Analyzers .............................................................................................. 65Writing Custom Analyzers .................................................................. 66

Page 5: This document was created with Prince, a great way of getting … · 2015. 12. 12. · Anatomy of a Riak Node..... 104 What Happens When a Node Joins a Cluster ..... 105 Leaving a

Other Schema Options..................................................................... 69An Example Schema............................................................................. 70Setting the Schema ............................................................................... 72

Indexing Data from Riak ......................................................................... 72Using the Solr Interface............................................................................ 74

Paginating Search Results .................................................................... 75Sorting Search Results .......................................................................... 76Search Operators .................................................................................. 76Summary of Solr API Search Options.................................................. 79Summary of the Solr Query Operators ................................................ 80Indexing Documents using the Solr API ............................................. 81Deleting Documents using the Solr API ............................................. 82Using Riak's MapReduce with Riak Search ........................................ 83The Overhead of Indexing................................................................... 83

Riak Secondary Indexes ........................................................................... 84Indexing Data with 2i........................................................................... 84Querying Data with 2i ......................................................................... 86Using Riak 2i with MapReduce........................................................... 87Storing Multiple Index Values ............................................................. 87Managing Object Associations: Links vs. 2i ........................................ 88How Does Riak 2i Compare to Riak Search? ...................................... 89Riak Search vs. Riak 2i vs. MapReduce................................................ 90

How Do I Index Data Already in Riak?................................................... 91Using Pre- and Post-Commit Hooks...................................................... 92

Validating Data..................................................................................... 92Enabling Pre-Commit Hooks ............................................................. 93Pre-Commit Hooks in Erlang ............................................................. 94Modifying Data in Pre-Commit Hooks.............................................. 95Accessing Riak Objects in Commit Hooks ......................................... 97Enabling Post-Commit Hooks .......................................................... 100Deploying Custom Erlang Functions................................................ 100Updating External Sources in Post-Commit Hooks......................... 102

Riak in its Setting........................................................................................ 102Building a Cluster................................................................................... 102

Adding a Node to a Riak Cluster ....................................................... 103Configuring a Riak Node .............................................................. 103Joining a Cluster ............................................................................. 104

Anatomy of a Riak Node.................................................................... 104What Happens When a Node Joins a Cluster ................................... 105Leaving a Cluster ................................................................................ 105

Page 6: This document was created with Prince, a great way of getting … · 2015. 12. 12. · Anatomy of a Riak Node..... 104 What Happens When a Node Joins a Cluster ..... 105 Leaving a

Eventually Consistent Riak .................................................................... 106Handling Consistency........................................................................ 106

Writing with a Non-Default Quorum.......................................... 106Durable Writes ............................................................................... 107Primary Writes ............................................................................... 108Tuning Default-Replication and Quorum Per Bucket................. 108Choosing the Right N Value ......................................................... 110Reading with a Non-Default Quorum.......................................... 110Read-Repair.................................................................................... 111

Modeling Data for Eventual Consistency ................................................. 111Choosing the Right Data Structures ...................................................... 112

Conflicts in Riak................................................................................. 115Siblings............................................................................................ 116

Reconciling Conflicts......................................................................... 117Modeling Counters and Other Data Structures ................................ 118

Problems with Timestamps for Conflict Resolution ..................... 119Strategies for Reconciling Conflicts .................................................. 123

Reads Before Writes ....................................................................... 124Merging Strategies ......................................................................... 124

Sibling Explosion................................................................................ 124Building a Timeline with Riak .......................................................... 125

Multi-User Timelines..................................................................... 128Avoiding Infinite Growth.................................................................. 129Intermission: How to Fetch Multiple Objects in one Request.......... 129Intermission: Paginating Using MapReduce .................................... 130

Handling Failure .................................................................................... 131Operating Riak....................................................................................... 132

Choosing a Ring Size ......................................................................... 132Protocol Buffers vs. HTTP ................................................................ 133Storage Backends................................................................................ 133

Innostore......................................................................................... 134Bitcask............................................................................................. 134LevelDB.......................................................................................... 135

Load-Balancing Riak ......................................................................... 136Placing Riak Nodes across a Network ............................................... 138Monitoring Riak................................................................................. 140

Request Times ................................................................................ 141Number of Requests ....................................................................... 142Read Repairs, Object Size, Siblings................................................ 143Monitoring 2i ................................................................................. 144

Page 7: This document was created with Prince, a great way of getting … · 2015. 12. 12. · Anatomy of a Riak Node..... 104 What Happens When a Node Joins a Cluster ..... 105 Leaving a

Miscellany ....................................................................................... 144Monitoring Reference.................................................................... 144

Managing a Riak Cluster with Riak Control..................................... 147Enabling Riak Control ................................................................... 147Intermission: Generating an SSL Certificate ................................. 148Riak Control Cluster Overview..................................................... 149Managing Nodes with Riak Control ............................................. 150Managing the Ring with Riak Control ......................................... 151To Be Continued............................................................................ 152

When To Riak? .......................................................................................... 152Riak Use Cases in Detail......................................................................... 153

Using Riak for File Storage ................................................................ 153File Storage Access Patterns ........................................................... 154Object Size...................................................................................... 154Storing Large Files in Riak ............................................................. 155Riak Cloud Storage ........................................................................ 155

Using Riak to Store Logs.................................................................... 156Modeling Log Records................................................................... 157Logging Access Patterns ................................................................ 157Indexing Log Data for Efficient Access ......................................... 158Secondary Index Ranges as Key Filter Replacement..................... 159Searching Logs ............................................................................... 160Riak for Log Storage in the Wild ................................................... 161Deleting Historical Data ................................................................ 161What about Analytics? ................................................................... 162

Session Storage ................................................................................... 162Modeling Session Data ................................................................... 163Session Storage Access Patterns...................................................... 164Bringing Session Data Closer to Users .......................................... 164

URL Shortener ................................................................................... 164URL Shortening Access Patterns ................................................... 165Modeling Data................................................................................ 165Riak URL Shortening in the Wild ................................................. 165

Where to go from here............................................................................... 165