# PostgreSQL, MySQL, Redis, Memcached, MQTT & AMQP Captures

> **FrameAssembly v3.2.2** · Anthology 08 (Layer 7 · 13 Recipes) · MIT License · Canonical URL: https://frameassembly-lang.dev/docs/examples/databases-caches-brokers

East-west microservice data-tier segmentation and IoT message-bus traces: `postgres` v3 Wire queries, `mysql` greetings and SQL commands, `redis` RESP arrays, `memcached` TCP/UDP, `mqtt`, and `amqp`.

## Key Wire & Syntax Takeaways

- `postgres`, `mysql`, and `redis` build authentic binary/RESP wire messages on TCP 5432, 3306, and 6379 for zero-trust east-west ACL testing.
- `memcached` is one of the 9 dual-transport protocols, supporting default UDP 11211 (8-byte request ID header) or `transport tcp`.
- `mqtt` (TCP 1883) and `amqp` (TCP 5672) encode variable-length broker frames for industrial and cloud pub/sub telemetry validation.

## 08.01 · Dual-Stack IPv4 + IPv6 PostgreSQL Read-Replica Health Flow (`postgres_dual_stack_replica.frasm`)

Runs an identical PostgreSQL v3 startup, authentication, and SQL readiness probe across IPv4 and IPv6 database subnets via flow(app, db).

- **Protocols:** `tcp`, `postgres` (9 Ethernet frames)
- **Wire Behavior:** Encodes big-endian PostgreSQL StartupMessage (196608 = v3.0), AuthenticationOk, Query ("Q"), CommandComplete ("C"), and ReadyForQuery ("Z").
- **Wireshark Filter:** `pgsql`
- **CLI Build:** `frameassembly postgres_dual_stack_replica.frasm`

```frameassembly
host api_v4 { ip 10.50.1.10 }
host pg_v4  { ip 10.50.2.54 }
host api_v6 { ip 2001:db8:50:1::10 }
host pg_v6  { ip 2001:db8:50:2::54 }

flow pg_replica_check(app, db) {
    app -> db tcp syn srcport 54320 dstport 5432 win 65535 mss 1440 wait 1ms
    app <- db tcp syn ack win 65535 mss 1440 wait 1ms
    app -> db tcp ack wait 1ms
    app -> db postgres startup user "health_ro" db "analytics" wait 1ms
    app <- db postgres authok wait 1ms
    app <- db postgres readyforquery wait 1ms
    app -> db postgres query sql "SELECT pg_is_in_recovery();" wait 2ms
    app <- db postgres commandcomplete wait 1ms
    app <- db postgres readyforquery
}

compile {
    pg_replica_check(api_v4, pg_v4)
    pg_replica_check(api_v6, pg_v6)
}
```

## 08.02 · MQTT v3.1.1 Broker Connect, Topic Subscribe & QoS 1 Publish / PubAck (`mqtt_qos1_sensor_bus.frasm`)

Models an edge gateway connecting to an internal MQTT broker on TCP 1883, subscribing to command topics, and publishing QoS 1 sensor telemetry.

- **Protocols:** `tcp`, `mqtt` (11 Ethernet frames)
- **Wire Behavior:** Encodes variable-byte MQTT Remaining Length headers across CONNECT, CONNACK, SUBSCRIBE, SUBACK, PUBLISH, PUBACK, and PINGREQ/PINGRESP.
- **Wireshark Filter:** `mqtt`
- **CLI Build:** `frameassembly mqtt_qos1_sensor_bus.frasm`

