For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Sign inTry it free
DocsGuidesSDKsIntegrationsAPI docsTutorialsFlagship blog
DocsGuidesSDKsIntegrationsAPI docsTutorialsFlagship blog
  • Guides
    • Cheatsheets
    • Feature flags
    • AgentControl
    • Experimentation
    • Statistical methodology
    • Metrics
    • Infrastructure
    • Account management
    • Teams and custom roles
    • SDKs
      • Unit testing with Jest
      • Use cases for SDK wrappers
      • Using LaunchDarkly without a supported SDK
      • Using mobile SDKs
      • Using the Lua SDK with HAProxy
      • Using the Lua SDK with NGINX
      • Using the JavaScript SDK in Salesforce Lightning Web Components
      • Resilient architecture patterns for LaunchDarkly's SDKs
    • Integrations
    • REST API
    • Additional resources
Sign inTry it free
LogoLogo
On this page
  • Prerequisites
  • Preparing the C++ server-side SDK
  • Ensuring correct initialization
GuidesSDKs

Using the Lua SDK with HAProxy

Was this page helpful?
Previous

Using the Lua SDK with NGINX

Next
Built with

This guide explains how to use our Lua server-side SDK with HAProxy. You can use HAProxy with LaunchDarkly to implement dynamic rate limiting, access controls, rollout, and many other features at the edge of your application architecture.

You can extend HAProxy with Lua 5.3, enabling complex control of HAProxy functionality. HAProxy has substantial commercial adoption.

Find a basic Dockerized app in the GitHub repository at hello-haproxy.

Prerequisites

To complete this guide, you must have the following prerequisites:

  • Basic working knowledge of the LaunchDarkly Lua server-side SDK
  • Basic working knowledge of the LaunchDarkly C++ server-side SDK

The C++ server-side SDK is required because the Lua server-side SDK is implemented as a wrapper around the C++ server-side SDK.

Preparing the C++ server-side SDK

Make the binary of the C++ server-side SDK accessible to the dynamic linker. The most convenient way to do this is to install the binary system-wide at /usr/lib/libldserverapi.so.

Ensuring correct initialization

The most important part of effective SDK usage is managing the lifetime of clients correctly. When HAProxy utilizes process-based concurrency, multiple clients initiate. If you accidentally initiate a client per request, the application is substantially slower because the SDK has to download a fresh ruleset from LaunchDarkly.

For ideal operations, initialize each HAProxy worker process exactly once. You can do this with the lua-load directive under global. This directive executes a script when a process is freshly spawned. Client initialization should reside in this script. In the example below, this file is called shared.lua.

Here is an example of initialization logic:

1local ld = require("launchdarkly_server_sdk")
2local os = require("os")
3
4return ld.clientInit(os.getenv(sdk_key), 1000, config)

Later we can use the result of this initialization process in other services:

1core.register_service("launchdarkly", "http", function(applet)
2 applet:start_response()
3
4 local context = ld.makeContext({
5 user = {
6 key = "abc"
7 }
8 })
9
10 if client:boolVariation(context, os.getenv(flag_key), false) then
11 applet:send("<p>feature launched</p>")
12 else
13 applet:send("<p>feature not launched</p>")
14 end
15end)