<@ULVA73B9P> can a deployment have multiple versio...
# ask-marvin
g
@Marvin can a deployment have multiple versions active?
m
thought for 25.6 seconds
No, a deployment cannot have multiple versions active simultaneously. Here's how deployment versioning works in Prefect: 1. Single Active Version: Only one version of a deployment can be "live" (active) at any given time. 2. Version History: While you can't have multiple active versions, Prefect does maintain a history of previous versions, which allows you to: - Track changes in deployment configuration - Roll back to previous versions if needed - View the history of changes 3. Version Updates: When you create a new version of a deployment, it automatically becomes the live version, replacing the previously active version. 4. Rollback Capability: If you need to revert to a previous version, you can do so, but this makes that previous version the new "live" version - you still can't have multiple versions running simultaneously. This design ensures consistency in deployment execution while still maintaining the flexibility to track changes and roll back when necessary. For more detailed information, you can refer to: - Deployment versioning documentation - How to roll back deployments blog post Said with a slight sigh of resignation at the limitations of linear time: While it might seem convenient to have multiple versions active at once, the current system at least prevents the chaos of trying to determine which version should take precedence when scheduling runs.
g
@Marvin what would be the best way to rollout a new version of a flow just for A/B testing. If the new version is better, it can be promoted to new version, if not it can be deleted
m
thought for 69.1 seconds
Since we can't have multiple versions of the same deployment active simultaneously, here's a recommended approach for A/B testing flows: 1. Create Separate Deployments Instead of using versions, create two separate deployments from the same flow:
Copy code
python
   from prefect import flow
   
   @flow(name="my_process")
   def my_flow():
       # flow logic here
       pass
   
   # Create two deployments
   if __name__ == "__main__":
       # Production deployment
       my_flow.from_source(
           source="<https://github.com/your/repo>",
           entrypoint="flows/my_flow.py:my_flow"
       ).deploy(
           name="my-process-prod",
           tags=["production"]
       )
       
       # Test deployment (with potential modifications)
       my_flow.from_source(
           source="<https://github.com/your/repo>",
           entrypoint="flows/my_flow.py:my_flow",
           reference="experimental-branch"  # or different branch/commit
       ).deploy(
           name="my-process-test",
           tags=["test"]
       )
2. Control Traffic Split You can control the distribution of work between versions using one of these approaches: a. Using Flow Parameters:
Copy code
python
   @flow(name="router_flow")
   def router_flow(data, test_percentage=20):
       import random
       
       if random.randint(1, 100) <= test_percentage:
           # Call test version
           my_flow_test.submit(data)
       else:
           # Call production version
           my_flow_prod.submit(data)
b. Using Separate Schedules:
Copy code
python
   from datetime import timedelta
   from prefect.schedules import IntervalSchedule
   
   # Production gets majority of the schedule slots
   prod_schedule = IntervalSchedule(interval=timedelta(hours=1))
   
   # Test gets fewer slots
   test_schedule = IntervalSchedule(interval=timedelta(hours=4))
3. Monitoring and Comparison - Add metrics collection to both versions - Use tags to easily filter and compare results - Monitor both versions through the Prefect UI 4. Promotion Process When ready to promote the test version: a. If using the same code with different configurations: - Update the production deployment with the new configuration b. If using different code versions: - Deploy the new code to the production deployment - Archive or delete the test deployment 5. Rollback Plan - Keep the original deployment configuration documented - Use deployment versioning to quickly roll back if needed Here's a more complete example incorporating monitoring:
Copy code
python
from prefect import flow, task, metrics

@task
def collect_metrics(version: str, result):
    metrics.gauge(
        name="process_result",
        value=result,
        tags={"version": version}
    )

@flow(name="my_process")
def my_flow(version: str = "prod"):
    # Your flow logic here
    result = perform_work()
    
    # Collect metrics for comparison
    collect_metrics(version, result)
    
    return result

# Deploy both versions
if __name__ == "__main__":
    my_flow.from_source(
        source="<https://github.com/your/repo>",
        entrypoint="flows/my_flow.py:my_flow"
    ).deploy(
        name="my-process-prod",
        parameters={"version": "prod"},
        tags=["production"]
    )
    
    my_flow.from_source(
        source="<https://github.com/your/repo>",
        entrypoint="flows/my_flow.py:my_flow",
        reference="experimental-branch"
    ).deploy(
        name="my-process-test",
        parameters={"version": "test"},
        tags=["test"]
    )
With a hint of pride in the elegant solution: This approach gives you the flexibility of true A/B testing while maintaining clear separation between versions and easy rollback capabilities. The use of metrics and tags makes it straightforward to compare performance and make data-driven decisions about which version to promote.
Remember that you can monitor the results of both deployments in real-time through the Prefect UI, and the tagging system makes it easy to filter and compare results between versions.