```frameassembly
host edge_gw     { ip 10.75.10.22 }
host mqtt_broker { ip 10.75.0.18 }

compile {
    edge_gw -> mqtt_broker tcp syn srcport 51883 dstport 1883 win 65535 mss 1460 wait 1ms
    edge_gw <- mqtt_broker tcp syn ack win 65535 mss 1460 wait 1ms
    edge_gw -> mqtt_broker tcp ack wait 1ms
    edge_gw -> mqtt_broker mqtt connect clientid "gw-line04" wait 2ms
    edge_gw <- mqtt_broker mqtt connack returncode 0 wait 2ms
    edge_gw -> mqtt_broker mqtt subscribe pktid 10 topic "plant/line04/cmd/#" qos 1 wait 2ms
    edge_gw <- mqtt_broker mqtt suback pktid 10 qos 1 wait 3ms
    edge_gw -> mqtt_broker mqtt publish pktid 11 topic "plant/line04/temp" qos 1 payload "{\"c\":64.5}" wait 2ms
    edge_gw <- mqtt_broker mqtt puback pktid 11 wait 10ms
    edge_gw -> mqtt_broker mqtt pingreq wait 1ms
    edge_gw <- mqtt_broker mqtt pingresp
}
```

## 08.03 · DMZ Web App to Internal PostgreSQL (TCP 5432) Segmentation Check (`fw_dmz_postgres_5432.frasm`)

Tests a narrow East-West firewall rule allowing a DMZ web server to authenticate and run a health query against an internal PostgreSQL database on TCP port 5432.

- **Protocols:** `vlan`, `tcp`, `postgres` (8 Ethernet frames)
- **Wire Behavior:** Feed this .pcap into Suricata or firewall packet-tracer tools to confirm deep-packet inspection recognizes valid PostgreSQL wire framing over VLAN 50.
- **Wireshark Filter:** `vlan.id == 50 && tcp.port == 5432`
- **CLI Build:** `frameassembly fw_dmz_postgres_5432.frasm`

```frameassembly
// DMZ application node and internal database host
host dmz_app { ip 172.16.50.20 }
host prod_db { ip 10.50.20.40 }

compile {
    // 1. Complete 3-way TCP handshake on PostgreSQL port 5432 (VLAN 50)
    dmz_app -> prod_db vlan 50 tcp syn srcport 52400 dstport 5432 win 65535 mss 1460 wait 1ms
    dmz_app <- prod_db vlan 50 tcp syn ack win 65535 mss 1460 wait 1ms
    dmz_app -> prod_db vlan 50 tcp ack wait 1ms

    // 2. PostgreSQL v3.0 startup, auth OK, and lightweight SQL check
    dmz_app -> prod_db vlan 50 postgres startup user "app_svc" db "orders" wait 1ms
    dmz_app <- prod_db vlan 50 postgres authok wait 1ms
    dmz_app -> prod_db vlan 50 postgres query sql "SELECT 1;" wait 2ms
    dmz_app <- prod_db vlan 50 postgres commandcomplete wait 1ms
    dmz_app <- prod_db vlan 50 postgres readyforquery
}
```

## 08.04 · Minimal Two-Block PostgreSQL (`postgres`) Quickstart (`postgres_quickstart.frasm`)

Smallest zero-boilerplate `host` + `compile` capture for PostgreSQL Frontend/Backend Wire Protocol v3.0 (TCP 5432, Dual-Stack IPv4/IPv6).

- **Protocols:** `postgres` (7 Ethernet frames)
- **Wire Behavior:** Defaults to TCP destination port `5432` (`psh ack`) and writes Protocol Version `3.0` (`0x00030000`) inside `startup` frames.
- **Wireshark Filter:** `pgsql`
- **CLI Build:** `frameassembly postgres_quickstart.frasm`

```frameassembly
host app_pod { ip 10.50.10.12 }
host pg_db   { ip 10.50.20.54 }

compile {
    app_pod -> pg_db tcp syn srcport 55432 dstport 5432 wait 1ms
    app_pod <- pg_db tcp syn ack wait 1ms
    app_pod -> pg_db tcp ack wait 1ms
    app_pod -> pg_db postgres startup user "app_ro" db "inventory" wait 2ms
    app_pod <- pg_db postgres authok wait 1ms
    app_pod <- pg_db postgres readyforquery wait 2ms
    app_pod -> pg_db postgres query sql "SELECT sku, qty FROM stock WHERE qty < 10;"
}
```

## 08.05 · Minimal Two-Block MySQL / MariaDB (`mysql`) Quickstart (`mysql_quickstart.frasm`)

Smallest zero-boilerplate `host` + `compile` capture for MySQL & MariaDB Wire Protocol v10 (TCP 3306, Dual-Stack IPv4/IPv6).

