Karthik Murugan
Karthik Murugan

Reputation: 1439

HAProxy stick-table store a custom value

I'd like to store a custom "value" in stick-table and use that in another ACL to select the server.

I've this config, which creates stick-table with the header value "x-external-id" as key and server-id as its value.

frontend frontend
   bind 125.213.51.144:8080
   default_backend  backend

backend  backend
   balance roundrobin
   stick store-request req.hdr(x-external-id)
   stick-table type string len 50 size 200k nopurge
   server gw1 125.213.51.100:8080 check id 1
   server gw2 125.213.51.101:8080 check id 2

This config produced this stick table:

# table: backend, type: string, size:204800, used:3
0x558955d52ac4: key=00000000000 use=0 exp=0 server_id=1
0x558955d53114: key=11111111111 use=0 exp=0 server_id=2
0x558955d87a34: key=22222222222 use=0 exp=0 server_id=2

The value (server-id) is set by HaProxy based on the server handled the request. But I'd like to save a custom value here. Is it possible?

Upvotes: 3

Views: 3542

Answers (1)

Karthik Murugan
Karthik Murugan

Reputation: 1439

Apparently HAProxy doesn't allow storing custom values. Only server_id and tracking counters can be stored in stick table.

So I defined two backends with one stick table each. Each client hits its own backend and populates the stick table.

From another HAProxy section, I could use table_server_id to lookup in stick tables and route to the backend which owned the stick table having the entry.

############## Frontend ################
frontend my-frontend
   bind 125.213.51.100:38989

   acl is_service1 req.hdr(x-external-id),table_server_id(stick-table-1) -m int gt 0 
   use_backend my-backend    if is_service1
   
   acl is_service2 req.hdr(x-external-id),table_server_id(stick-table-2) -m int gt 0  
   use_backend my-backend-2  if is_service2

   default_backend my-backend-default

############## Backend 1 ################
backend my-backend
   balance roundrobin
   server service1 125.213.51.100:18989 check id 1 inter 10s fall 1 rise 1
   server service2 125.213.51.200:18989 check id 2 backup

############## Backend 2 ################
backend my-backend-2
   balance roundrobin
   server service2 125.213.51.100:18989 check id 2 inter 10s fall 1 rise 1
   server service1 125.213.51.200:18989 check id 1 backup

############## Backend Default  ################
backend my-backend-default
   balance roundrobin
   server service1 125.213.51.100:18989 check id 1
   server service2 125.213.51.200:28989 check id 2

Upvotes: 2

Related Questions