All Products
Search
Document Center

Server Load Balancer:Dictionary functions

Last Updated:Apr 08, 2026

ALB forwarding rules support a dictionary data structure for storing and manipulating key-value pairs at request processing time. Use dictionary functions to build dynamic routing logic, pass state between operations, or process structured data such as parsed M3U8 playlists — without a chain of if/else comparisons.

set

Item Description
Syntax set(d, k, v)
Description Sets a key-value pair in dictionary d.
Parameters d: the dictionary. k: the key (any type). v: the value (any type).
Return value true

Example 1: Initialize two dictionaries — one with numeric indexes, one with string keys — and iterate over the string-key dictionary using foreach.

outer_keys=['e66fd4aa-f281-472f-b919-fc7e7474de25', '66fee78d-1887-42ec-9119-a9b50b7fbca2']
say(concat('keys[1]=', get(outer_keys, 1)))
say(concat('keys[2]=', get(outer_keys, 2)))

inner_keys=[]
set(inner_keys, 'dev', '243390eb-00b7-4551-a6b8-021bb34d1674')
set(inner_keys, 'zeus', '4747d33b-12b0-45e6-ac10-a8e191d6adaa')

def echo_each(k, v, u) {
    s = concat('keys[', k, ']=', v)
    say(s)
}
foreach(inner_keys, echo_each, [])

Output:

keys[1]=e66fd4aa-f281-472f-b919-fc7e7474de25
keys[2]=66fee78d-1887-42ec-9119-a9b50b7fbca2
keys[dev]=243390eb-00b7-4551-a6b8-021bb34d1674
keys[zeus]=4747d33b-12b0-45e6-ac10-a8e191d6adaa

Example 2: Store a nested dictionary (d_inner inside d_outer) and read back a value from the inner dictionary.

d_inner = []
set(d_inner, 'name', 'inner ascript')

d_outer = []
set(d_outer, 'dictA', d_inner)

v = get(d_outer, 'dictA')
if v {
    v = get(v, 'name')
    if v {
        add_rsp_header('X-DSL-NESTED-DICT', v)
    }
}

Output:

X-DSL-NESTED-DICT: inner ascript

get

Item Description
Syntax get(d, k)
Description Retrieves the value of a key from dictionary d.
Parameters d: the dictionary. k: the key (any type).
Return value The value of the key if it exists; false if the key is not found.

Example 1: Access values from both a numeric-indexed and a string-keyed dictionary.

outer_keys=['e66fd4aa-f281-472f-b919-fc7e7474de25', '66fee78d-1887-42ec-9119-a9b50b7fbca2']
say(concat('keys[1]=', get(outer_keys, 1)))
say(concat('keys[2]=', get(outer_keys, 2)))

inner_keys=[]
set(inner_keys, 'dev', '243390eb-00b7-4551-a6b8-021bb34d1674')
set(inner_keys, 'zeus', '4747d33b-12b0-45e6-ac10-a8e191d6adaa')

def echo_each(k, v, u) {
    s = concat('keys[', k, ']=', v)
    say(s)
}
foreach(inner_keys, echo_each, [])

Output:

keys[1]=e66fd4aa-f281-472f-b919-fc7e7474de25
keys[2]=66fee78d-1887-42ec-9119-a9b50b7fbca2
keys[dev]=243390eb-00b7-4551-a6b8-021bb34d1674
keys[zeus]=4747d33b-12b0-45e6-ac10-a8e191d6adaa

Example 2: Read a value from a nested dictionary, using if v to guard against a missing key.

d_inner = []
set(d_inner, 'name', 'inner ascript')

d_outer = []
set(d_outer, 'dictA', d_inner)

v = get(d_outer, 'dictA')
if v {
    v = get(v, 'name')
    if v {
        add_rsp_header('X-DSL-NESTED-DICT', v)
    }
}

Output:

X-DSL-NESTED-DICT: inner ascript

foreach

Item Description
Syntax foreach(d, f, user_data)
Description Traverses each element in dictionary d and calls callback f for each element. The loop ends early if f returns false.
Parameters d: the dictionary. f: the callback function, with the signature f(key, value, user_data). user_data: a dictionary passed to each callback invocation.
Return value true