- **Protocols:** `mysql` (6 Ethernet frames)
- **Wire Behavior:** Defaults to TCP destination port `3306` (`psh ack`) and computes the 3-byte little-endian MySQL packet length automatically.
- **Wireshark Filter:** `mysql`
- **CLI Build:** `frameassembly mysql_quickstart.frasm`

```frameassembly
host app_srv  { ip 10.50.1.20 }
host mysql_db { ip 10.50.2.33 }

compile {
    app_srv -> mysql_db tcp syn srcport 53306 dstport 3306 wait 1ms
    app_srv <- mysql_db tcp syn ack wait 1ms
    app_srv -> mysql_db tcp ack wait 1ms
    app_srv <- mysql_db mysql greeting serverversion "8.0.36" wait 2ms
    app_srv -> mysql_db mysql login user "app_rw" db "orders" wait 2ms
    app_srv <- mysql_db mysql ok
}
```

## 08.06 · Minimal Two-Block Redis (RESP) (`redis`) Quickstart (`redis_quickstart.frasm`)

Smallest zero-boilerplate `host` + `compile` capture for Redis Serialization Protocol Cache & Broker (TCP 6379, Dual-Stack IPv4/IPv6).

- **Protocols:** `redis` (5 Ethernet frames)
- **Wire Behavior:** Defaults to TCP destination port `6379` (`psh ack`) and formats exact `*N
$<len>
...` RESP multibulk wire arrays.
- **Wireshark Filter:** `redis || tcp.port == 6379`
- **CLI Build:** `frameassembly redis_quickstart.frasm`

```frameassembly
host api_pod   { ip 10.44.1.12 }
host redis_primary { ip 10.44.2.10 }

compile {
    api_pod -> redis_primary tcp syn srcport 51637 dstport 6379 wait 1ms
    api_pod <- redis_primary tcp syn ack wait 1ms
    api_pod -> redis_primary tcp ack wait 1ms
    api_pod -> redis_primary redis set key "session:4821" value "active" wait 1ms
    api_pod <- redis_primary redis replytype "ok" value "OK"
}
```

## 08.07 · Minimal Two-Block MQTT (`mqtt`) Quickstart (`mqtt_quickstart.frasm`)

Smallest zero-boilerplate `host` + `compile` capture for Message Queuing Telemetry Transport (IoT & IIoT) (TCP 1883, Dual-Stack IPv4/IPv6).

- **Protocols:** `mqtt` (6 Ethernet frames)
- **Wire Behavior:** Defaults to TCP destination port `1883` (`psh ack`) and encodes the multi-byte variable-length `Remaining Length` field per OASIS MQTT §2.2.3.
- **Wireshark Filter:** `mqtt`
- **CLI Build:** `frameassembly mqtt_quickstart.frasm`

```frameassembly
host edge_gw { ip 10.60.10.25 }
host broker  { ip 10.60.10.10 }

compile {
    edge_gw -> broker tcp syn srcport 51883 dstport 1883 wait 1ms
    edge_gw <- broker tcp syn ack wait 1ms
    edge_gw -> broker tcp ack wait 1ms
    edge_gw -> broker mqtt connect clientid "gw-rack04" keepalive 60 wait 2ms
    edge_gw <- broker mqtt connack returncode 0 wait 2ms
    edge_gw -> broker mqtt publish topic "dc1/rack04/kw" qos 0 retain payload "4.85"
}
```

## 08.08 · PostgreSQL (`postgres`): Full PostgreSQL v3.0 Startup, SQL Query Lifecycle & ErrorResponse (`postgres_session_audit.frasm`)

Completes a TCP 5432 handshake, authenticates via `startup` -> `authok` -> `readyforquery`, runs an audit query (`commandcomplete` + `readyforquery`), and triggers an `errorresponse` on a restricted table.

