vADC Docs

Feature Brief: TrafficScript

by on ‎02-28-2013 02:42 AM (4,743 Views)

TrafficScript is Stingray's scripting and configuration language that lets you specify precisely how Stingray must handle each type of request, in as much detail as you need.

Without TrafficScript, you would have to configure your load balancer with a single, ‘lowest-common-denominator’ policy that describes how to handle all your network traffic.  With TrafficScript, you control how Stingray handles your traffic, inspecting and modifying each request and response as you wish, and pulling in each of Stingray’s features as you require.

What is TrafficScript?

TrafficScript is a high-level programming language used to create ‘rules’ which are invoked by the traffic manager each time a transaction request or response is received:


TrafficScript rules have full access to all request and response data, and give the you full control over how end users interact with the load balanced services.  They are commonly used to selectively enable particular Traffic Manager features (for example, bandwidth control, caching or security policies) and to modify request and response data to handle error cases or augment web page data.

Although TrafficScript is a new language, the syntax is intentionally familiar.  It is deeply integrated with the traffic management kernel for two reasons:

  • Performance: the integration allows for very efficient, high-performance interaction with the internal state of the traffic manager
  • Abstraction: TrafficScript presents a very easy-to-use request/response event model that abstracts the internal complexities of managing network traffic away from the developer.

You can use TrafficScript to create a wide range of solutions and the familiar syntax means that complex code can be prototyped and deployed rapidly.

Example 1 - Modifying Requests

# Is this request a video download?

$url = http.getPath();

if( string.wildmatch( $url, "/videos/*.flv" ) ) {

  # Rewrite the request to target an f4v container, not flv

  $url = string.replace( $url, ".flv", ".f4v" );

  http.setPath( $url );

  # We encode flash videos at 1088 Kbits max.  Apply a limit to 2 Gbit

  # to control download tools and other greedy clients

  response.setBandwidthClass( "Videos 2Mbits" );

  # We don't want to cache the response in the Stingray cache, even if

  # the HTTP headers state that it is cacheable



A simple request rule that modifies the request and instructs the traffic manager to apply bandwidth and cache customizations

TrafficScript's close integration with the traffic management kernel makes it as easy to rewrite HTTP responses as HTTP requests:

Example 2 - Modifying Responses

$url = http.getResponseHeader( "Content-Type" );

if( !string.startsWith( $url, "text/html" ) ) break;

$response = http.getResponseBody();

$response = string.replaceAll( $response,

    "", "" );

http.setResponseBody( $response );

A response rule that makes a simple replacement to change links embedded in HTTP responses

TrafficScript can invoke external systems in a synchronous or asynchronous fashion:

  • TrafficScript functions like http.request.get(), auth.query() and net.dns.resolveIP() will query an external HTTP, LDAP or DNS server and return the result of the query.  They operate synchronously (the rule is ‘blocked’ while the query is running) but the Traffic Manager will process other network traffic while the current rule is temporarily suspended.
  • The TrafficScript function event.emit() raises an event to Stingray’s Event Handling system.  The TrafficScript rule continues to execute and the event is processed asynchronously. Events can trigger a variety of actions, ranging from syslog or email alerts to complex user-provided scripts.

These capabilities allow the Traffic Manager to interface with external systems to retrieve data, verify credentials or initiate external control-plane actions.

Example 3 - Accessing an external source (Google News)

$type = http.getResponseHeader( "Content-Type" );

if( $type != "text/html" ) break;  # Stop processing this rule

$res = http.request.get(


  "v=1.0&q=Riverbed" );

$r = json.deserialize( $res );

$rs = $r['responseData']['results'];

$html .= "<ul>\n";

foreach( $e in $rs ) {

  $html .= '<li>' .

'<a href="'.$e['unescapedUrl'].'">'.$e['titleNoFormatting'].'</a>'.



$html .= "</ul>\n";

$body = http.getResponseBody();

$body = string.replace( $body, "<!--RESULTS-->", $html);

http.setResponseBody( $body );

An advanced response rule that queries an external datasource and inserts additional data into the web page response

TrafficScript rules may also invoke Java Extensions.  Extensions may be written in any language that targets the JVM, such as Python or Ruby as well as Java. They allow developers to use third-party code libraries and to write sophisticated rules that maintain long-term state or perform complex calculations.

Getting started with RuleBuilder

The full TrafficScript language gives you access to over 200 functions, with the support of a proper programming language - variables, tests, loops and other flow control. You can write TrafficScript rules much as you'd write Perl scripts (or Python, JavaScript, Ruby, etc).

The RuleBuilder gives you a UI that lets you configure tests, and actions which are executed if one-of or all-of the tests are satisfied. The tests and actions you can use are predefined, and cover a subset of the full functions of TrafficScript. You can use the RuleBuilder much as you'd use the filtering rules in your email client.


RuleBuilder provides a simple way to create basic policies to control Traffic Manager

If you're not familiar with programming languages, then RuleBuilder is a great way to get started.  You can create simple policies to control Stingray's operation, and then, with one click, transform them into the equivalent TrafficScript rule so that you can learn the syntax and extend them as required.  There's a good example to start with in the Stop hot-linking and bandwidth theft! article.


Read More