Edge Flags: A Comprehensive Guide to Feature Flagging with Serverless Functions
Hi! I’m Mehmet Akar, a database enthusiast passionate about exploring cutting-edge technologies that make modern development faster, scalable, and efficient. Today, we’re diving into Edge Flags, a revolutionary feature flagging solution designed to run at the edge, leveraging serverless computing and global geolocation rules.
Feature flags let you dynamically change application behavior without redeploying. Whether for A/B testing, gradual rollouts, or fail-safe toggles, they’re essential for high-quality, user-focused applications. Let’s explore how Edge Flags, powered by Upstash Redis, can transform your development workflow.
Why Use Feature Flags?
Feature flags allow you to:
- Ship Safely: Release new features without worrying about breaking your application.
- Run Experiments: Easily conduct A/B tests or gradual rollouts to measure feature performance.
- Adapt Dynamically: Enable or disable functionality instantly in response to issues or user feedback.
Edge Flags take this concept further by bringing feature flagging to serverless architectures, enabling low-latency responses and seamless geolocation-based rules.
Setting Up Edge Flags: A Step-by-Step Guide
1. Create a Redis Database
First, log into the Upstash Console and create a new Redis database. Choose the Global
option for optimal read latency across regions.
Copy the UPSTASH_REDIS_REST_URL
and UPSTASH_REDIS_REST_TOKEN
credentials to your .env
file.
2. Install Edge Flags SDK
Install the @upstash/edge-flags
SDK in your project:
npm install @upstash/edge-flags
3. Configure the Edge Handler
Create an edge function in your application to handle feature flag evaluations. Here’s an example for Next.js:
import { createEdgeHandler } from "@upstash/edge-flags";
export default createEdgeHandler({
redisUrl: process.env.UPSTASH_REDIS_REST_URL!,
redisToken: process.env.UPSTASH_REDIS_REST_TOKEN!,
cacheMaxAge: 60, // Optional: cache duration in seconds
});
export const config = {
runtime: "edge",
};
This function retrieves geolocation and custom attributes from incoming requests to evaluate feature flags dynamically.
4. Add Flags via the Console
Go to the Edge Flags Console and:
- Create a new feature flag.
- Define rules, such as geolocation targeting or gradual rollouts (e.g., enabling a feature for 10% of users).
- Save the changes.
Advanced Use Case: Geolocation-Based Middleware
Edge Flags integrate seamlessly with Next.js middleware. Here’s an example of using geolocation rules to restrict access to users in the EU:
Create a flag (
eu-countries
) and define rules for EU countries in the console.Add a
middleware.ts
file to rewrite requests for non-EU users:
import { NextRequest, NextResponse } from "next/server";
import { Client as EdgeFlags } from "@upstash/edge-flags";
const edgeFlags = new EdgeFlags({
redisUrl: process.env.UPSTASH_REDIS_REST_URL!,
redisToken: process.env.UPSTASH_REDIS_REST_TOKEN!,
});
export default async function middleware(req: NextRequest) {
const enabled = await edgeFlags.getFlag("eu-countries", req.geo ?? {});
if (!enabled) {
const url = new URL(req.url);
url.pathname = "/blocked";
return NextResponse.rewrite(url);
}
return NextResponse.next();
}
export const config = {
matcher: "/",
};
- Create a
/blocked
page for users outside the EU:
export default function Blocked() {
return <div>Access restricted: This content is only available in the EU.</div>;
}
- Deploy the application on Vercel or another edge-capable platform.
Best Practices for Using Edge Flags
-
Cache Smartly:
- Use the
cacheMaxAge
parameter to reduce Redis queries and improve performance. - Evaluate caching trade-offs: longer cache times reduce costs but delay flag updates.
- Use the
-
Start with Small Rollouts:
- Gradually enable features for a subset of users to monitor performance and feedback.
-
Use Geolocation Rules:
- Target users by region, country, or city for localized feature rollouts.
-
Leverage A/B Testing:
- Create multiple flags to compare feature performance across user segments.
Why Choose Edge Flags?
Edge Flags, built on Upstash Redis, offers:
- Serverless Scalability: No infrastructure management; only pay for what you use.
- Low-Latency Access: Globally distributed Redis ensures fast data retrieval.
- Developer-Friendly SDK: Easy integration with frameworks like Next.js.
- Geolocation and Custom Rules: Advanced targeting capabilities out of the box.
For a detailed overview, check out the official documentation.
Edge Flags Conclusion
Feature flags are indispensable for modern development, and Edge Flags make them even better by bringing the power of serverless computing and edge capabilities. Whether you’re rolling out features gradually or running A/B tests, Edge Flags can simplify your workflow and improve user experience.
Ready to get started? Head over to the Edge Flags Console to create your first flag today!
Top comments (0)