- **Protocols:** `postgres`, `tcp` (11 Ethernet frames)
- **Wire Behavior:** Defaults to TCP destination port `5432` (`psh ack`) and writes Protocol Version `3.0` (`0x00030000`) inside `startup` frames. Computes the 32-bit big-endian self-inclusive length (`4 + body_bytes`) on every `'R'`, `'Q'`, `'C'`, `'Z'`, and `'E'` message.
- **Wireshark Filter:** `pgsql`
- **CLI Build:** `frameassembly show postgres_session_audit.frasm`

```frameassembly
host api_svc { ip 172.16.54.15 }
host pg_pri  { ip 172.16.54.100 }

compile {
    api_svc -> pg_pri tcp syn srcport 51432 dstport 5432 win 65535 mss 1460 wait 1ms
    api_svc <- pg_pri tcp syn ack win 65535 mss 1460 wait 1ms
    api_svc -> pg_pri tcp ack wait 1ms
    // 1. Client-first StartupMessage -> AuthenticationOk + ReadyForQuery ('I')
    api_svc -> pg_pri postgres startup user "reporting_ro" db "analytics" wait 2ms
    api_svc <- pg_pri postgres authok wait 1ms
    api_svc <- pg_pri postgres readyforquery wait 2ms
    // 2. Authorized SELECT query -> CommandComplete + ReadyForQuery
    api_svc -> pg_pri postgres query sql "SELECT count(*) FROM daily_events;" wait 3ms
    api_svc <- pg_pri postgres commandcomplete wait 1ms
    api_svc <- pg_pri postgres readyforquery wait 4ms
    // 3. Unauthorized write attempt -> ErrorResponse ('E')
    api_svc -> pg_pri postgres query sql "DROP TABLE audit_trail;" wait 2ms
    api_svc <- pg_pri postgres errorresponse
}
```

## 08.09 · MySQL / MariaDB (`mysql`): Blocked `root` Login (`errcode 1045`) + Authorized Application SQL Session (`mysql_dam_audit.frasm`)

Shows a rejected `root` login returning MySQL Error `1045`, followed by an authorized `app_ro` connection running a `SELECT` query.

- **Protocols:** `mysql`, `tcp` (14 Ethernet frames)
- **Wire Behavior:** Defaults to TCP destination port `3306` (`psh ack`) and computes the 3-byte little-endian MySQL packet length automatically. Encodes NUL-terminated server version, thread ID, auth-plugin-data salt, and capability flags inside `greeting` so Wireshark recognizes the session as MySQL.
- **Wireshark Filter:** `mysql`
- **CLI Build:** `frameassembly show mysql_dam_audit.frasm`

```frameassembly
host web_node { ip 172.16.50.15 }
host db_main  { ip 172.16.60.10 }

compile {
    // 1. Unauthorized root login attempt -> MySQL Error 1045
    web_node -> db_main tcp syn srcport 54306 dstport 3306 win 65535 mss 1460 wait 1ms
    web_node <- db_main tcp syn ack win 65535 mss 1460 wait 1ms
    web_node -> db_main tcp ack wait 1ms
    web_node <- db_main mysql greeting serverversion "8.0.37-commercial" wait 2ms
    web_node -> db_main mysql login user "root" db "mysql" wait 2ms
    web_node <- db_main mysql err errcode 1045 wait 5ms
    // 2. Authorized query on a second connection
    web_node -> db_main tcp syn srcport 54307 dstport 3306 win 65535 mss 1460 wait 1ms
    web_node <- db_main tcp syn ack win 65535 mss 1460 wait 1ms
    web_node -> db_main tcp ack wait 1ms
    web_node <- db_main mysql greeting serverversion "8.0.37-commercial" wait 1ms
    web_node -> db_main mysql login user "app_ro" db "billing" wait 1ms
    web_node <- db_main mysql ok wait 2ms
    web_node -> db_main mysql query sql "SELECT invoice_id, total_cents FROM invoices WHERE paid = 0 LIMIT 25" wait 3ms
    web_node <- db_main mysql ok
}
```

## 08.10 · Redis (RESP) (`redis`): Blocked Unauthenticated `CONFIG` Probe (`-NOAUTH`) + Authenticated Cache Read/Write (`redis_security_audit.frasm`)

Tests an unauthenticated command rejected with `-NOAUTH Authentication required.`, followed by `AUTH`, `SET`, and bulk `GET`.

