Documentation Index
Fetch the complete documentation index at: https://launchdarkly-preview.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Overview
High-traffic events, such as product launches or promotional campaigns, can strain an application’s infrastructure, leading to degraded performance or downtime. Implementing kill switch flags using LaunchDarkly allows you to gracefully degrade non-essential features, ensuring core functionalities remain available. In this tutorial, we’ll explore how to integrate LaunchDarkly kill switch flags into a Python application to manage traffic effectively during peak events. This project will show a sample social media feed that makes calls to a 3rd party API for unique profile avatars and show personalized recommendations. However, if the developer has to toggle the kill switch, then the social media feed will fall back to default avatar images and not serve personalized recommendations. A WebSocket is required to send the flag value from the server to the client side browser.Tutorial requirements
- Python 3.6 or newer. If your operating system does not provide a Python interpreter, you can go to python.org to download an installer.
- Visual Studio Code or your favorite IDE.
- LaunchDarkly account. If you haven’t done so already, create a free account.
Set Up the Environment
Clone the repository and switch over to the “kill-switch-event-starter” branch to follow along in this tutorial.Create and Activate a Virtual Environment
Run the following commands in your terminal to clone the repository with starter code, install dependencies, and start the server.
Let’s integrate the LaunchDarkly SDK so that we can see the power of kill switches in action.
Set up the developer environment for the Python application
Make sure that you are currently in the virtual environment of your project’s directory in the terminal or command prompt. Create a file at the root of your project folder named .env and add the following lines to define the environment variables. These are placeholder values, which you’ll be updating later with real ones. LAUNCHDARKLY_SDK_KEY=“sdk-###############“Configure your Launch
Darkly kill switch flag In the LaunchDarkly app, click on “Flags” in the left navigation menu and then click one of the Create flag buttons.
Configure your flag as follows:
- Name your flag “show-avatars-and-reccs”. When you type in the Name, the key will automatically populate.
- Enter some text in the description field to explain the purpose of this flag: “Shut off calls to the 3rd-party API that generates avatars.”
- Click No template and select “Kill switch” as the flag template. Selecting this option will automatically set up the flag variations for you.
Go to the app.py file and change the following line to match the name of the newly created flag key:
flag_key = "replace-key-here"
Test the kill switch flag
It’s time to initialize and make calls to the LaunchDarkly Python SDK. In the index route, uncomment the lines of code that creates theshow_avatar function. This function will retrieve feature flag values from the LaunchDarkly client and determine the actions based on the flag logic.
The sample lines of code defining the user “Sandy” are no longer needed, so remove them completely.
Make sure that the index route looks like the code below:
Great! The social media feed is operating successfully as if everything is normal and there is no need to cut off any resources.
However, the magic of kill switches can only be appreciated if you don’t have to click refresh on the page. Your users should also not be required to click refresh in order to continue enjoying the application.
Add a websocket to the Python application
WebSockets need to be implemented to enable real-time two-way communication between the server and client-side browser. SocketIO is a great Python library that can get the job done quickly with low latency. Without this library, the browser would not know about the flag changes and the user would have to manually refresh the page, which is a slow process and will not truly demonstrate the power of a kill switch flag. To have your website update immediately when a LaunchDarkly flag changes, you need to implement SocketIO as the real-time update mechanism between your back end and front end. The front end will wait and listen for the flag value changes in order to update the UI accordingly. In your terminal, run the following command to install the library.pip install flask-socketio
Navigate to the app.py file and add the following import statements to the top of the file:
show_evaluation_result function, add a class named FlagValueChangeListener to handle the values from the WebSocket:
Add the socket to the front end
At this point, you’ll need to add some code so the browser can listen for the event using SocketIO’s JavaScript client. Add the following JavaScript to the head tag in the index.html file in the templates folder:flag_update event via the SocketIO library.
The front end listens for this event and reloads the page automatically, so users see the updated content immediately without a manual refresh needed.
Navigate back to LaunchDarkly and disable your flag. Click Review and save again as you did previously when you enabled it. This is the process you will use if you have to quickly kill external API calls. No more wasting resources when one part of your application is failing and you need to act fast!
What’s next for building with kill switches and Web
Sockets? In this post you’ve learned how to use the LaunchDarkly Python SDK to add kill switch flags and WebSockets to your Flask web application. If you want to learn more about what you can do with kill switch flags, check out the following articles:- Add LaunchDarkly kill switches to a FastAPI app
- Quickly disable external API calls in your Sinatra application sing LaunchDarkly kill switch flags
- Implement Kill Switch Flags to a Spring Boot Application