Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Working TCP load balancer synchronized with Redis to track changes to
backend servers.
  • Loading branch information
NicolasLM committed Oct 18, 2015
0 parents commit 2876ff5
Show file tree
Hide file tree
Showing 10 changed files with 1,031 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
target
124 changes: 124 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Cargo.toml
@@ -0,0 +1,10 @@
[package]
name = "nucleon"
version = "0.0.1"
authors = ["Nicolas Le Manchet <nicolas@lemanchet.fr>"]

[dependencies]
log = "0.3.1"
argparse = "0.2.1"
mio = "0.4.3"
redis = "0.5.1"
23 changes: 23 additions & 0 deletions LICENSE
@@ -0,0 +1,23 @@
Copyright (c) 2015, Nicolas Le Manchet
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
86 changes: 86 additions & 0 deletions README.md
@@ -0,0 +1,86 @@
Nucleon
=======

Nucleon is a dynamic TCP load balancer written in Rust. It has the ability to
insert and remove backend servers on the flight. To do that it leverages [Redis
Pub/Sub](http://redis.io/topics/pubsub) mechanism. Adding or removing a server
to a cluster is as easy as publishing a message to Redis.

How to build it
---------------

All you need to build it is [Rust
1.3](https://doc.rust-lang.org/stable/book/installing-rust.html).

Just go in the repository and issue:

$ cargo build --release

Usage
-----

Nucleon can be used with or without a Redis database. When ran without Redis it
is not possible to add or remove load balanced servers without restarting the
process.

```
Usage:
nucleon [OPTIONS] [SERVER ...]
Dynamic TCP load balancer
positional arguments:
server Servers to load balance
optional arguments:
-h,--help show this help message and exit
-b,--bind BIND Bind the load balancer to address:port (127.0.0.1:8000)
-r,--redis REDIS URL of Redis database (redis://localhost)
--no-redis Disable updates of backend through Redis
-l,--log LOG Log level [debug, info, warn, error] (info)
```

Imagine you have two web servers to load balance, and a local Redis. Run the
load balancer with:

nucleon --bind 0.0.0.0:8000 10.0.0.1:80 10.0.0.2:80

Now imagine that you want to scale up your infrastructure by spawning a new web
server at 10.0.0.3. Just send a message to the Redis channel `backend_add`:

redis:6379> PUBLISH backend_add 10.0.0.3:80
(integer) 1

Your will see in logs:

INFO - Load balancing server V4(10.0.0.1:80)
INFO - Load balancing server V4(10.0.0.2:80)
INFO - Now listening on 0.0.0.0:8000
INFO - Subscribed to Redis channels 'backend_add' and 'backend_remove'
INFO - Added new server 10.0.0.3:80

If you decide that you do not need server 2 any longer:

redis:6379> PUBLISH backend_remove 10.0.0.2:80
(integer) 1

How does it perform?
--------------------

Surprisingly well. A quick comparison with HA Proxy in TCP mode with a single
backend containing a single server using iperf results in:

| Connections | HA Proxy | Nucleon |
| -----------:| ------------:| -------------:|
| 1 | 15.1 Gbits/s | 15.7 Gbits/s |
| 10 | 13.5 Gbits/s | 11.3 Gbits/s |
| 100 | 8.9 Gbits/s | 10.5 Gbits/s |

Keep in mind that this is a really simple test, far from what real life traffic
looks like. A real benchmark should compare short lived connections with long
running one, etc.

Licence
-------

MIT
105 changes: 105 additions & 0 deletions src/backend.rs
@@ -0,0 +1,105 @@
use std::net::{SocketAddr, AddrParseError};
use std::str::FromStr;

pub trait GetBackend {
fn get(&mut self) -> Option<SocketAddr>;
fn add(&mut self, backend_str: &str) -> Result<(), AddrParseError>;
fn remove(&mut self, backend_str: &str) -> Result<(), AddrParseError>;
}

pub struct RoundRobinBackend {
backends: Vec<SocketAddr>,
last_used: usize
}

impl RoundRobinBackend {
pub fn new(backends_str: Vec<String>) -> Result<RoundRobinBackend, AddrParseError> {
let mut backends = Vec::new();
for backend_str in backends_str {
let backend_socket_addr: SocketAddr = try!(FromStr::from_str(&backend_str));
backends.push(backend_socket_addr);
info!("Load balancing server {:?}", backend_socket_addr);
}
Ok(RoundRobinBackend {
backends: backends,
last_used: 0
})
}
}

impl GetBackend for RoundRobinBackend {
fn get(&mut self) -> Option<SocketAddr> {
if self.backends.is_empty() {
return None;
}
self.last_used = (self.last_used + 1) % self.backends.len();
self.backends.get(self.last_used).map(|b| b.clone())
}

fn add(&mut self, backend_str: &str) -> Result<(), AddrParseError> {
let backend_socket_addr: SocketAddr = try!(FromStr::from_str(&backend_str));
self.backends.push(backend_socket_addr);
Ok(())
}

fn remove(&mut self, backend_str: &str) -> Result<(), AddrParseError> {
let backend_socket_addr: SocketAddr = try!(FromStr::from_str(&backend_str));
self.backends.retain(|&x| x != backend_socket_addr);
Ok(())
}
}


#[cfg(test)]
mod tests {
use std::net::{SocketAddr, AddrParseError};
use super::{RoundRobinBackend, GetBackend};

#[test]
fn test_rrb_backend() {
let backends_str = vec!["127.0.0.1:6000".to_string(),
"127.0.0.1:6001".to_string()];
let mut rrb = RoundRobinBackend::new(backends_str).unwrap();
assert_eq!(2, rrb.backends.len());

let first_socket_addr = rrb.get().unwrap();
let second_socket_addr = rrb.get().unwrap();
let third_socket_addr = rrb.get().unwrap();
let fourth_socket_addr = rrb.get().unwrap();
assert_eq!(first_socket_addr, third_socket_addr);
assert_eq!(second_socket_addr, fourth_socket_addr);
assert!(first_socket_addr != second_socket_addr);
}

#[test]
fn test_empty_rrb_backend() {
let backends_str = vec![];
let mut rrb = RoundRobinBackend::new(backends_str).unwrap();
assert_eq!(0, rrb.backends.len());
assert!(rrb.get().is_none());
}

#[test]
fn test_add_to_rrb_backend() {
let mut rrb = RoundRobinBackend::new(vec![]).unwrap();
assert!(rrb.get().is_none());
assert!(rrb.add("327.0.0.1:6000").is_err());
assert!(rrb.get().is_none());
assert!(rrb.add("127.0.0.1:6000").is_ok());
assert!(rrb.get().is_some());
}

#[test]
fn test_remove_from_rrb_backend() {
let backends_str = vec!["127.0.0.1:6000".to_string(),
"127.0.0.1:6001".to_string()];
let mut rrb = RoundRobinBackend::new(backends_str).unwrap();
assert!(rrb.remove("327.0.0.1:6000").is_err());
assert_eq!(2, rrb.backends.len());
assert!(rrb.remove("127.0.0.1:6000").is_ok());
assert_eq!(1, rrb.backends.len());
assert!(rrb.remove("127.0.0.1:6000").is_ok());
assert_eq!(1, rrb.backends.len());
}

}

0 comments on commit 2876ff5

Please sign in to comment.