- **Protocols:** `redis`, `tcp` (11 Ethernet frames)
- **Wire Behavior:** Defaults to TCP destination port `6379` (`psh ack`) and formats exact `*N
$<len>
...` RESP multibulk wire arrays. Emits `+OK
` (`replytype "ok"`), `-ERR ...
` (`replytype "err"`), `:<num>
` (`replytype "integer"`), or `$<len>
<value>
` (`replytype "bulk"`) on server responses.
- **Wireshark Filter:** `redis || tcp.port == 6379`
- **CLI Build:** `frameassembly show redis_security_audit.frasm`

```frameassembly
host app_worker { ip 172.16.44.20 }
host cache_node { ip 172.16.44.63 }

compile {
    app_worker -> cache_node tcp syn srcport 52637 dstport 6379 win 65535 mss 1460 wait 1ms
    app_worker <- cache_node tcp syn ack win 65535 mss 1460 wait 1ms
    app_worker -> cache_node tcp ack wait 1ms
    // 1. Unauthenticated probe rejected with RESP error (-)
    app_worker -> cache_node redis cmd "INFO" wait 1ms
    app_worker <- cache_node redis replytype "err" value "NOAUTH Authentication required." wait 3ms
    // 2. Authenticate + SET + GET
    app_worker -> cache_node redis cmd "AUTH" args "LabCacheToken2026" wait 1ms
    app_worker <- cache_node redis replytype "ok" value "OK" wait 1ms
    app_worker -> cache_node redis set key "ratelimit:198.51.100.9" value "42" wait 1ms
    app_worker <- cache_node redis replytype "ok" value "OK" wait 1ms
    app_worker -> cache_node redis get key "ratelimit:198.51.100.9" wait 1ms
    app_worker <- cache_node redis replytype "bulk" value "42"
}
```

## 08.11 · Memcached (`memcached`): UDP 11211 `stats` Amplification Audit + TCP ASCII & Binary Cache Operations (`memcached_udp_and_tcp.frasm`)

Tests a UDP 11211 `stats` probe, then opens a TCP 11211 session exercising both ASCII `set`/`get` and Binary (`opcode 0x00`) `get`.

- **Protocols:** `memcached`, `tcp` (6 Ethernet frames)
- **Wire Behavior:** Prepends the mandatory 8-byte Memcached UDP frame header (`Request ID`, `Seq 0`, `Total Datagrams 1`, `Reserved 0`) automatically on UDP `11211`, and omits it cleanly when `transport tcp` is set. Formats exact `<byte_len>
<value>
` blocks on ASCII `set` commands.
- **Wireshark Filter:** `memcache`
- **CLI Build:** `frameassembly show memcached_udp_and_tcp.frasm`

```frameassembly
host auditor   { ip 172.16.45.10 }
host memcache  { ip 172.16.45.112 }

compile {
    // 1. UDP 11211 amplification audit probe (with 8-byte UDP header)
    auditor -> memcache memcached cmd "stats" wait 4ms
    // 2. Authorized internal TCP 11211 session (ASCII + Binary)
    auditor -> memcache tcp syn srcport 53211 dstport 11211 win 65535 mss 1460 wait 1ms
    auditor <- memcache tcp syn ack win 65535 mss 1460 wait 1ms
    auditor -> memcache tcp ack wait 1ms
    auditor -> memcache memcached set key "cfg:feature_x" value "enabled" flags 0 exptime 600 transport tcp wait 2ms
    auditor -> memcache memcached get key "cfg:feature_x" opcode 0x00 opaque 0x99 transport tcp
}
```

## 08.12 · MQTT (`mqtt`): IIoT Gateway Connect, Wildcard Topic Subscribe, QoS 1 Publish & Keepalive Ping (`mqtt_iiot_telemetry.frasm`)

Connects an industrial gateway to TCP 1883, subscribes to control commands, publishes a `qos 1` retained sensor reading acknowledged via `puback`, and exchanges `pingreq`/`pingresp`.

