神刀安全网

rqlite v3: Globally replicating SQLite

rqlite is a distributed relational database, which uses SQLite as its storage engine. rqlite is written in Go and uses Raft to achieve consensus across a set of SQLite databases.

With the v3 release series , rqlite can now replicate SQLite databases on a global scale. Let’s see it in action using the AWS EC2 cloud .

Let’s build our global infrastructure

The EC2 system makes it simple to fire up a global presence, so I did just that. I launched 3 m4.xlarge instances, running Ubuntu 14.04 LTS — one in the USA (Oregon), one in Europe (Dublin), and, finally, one in Australia (Sydney).

I ended up with the following hosts:

AWS Region Public Hostname Public IP Private IP
USA (Oregon) ec2-52-36-0-40.us-west-2.compute.amazonaws.com 52.36.0.40 172.31.40.213
Europe (Dublin) None assigned 52.51.50.174 172.31.40.213
Australia (Sydney) ec2-52-64-65-8.ap-southeast-2.compute.amazonaws.com 52.36.0.40 172.31.6.75

For the purposes of this experiment, I made the Security Groups wide open, so there would be no problems with network access. Of course, you wouldn’t do this in a production environment.

Check the network

To get an idea of network latency, I ran some ping tests.

From Oregon to Dublin

$ ping 52.51.50.174 PING 52.51.50.174 (52.51.50.174) 56(84) bytesofdata. 64 bytesfrom 52.51.50.174: icmp_seq=1 ttl=48 time=135 ms 

From Oregon to Sydney

$ ping 52.64.65.8 PING 52.64.65.8 (52.64.65.8) 56(84) bytesofdata. 64 bytesfrom 52.64.65.8: icmp_seq=1 ttl=51 time=161 ms 

From Sydney to Dublin

$ ping 52.51.50.174 PING 52.51.50.174 (52.51.50.174) 56(84) bytesofdata. 64 bytesfrom 52.51.50.174: icmp_seq=1 ttl=47 time=305 ms 

rqlite v3: Globally replicating SQLite
Ping times between rqlite nodes

Install rqlite

Next, after ssh’ing in to each node, let’s install rqlite v3.1.0 on each node like so:

$ bash < <(curlhttps://raw.githubusercontent.com/otoolep/global-rqlite/master/install.sh) 

The source of this script is available here .

Start the leader

Let’s choose to fire up the leader in Oregon, using the EC2 metadata system to retrieve the IP addresses. So I ran the following commands on the EC2 instance in Oregon.

$ PUBLIC_IP=`curl -s http://169.254.169.254/latest/meta-$ data/public-ipv4` $ LOCAL_IP=`curl -s http://169.254.169.254/latest/meta-data/local-ipv4` $ ./rqlited -http $LOCAL_IP:4001 -httpadv $PUBLIC_IP:4001 -raft $LOCAL_IP:4002 -raftadv $PUBLIC_IP:4002 $HOME/node 

We now have a single rqlite node, with a real SQLite database underneath it. v3.1.0 includes the important options httpadv and raftadv , which allows the node to advertise an IP address other than the address it is bound to — in this case it advertises the public IP address of the EC2 instance, allowing other nodes to contact it from outside the AWS US Oregon region.

This node will accept commands, so let’s create a table and insert a record, using the rqlite CLI .

$ ./rqlite -H ec2-52-36-0-40.us-west-2.compute.amazonaws.com ec2-52-36-0-40.us-west-2.compute.amazonaws.com:4001> CREATETABLEfoo (idINTEGER NOT NULL PRIMARYKEY, nameTEXT) 0 rowaffected (0.000336 sec) ec2-52-36-0-40.us-west-2.compute.amazonaws.com:4001> INSERTINTOfoo(name) VALUES("fiona") 1 rowaffected (0.000074 sec) ec2-52-36-0-40.us-west-2.compute.amazonaws.com:4001> SELECT * FROMfoo +----+-------+ | id | name  | +----+-------+ | 1  | fiona | +----+-------+ 

The rqlite CLI provides a very similar experience to the SQLite CLI. But for full functionality (such as bulk inserts and transaction control), use the HTTP API directly.

Create a cluster

There isn’t much point to a single-node cluster, so let’s add two more nodes. To do that I run the next commands on each of the two remaining EC2 instances:

$ PUBLIC_IP=`curl -s http://169.254.169.254/latest/meta-data/public-ipv4` $ LOCAL_IP=`curl -s http://169.254.169.254/latest/meta-data/local-ipv4` $ ./rqlited -http $LOCAL_IP:4001 -httpadv $PUBLIC_IP:4001 -raft $LOCAL_IP:4002 -raftadv $PUBLIC_IP:4002 -joinhttp://ec2-52-36-0-40.us-west-2.compute.amazonaws.com:4001 $HOME/node 

Global replication!

With these simple steps, we’ve now got a globally replicated relational database. The new table and row have been replicated to the two new nodes. And, of course, every future change made on the leader database will be replicated synchronously to the other 2 nodes, placed out of harms way, thousands of miles from the leader.

Fault tolerance

This 3-node cluster can tolerate the failure of a node, and if that node is the leader, a new leader will be elected within a couple of seconds. If the cluster was 5 nodes in size, it could tolerate the failure of two nodes.

rqlite exposes a HTTP API, which the CLI uses. This API will also redirect clients to the leader node, if the node contacted is not the leader.

Practical?

Is rqlite practical? Probably not on a cluster with such high latency between the nodes. In this set up I managed to insert about 2 rows a second, though if transactions and bulk updates are used, the effective rate is much higher.

Definitely

However, within, say, a single EC2 Region, the performance will be much greater. And by running an rqlite cluster within the same data-center, with each node on a different rack for reliability, one should see 100s of insertions a second. And all with very easy deployment and operation.

Fun?

Absolutely.  rqlite is a serious program, and it shows the remarkable power available to individual developers today. rqlite builds on a world-class database , a proven distributed consensus implementation , and a worldwide public cloud, to provide a globally replicated relational database, with SQLite at its core.

See for yourself

The code for rqlite is open-source and is available on GitHub , as are pre-built releases .

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » rqlite v3: Globally replicating SQLite

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址