Availability
Active-active deployment is available from Tyk AI Studio 2.3.0.
Overview
In an active-active deployment, AI Studio runs in two regions at the same time. When you lose one region, the other region continues to operate. Edge Gateways continue to serve traffic and move to the control plane of the other region without help.Topology
The deployment has these parts:- AI Studio replicas in both regions. All replicas write to the PostgreSQL node that is the primary at that time. The replicas coordinate through the database in the same way as several replicas in one region. One replica is the leader for the work that must run only once.
- One primary and one hot standby. The primary is in one region and the standby is in the other region. AI Studio does not read from the standby. Your failover manager promotes it.
- A witness in a third location. The witness lets the failover manager decide on a failover safely. Refer to Add a Witness in a Third Location.
- Edge Gateways with two control endpoints. Each Edge Gateway lists the control plane of its own region first and the other region second.
Database Requirements
- PostgreSQL 14 or later. The failover behavior on this page is tested with PostgreSQL 16.
- PostgreSQL with physical streaming replication. Put the primary in one region and a hot standby in the other region (
hot_standby = on). - Synchronous replication between the regions (recommended). On the primary, set
synchronous_commit = onand setsynchronous_standby_namesto the standby of the other region.- A commit then completes only after the standby has it.
- If you lose the region of the primary, you lose no committed data.
- Automatic failover. Use a managed PostgreSQL service with cross-zone or cross-region high availability.
- NTP on the database nodes and on the AI Studio replicas. The leader lease of AI Studio allows for a clock difference of up to 30 seconds after a failover. Larger differences are not supported.
Add a Witness in a Third Location
Two regions cannot decide on a failover safely by themselves. When region A loses contact with region B, region A cannot tell if region B is down or if only the link is down. If each region promotes its own node, both nodes accept writes. This is split brain, and you lose the writes of one side when the regions join again. A witness in a third location breaks the tie. Use one of these:- The control plane of a managed PostgreSQL service
- A Kubernetes API server, or a CloudNativePG or Patroni quorum, across three zones
- A witness node in a third region, such as an etcd, Consul, or ZooKeeper member
Configure AI Studio
Database Connection String
Give every AI Studio replica in both regions the same multi-host connection string. Settarget_session_attrs=read-write, so that the driver connects to the node that accepts writes:
- List the node of your own region first. The driver tries the hosts in order. A node that does not answer costs the full connect timeout before the driver tries the next host.
- Set a short connect timeout. Use
connect_timeout=3in the URL, or setDATABASE_CONNECT_TIMEOUT. - Set an explicit
sslmode, such asrequireorverify-full.
Failover and Cluster Settings
The Held by column of the Edge Gateways page shows the label of the replica that holds each Edge Gateway. The column shows when more than one replica holds Edge Gateways.
For all AI Studio variables, refer to AI Studio Environment Variables.
Settings That Must Match in Both Regions
AI Studio compares the first four secrets across the live replicas, and the cluster status shows a warning when they differ. A replica with a different value still starts, so that you can roll a rotation through the replicas.
You cannot rotate
TYK_AI_SECRET_KEY or MICROGATEWAY_ENCRYPTION_KEY on a running deployment. Refer to Secrets Management and Shared Secrets Reference.
Load Balancers and Health Checks
- Put the UI and API of both regions behind one name with session affinity.
- Chat sessions, agent conversations, and MCP connections over SSE stay on the replica that started them.
- Sign-in sessions are in the database, so they continue on the other region.
- Use
GET /healthfor liveness. It does not check the database. - Use
GET /readyfor readiness. The response body reports the database state aswritable,read_only, orunreachable./readystays200during a database failover that is shorter thanDATABASE_READY_GRACE. Thus, a failover does not remove all replicas from the load balancer.
- Use
GET /ready?require=writeif the load balancer must send traffic only to replicas that can save changes. It returns200only while the database accepts writes, with the same grace period. - Give the gRPC port (
50051) of each region its own address. A load balancer in front of this port must support HTTP/2. It must keep idle streams open for at least one minute. - Keep DNS available inside each region. If a region reaches its DNS server only across the link between the regions, a link failure stops name resolution. On Kubernetes, run a DNS replica in each zone and route lookups to the replica in the same zone.
Configure Edge Gateways
Two Control Endpoints
SetCONTROL_ENDPOINTS on each Edge Gateway. List the control plane of its own region first:
CONTROL_ENDPOINT and CONTROL_ENDPOINTS together. The Edge Gateway does not start if you set both.
Edge Gateway Settings
For the other settings that keep an Edge Gateway running during an outage, refer to Edge Gateway Resilience.
Start Without a Control Plane
By default, an Edge Gateway that starts while no control plane answers exits. To let it start from the configuration that it last applied, setEDGE_START_FROM_LAST_KNOWN_GOOD=true. Refer to Start Without a Control Plane.
Full Credential Sync (Enterprise)
WithEDGE_CREDENTIAL_SYNC=full, an Edge Gateway validates App keys from a hashed list, with or without a control plane. Refer to Full Credential Sync.
Size Edge Gateways for the Loss of a Region
After you lose a region, the Edge Gateways of the other region carry all traffic. Size the Edge Gateways of each region for the load of both regions. During a control-plane outage, each Edge Gateway also holds its undelivered analytics in memory. For sizing guidance, refer to Scaling Edge Gateways.What Happens During a Failover
Requests While There Is No Primary
Between the loss of the primary and the promotion of the standby, no node accepts writes. AI Studio holds the database statement of a request and tries it again until a writable node answers, for up toDATABASE_WRITE_RETRY_WINDOW:
- If the promotion completes inside the window, the request waits a few seconds and then succeeds once. AI Studio creates an App or a key only once during an automatic failover.
- If no promotion completes inside the window, the request gets
503withRetry-Afterand the error codedatabase_unavailable. - Sign-in requests and requests with a session or an API key also get
503, and never401. Clients then retry and keep their credentials. - AI Studio does not queue a request after the window. The client sends the request again.
- A statement in a transaction that was already open when the primary was lost
- A statement that may have reached the server
- New statements when 512 statements are already held on the replica
- Calls from Edge Gateways to the control plane, so that an Edge Gateway moves to the other region without delay
Failure Scenarios
- You lose a whole region (AI Studio and its PostgreSQL node).
- The Edge Gateways of that region move to the control plane of the other region.
- If the lost node was the primary, the failover manager promotes the standby. The AI Studio replicas in the other region then write to it without a restart.
- A surviving replica becomes the leader within about 30 seconds.
- You need a witness for automatic failover, and capacity in each region for all traffic.
- You lose the PostgreSQL primary, and both AI Studio regions are up.
- AI Studio holds requests for up to
DATABASE_WRITE_RETRY_WINDOW. They complete after the promotion, or they get503. /readystays200forDATABASE_READY_GRACE.- You need a promotion time shorter than
DATABASE_WRITE_RETRY_WINDOW, andDATABASE_READY_GRACElonger than both.
- AI Studio holds requests for up to
- A primary is demoted but continues to run (a switchover).
- Writes on the old primary fail. AI Studio detects this at the first refused write, or within 5 seconds when idle. It then reconnects to the new primary.
- You need no action.
- You lose AI Studio in one region, and its database node is up.
- The Edge Gateways of that region move to the other region. The load balancer stops sending traffic there.
- Users on that region can lose an open chat session.
- You need session affinity that fails over. Set
EDGE_CONTROL_FAILBACK_INTERVALif Edge Gateways must return when the region is back.
- You lose the link between the regions.
- The region without the primary cannot write. After
DATABASE_READY_GRACE, its/readyreturns503and the load balancer stops sending traffic there. - Its Edge Gateways continue to serve traffic.
- The failover manager must not promote the standby unless a witness confirms that the region of the primary is lost.
- You need a witness, alerts on
aistudio_db_writable, and DNS that answers inside each region.
- The region without the primary cannot write. After
- An Edge Gateway restarts during a control-plane outage.
- It exits, unless
EDGE_START_FROM_LAST_KNOWN_GOOD=trueand its database is on a persistent volume. - It validates keys only with full credential sync.
- You need a decision on last-known-good, and persistent volumes.
- It exits, unless
Promote the Standby Manually
Use this procedure when you have no witness and an administrator decides that the region of the primary is lost:- Make sure that the old primary cannot accept writes. Stop it, or isolate it from the network (fence it). If you promote the standby while the old primary can still accept writes, you get split brain.
- Promote the standby. Use
pg_ctl promoteorSELECT pg_promote()on the standby, or the command of your failover manager. - Clear
synchronous_standby_nameson the new primary. Otherwise every commit waits for a standby that is not there. - Check that the AI Studio replicas write to the new primary. The
aistudio_db_writablemetric is1on each replica, and the cluster status shows all replicas.
DATABASE_WRITE_RETRY_WINDOW before the promotion got 503, and users must make these changes again.
Bring the Old Primary Back as a Standby
Do not start the old primary as a primary next to the promoted node:- Rebuild the old primary as a standby of the new primary. Use
pg_rewind, a newpg_basebackup, or the reinit command of your failover manager. - Wait until it streams, then make it synchronous again.
- Start the AI Studio replicas of that region. They wait for a writable database for up to
DATABASE_STARTUP_TIMEOUT, then join the cluster.
EDGE_CONTROL_FAILBACK_INTERVAL if you set it.
Monitoring
The cluster status is available from the AI Studio API at
/api/v1/cluster/status. It requires read access to Edge Gateways. Refer to the AI Studio API reference. For metrics setup, refer to Observability.