Deployment strategies

From kid-sid/codex-spellbook · View interactive version

Deployment strategies is an AI agent skill from the GitHub repository kid-sid/codex-spellbook. Use when choosing a deployment strategy for a release, setting up canary or blue/green rollouts, adding feature flags to decouple deployment from release, coordinating a zero-downtime database migration, or defining rollback criteria and procedures.

How to use this skill

Skill file

Deployment Strategies

A reference for selecting and implementing deployment strategies that minimize risk, enable zero-downtime releases, and provide fast rollback paths.

When to Activate

Strategy Comparison

| Strategy | Traffic routing | Rollback speed | Risk | Infrastructure cost | Best for |

|---|---|---|---|---|---|

| Recreate | Stop all, start new | Fast (redeploy) | High (downtime) | Low | Dev/non-prod |

| Rolling update | Replace pods gradually | Medium (rollback flag) | Medium | Low | Most services |

| Blue/Green | Flip all traffic at once | Instant (flip back) | Low | 2x | High-stakes releases |

| Canary | Shift % traffic gradually | Instant (shift back) | Very low | Slightly > 1x | High-traffic, data-sensitive |

| A/B Testing | Route by user segment | Instant | Low | ~1x | Feature experiments |

| Shadow | Mirror traffic, no user impact | N/A | None | ~2x | Testing new version with real traffic |

Rolling Updates (Kubernetes)

Default Kubernetes behavior when you run kubectl apply. Pods are replaced incrementally — no full restart required.

strategy:
  type: RollingUpdate
  rollingUpdate:
    maxSurge: 1        # max pods above desired count during rollout
    maxUnavailable: 0  # never go below desired count (zero-downtime)

Blue/Green Deployments

Two identical environments run in parallel: Blue (live) and Green (new version). Traffic flips atomically from one to the other.

Process

  1. Deploy new version to Green environment
  2. Run smoke tests against Green (no user traffic yet)
  3. Flip traffic: update load balancer rule or Kubernetes Service selector
  4. Monitor error rate and latency for 15–30 minutes
  5. Decommission Blue (or keep as instant rollback for 24 hours)

Kubernetes Implementation

Flip the Service selector to switch which deployment receives traffic.

# Blue deployment (live)
spec:
  selector:
    app: payment-service
    version: blue   # Service points here

# Green deployment (new)
spec:
  selector:
    app: payment-service
    version: green  # Update Service to point here after smoke tests

Flip command:

kubectl patch service payment-service -p '{"spec":{"selector":{"version":"green"}}}'

Considerations

Canary Releases

Gradually shift traffic from the stable version to the new version. Automated analysis gates promotion based on SLO metrics.

Argo Rollouts

apiVersion: argoproj.io/v1alpha1
kind: Rollout
metadata:
  name: payment-service
spec:
  strategy:
    canary:
      steps:
        - setWeight: 5
        - pause: { duration: 10m }
        - setWeight: 25
        - pause: {}  # manual gate — requires human approval
        - setWeight: 50
        - pause: { duration: 10m }
        - setWeight: 100
      analysis:
        templates:
          - templateName: error-rate-check
        startingStep: 1
        args:
          - name: service-name
            value: payment-service

Promote or abort the rollout:

kubectl argo rollouts promote payment-service  # advance to next step
kubectl argo rollouts abort payment-service    # rollback to stable

Flagger (Linkerd / Istio)

Flagger integrates with service meshes for automatic traffic splitting and metric-based promotion. Define a Canary CR with analysis.metrics referencing Prometheus queries. Flagger handles weight increments and rollback automatically — no manual step definitions required.

Feature Flags

Why Feature Flags

Flag Lifecycle

  1. Add flag (disabled by default)
  2. Deploy code wrapped behind flag
  3. Enable for internal users → beta users → percentage rollout → 100%
  4. Remove flag and dead code (flags are technical debt — clean up within a sprint of full rollout)

Tools Comparison

| Tool | Hosting | SDK support | Best for |

|---|---|---|---|

| LaunchDarkly | Cloud (paid) | 20+ SDKs | Enterprise, A/B testing |

| Unleash | Self-hosted or cloud | 10+ SDKs | Open-source, full control |

| OpenFeature | Standard (vendor-agnostic SDK) | All vendors | Portability across providers |

| AWS AppConfig | Cloud | AWS SDK | AWS-native workloads |

| Environment variables | N/A | Simple | Simple boolean flags, no runtime toggle needed |

Code Pattern (OpenFeature)

import { OpenFeature } from '@openfeature/server-sdk';

const client = OpenFeature.getClient();

// Simple boolean flag
const isNewCheckoutEnabled = await client.getBooleanValue(
  'new-checkout-flow',
  false,  // default value — returned if flag is missing or evaluation fails
  { targetingKey: userId }
);

if (isNewCheckoutEnabled) {
  return newCheckoutHandler(req, res);
} else {
  return legacyCheckoutHandler(req, res);
}

OpenFeature's provider abstraction means swapping from LaunchDarkly to Unleash requires changing only the registered provider — application code stays the same.

Database Migrations and Zero-Downtime Deployments

The Problem

Direct ALTER TABLE can lock tables under load. Renaming columns breaks the old app version that runs alongside the new version during a rolling deploy. Any migration that removes or renames a column must be done in phases.

Expand-Contract Pattern (Parallel Change)

Use for: adding NOT NULL columns, renaming columns or tables, changing data types.

Phase 1 — Expand (additive only):

Phase 2 — Migrate:

```sql

UPDATE table SET new_col = old_col WHERE new_col IS NULL LIMIT 10000;

```

Phase 3 — Contract (remove old):

Example Timeline

Renaming user.username to user.display_name:

Sprint 1: Add display_name (nullable), write to both columns
Sprint 2: Backfill rows, read from display_name, add NOT NULL
Sprint 3: Remove username column

Large Table Migrations

For tables with millions of rows, use pt-online-schema-change (Percona) or gh-ost (GitHub) to perform the migration on a shadow table and cut over with minimal locking.

Rollback Procedures

When to Roll Back

Roll back when:

Do not roll back immediately for:

Rollback Decision Tree

Error rate > SLO?
├── Yes → Can we fix forward in < 15 minutes? → No  → ROLLBACK
│                                              → Yes → hotfix + monitor
└── No  → Monitor, do not rollback

Rollback Commands

# Kubernetes rolling update — undo last rollout
kubectl rollout undo deployment/payment-service

# Kubernetes — target a specific revision
kubectl rollout undo deployment/payment-service --to-revision=3

# Argo Rollouts canary — abort and revert to stable
kubectl argo rollouts abort payment-service

# Helm — rollback to a previous release number
helm rollback payment-service 3

Rollback Runbook Template

## Rollback: [Service Name]

**Trigger criteria:** [e.g., error rate > 1% for 5 minutes]

**Steps:**
1. Notify on-call channel: "@oncall rolling back payment-service due to [reason]"
2. Run: `kubectl rollout undo deployment/payment-service -n production`
3. Verify: `kubectl rollout status deployment/payment-service`
4. Check metrics: confirm error rate returns to baseline
5. Create incident ticket with timeline and root cause

**Data rollback:** [specify if DB migration rollback is needed and how]
**Escalation:** [who to page if rollback fails]

See also: ci-cd, containerization, observability, incident-response

Red Flags

Checklist

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.