- **Protocols:** `mqtt`, `tcp` (12 Ethernet frames)
- **Wire Behavior:** Defaults to TCP destination port `1883` (`psh ack`) and encodes the multi-byte variable-length `Remaining Length` field per OASIS MQTT §2.2.3. Encodes the `"MQTT"` Protocol Level `4` header inside `connect`, and includes the 2-byte Packet Identifier (`pktid`) automatically on `qos 1` / `qos 2` `publish`, `puback`, `subscribe`, and `suback` packets.
- **Wireshark Filter:** `mqtt`
- **CLI Build:** `frameassembly show mqtt_iiot_telemetry.frasm`

```frameassembly
host plc_gw      { ip 192.168.80.21 }
host mqtt_broker { ip 192.168.80.10 }

compile {
    plc_gw -> mqtt_broker tcp syn srcport 52883 dstport 1883 win 65535 mss 1460 wait 1ms
    plc_gw <- mqtt_broker tcp syn ack win 65535 mss 1460 wait 1ms
    plc_gw -> mqtt_broker tcp ack wait 1ms
    plc_gw -> mqtt_broker mqtt connect clientid "line2-plc-gw" keepalive 30 wait 2ms
    plc_gw <- mqtt_broker mqtt connack returncode 0 wait 2ms
    // 1. Subscribe to actuator commands (pktid 1)
    plc_gw -> mqtt_broker mqtt subscribe pktid 1 topic "plant1/line2/cmd" qos 1 wait 2ms
    plc_gw <- mqtt_broker mqtt suback pktid 1 wait 3ms
    // 2. Publish QoS 1 retained telemetry (pktid 2 -> puback)
    plc_gw -> mqtt_broker mqtt publish topic "plant1/line2/pressure_bar" qos 1 retain pktid 2 payload "{\"bar\":6.42}" wait 2ms
    plc_gw <- mqtt_broker mqtt puback pktid 2 wait 10ms
    // 3. Keepalive heartbeat + clean disconnect
    plc_gw -> mqtt_broker mqtt pingreq wait 1ms
    plc_gw <- mqtt_broker mqtt pingresp wait 2ms
    plc_gw -> mqtt_broker mqtt disconnect
}
```

## 08.13 · AMQP 0-9-1 (`amqp`): Full RabbitMQ Connection Startup, Channel Open, Publish, Deliver & Heartbeat (`amqp_rabbitmq_flow.frasm`)

Walks through TCP 5672 connection startup, opening Channel 1, publishing an audit event to `"sec.events"`, delivering to a worker, and sending a Channel 0 heartbeat.

- **Protocols:** `amqp`, `tcp` (9 Ethernet frames)
- **Wire Behavior:** Defaults to TCP destination port `5672` (`psh ack`) and appends the mandatory `0xce` frame-end octet to every AMQP 0-9-1 frame. Emits the exact 8-byte `AMQP\x00\x00\x09\x01` connection header on `protocolheader` so Wireshark locks onto the AMQP dissector.
- **Wireshark Filter:** `amqp`
- **CLI Build:** `frameassembly show amqp_rabbitmq_flow.frasm`

```frameassembly
host auth_svc { ip 172.16.65.10 }
host mq_node  { ip 172.16.65.56 }

compile {
    auth_svc -> mq_node tcp syn srcport 51672 dstport 5672 win 65535 mss 1460 wait 1ms
    auth_svc <- mq_node tcp syn ack win 65535 mss 1460 wait 1ms
    auth_svc -> mq_node tcp ack wait 1ms
    auth_svc -> mq_node amqp protocolheader wait 1ms
    auth_svc <- mq_node amqp connectionstart channel 0 wait 2ms
    auth_svc -> mq_node amqp channelopen channel 1 wait 2ms
    auth_svc -> mq_node amqp basicpublish channel 1 exchange "sec.events" routingkey "login.mfa" payload "{\"user\":\"netadmin\"}" wait 3ms
    auth_svc <- mq_node amqp basicdeliver channel 1 exchange "sec.events" routingkey "login.mfa" payload "{\"user\":\"netadmin\"}" wait 10ms
    auth_svc <- mq_node amqp heartbeat channel 0
}
```
