AboutBlogContact
Distributed SystemsJune 28, 2010 2 min read 32

Riak: Dynamo in Practice with Riak Core (2010)

AunimedaAunimeda
📋 Table of Contents

Riak: Dynamo in Practice with Riak Core

Riak isn't just a database; it's a masterpiece of Erlang engineering. It implements the Dynamo architecture, which focuses on high availability and partition tolerance (AP in the CAP theorem).

The Hash Ring

Riak uses a consistent hashing ring divided into 64 (or more) partitions. These partitions are managed by "vnodes" (virtual nodes).

%% Riak Core vnode initialization
init([Partition]) ->
    {ok, #state{partition=Partition}}.

handle_command({put, Key, Value}, _Sender, State) ->
    %% Store data locally for this partition
    Storage = do_store(Key, Value),
    {reply, ok, State#state{storage=Storage}}.

Quorum and N, R, W

The secret to Riak's flexibility is the N, R, W parameters:

  • N: Number of replicas (default 3).
  • R: Number of nodes that must respond to a read.
  • W: Number of nodes that must respond to a write.

If you set W=1, your writes are lightning fast but potentially less durable. If you set W=3, you have high durability but a single node failure can block writes.

Vector Clocks

Because Riak is eventually consistent, two clients might update the same key on different nodes during a network partition. Riak uses Vector Clocks to track the causal history of an object.

Object A, vclock: {node1, 1}, {node2, 2}

If the vclocks are siblings (neither is a descendant of the other), Riak creates a "conflict" (sibling) and lets the application resolve it. This is why Riak is perfect for shopping carts—you never want to lose a customer's item just because of a network glitch.

Read Also

Fault-Tolerant Systems with Erlang/OTP Supervision Trees (2007)aunimeda
Distributed Systems

Fault-Tolerant Systems with Erlang/OTP Supervision Trees (2007)

Designing 'Nine Nines' availability. How Erlang 5.5 (R11B) uses the Let It Crash philosophy and OTP supervision hierarchies to build distributed systems that never die.

Distributed Locking: etcd vs. Consul (2016)aunimeda
Distributed Systems

Distributed Locking: etcd vs. Consul (2016)

Don't let two cron jobs run at once. In 2016, we're comparing the Raft implementations of etcd and Consul for reliable distributed locking.

Thrift vs. Protocol Buffers: Choosing Your Binary Protocol (2007)aunimeda
Distributed Systems

Thrift vs. Protocol Buffers: Choosing Your Binary Protocol (2007)

In 2007, high-throughput RPC is all about binary. Facebook just open-sourced Thrift, and Google's Protobuf is the industry's open secret. Which one should you choose?

Need IT development for your business?

We build websites, mobile apps and AI solutions. Free consultation.

Get Consultation All articles