CAP理论指出:一个分布式系统不可能同时满足一致性(Consistency)、可用性(Availibility)和分区容忍性(Partition Tolerance)这三个需求,最多只能同时满足其中的两个。在Riak强调的是Availibility和Partition Tolerance,它的Consistency是指eventually consistent,在一些concurrency的场合,你会发现你的data model会有inconsistent的情况出现,可以使用statebox去解决这个问题,下面用statebox去实现博客互粉的功能:
statebox_riak的github地址:https://github.com/mochi/statebox_riak
{ok, Pid} = riakc_pb_socket:start_link("127.0.0.1", 8087),
S = statebox_riak:new([{riakc_pb_socket, Pid}, {max_queue, 20}]),
statebox_riak:apply_bucket_ops(?BUCKET, [{[list_to_binary(FromId)], statebox_orddict:f_union(following, [list_to_binary(ToId)])},{[list_to_binary(ToId)], statebox_orddict:f_union(followers, [list_to_binary(FromId)])} ], S)
S = statebox_riak:new([{riakc_pb_socket, Pid}, {max_queue, 20}]),
statebox_riak:apply_bucket_ops(?BUCKET, [{[list_to_binary(FromId)], statebox_orddict:f_union(following, [list_to_binary(ToId)])},{[list_to_binary(ToId)], statebox_orddict:f_union(followers, [list_to_binary(FromId)])} ], S)
转载于:https://blog.51cto.com/erlang/1004918