Custom load balancing for RStudio Workbench / RStudio Server Pro

Follow

RStudio Workbench (previously RStudio Server Pro) gives you the ability to load balance by session, by user hash, or by server load. But it also gives you the ability to add your own custom load balancing script to make load balancing decisions. Specifics can be found in the RStudio Workbench admin guide.

First, configure RStudio Workbench to accept custom load balancing in /etc/rstudio/load-balancer(Note: As of the 2021.09.0 "Ghost Orchid" release, the [config] header is optional):

[config]
balancer = custom

Second, create a new bash script at:

/usr/lib/rstudio-server/bin/rserver-balancer

This script will be passed two environment variables: RSTUDIO_USERNAME — The user on behalf or which the new R session is being created. RSTUDIO_NODES — Comma separated list of the IP address and port of available nodes. The script should return the node to start the new session on using its standard output. Note that the format of the returned node should be identical to its format as passed to the script (i.e., include the IP address and port).

The example below load balances by randomly selecting an RStudio node for each new session: 

#!/usr/bin/env bash

# Assuming input is comma-separated, no spaces
# RSTUDIO_NODES="10.1.1.10:8787,10.1.1.11:8787,10.1.1.12:8787"

# reads node list into an array
IFS=',' read -r -a RSTUDIO_NODES_ARRAY <<< "$RSTUDIO_NODES"

# selects random node from array and returns it to stdout
echo "${RSTUDIO_NODES_ARRAY[$RANDOM % ${#RSTUDIO_NODES_ARRAY[@]}]}"

Comments