Example 1: Iterate over a string-keyed dictionary and print each key-value pair.

outer_keys=['e66fd4aa-f281-472f-b919-fc7e7474de25', '66fee78d-1887-42ec-9119-a9b50b7fbca2']
say(concat('keys[1]=', get(outer_keys, 1)))
say(concat('keys[2]=', get(outer_keys, 2)))

inner_keys=[]
set(inner_keys, 'dev', '243390eb-00b7-4551-a6b8-021bb34d1674')
set(inner_keys, 'zeus', '4747d33b-12b0-45e6-ac10-a8e191d6adaa')

def echo_each(k, v, u) {
    s = concat('keys[', k, ']=', v)
    say(s)
}
foreach(inner_keys, echo_each, [])

Output:

keys[1]=e66fd4aa-f281-472f-b919-fc7e7474de25
keys[2]=66fee78d-1887-42ec-9119-a9b50b7fbca2
keys[dev]=243390eb-00b7-4551-a6b8-021bb34d1674
keys[zeus]=4747d33b-12b0-45e6-ac10-a8e191d6adaa

Example 2: Print the first two .ts segments from an M3U8 playlist and stop the loop early. The callback tracks a counter in user_data and returns false once two .ts entries have been printed.

def echo_each(k, v, u) {
    say(v)

    if match(v, '.*ts') {
        ts_cnt = get(u, 'ts_cnt')
        ts_cnt = add(ts_cnt, 1)
        set(u, 'ts_cnt', ts_cnt)

        if ge(ts_cnt, 2) {
            return false
        }
    }
}

m3u8 = ''
m3u8 = concat(m3u8, '#EXTM3U8', '\n')
m3u8 = concat(m3u8, '#EXT-X-MEDIA-SEQUENCE:140651513\n')
m3u8 = concat(m3u8, '#EXT-X-TARGETDURATION:10\n')
m3u8 = concat(m3u8, '#EXTINF:8,\n')
m3u8 = concat(m3u8, 'http://vapp1.fw.live.cntv.cn/cache/289_/seg0/index140651514_140651513.ts\n')
m3u8 = concat(m3u8, '#EXTINF:9,\n')
m3u8 = concat(m3u8, 'http://vapp1.fw.live.cntv.cn/cache/289_/seg0/index140651514_140651514.ts\n')
m3u8 = concat(m3u8, '#EXTINF:10,\n')
m3u8 = concat(m3u8, 'http://vapp1.fw.live.cntv.cn/cache/289_/seg0/index140651514_140651515.ts\n')

lines = split(m3u8, '\n')
u = []
set(u, 'ts_cnt', 0)
foreach(lines, echo_each, u)

Output:

#EXTM3U8
#EXT-X-MEDIA-SEQUENCE:140651513
#EXT-X-TARGETDURATION:10
#EXTINF:8,
http://vapp1.fw.live.cntv.cn/cache/289_/seg0/index140651514_140651513.ts
#EXTINF:9,
http://vapp1.fw.live.cntv.cn/cache/289_/seg0/index140651514_140651514.ts

del

Item Description
Syntax del(d, k)
Description Deletes a key-value pair from dictionary d.
Parameters d: the dictionary. k: the key (any type).
Return value true

Example: Set a key, confirm it exists, delete it, then confirm it is gone. The response header reflects both states.

var_a = []
set(var_a, 'note_a', 'note a info')
if get(var_a, 'note_a') {
    add_rsp_header('X-RESPOND-OUTPUT', 'found var_a key', true)
} else {
    add_rsp_header('X-RESPOND-OUTPUT', 'del var_a key', true)
}
del(var_a, 'note_a')
if get(var_a, 'note_a') {
    add_rsp_header('X-RESPOND-OUTPUT', 'found var_a key', true)
} else {
    add_rsp_header('X-RESPOND-OUTPUT', 'del var_a key', true)
}

Output:

X-RESPOND-OUTPUT: found var_a key
X-RESPOND-OUTPUT: del var_a key