Edge Flags

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 …


This content originally appeared on DEV Community and was authored by mehmet akar

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:

  1. Create a new feature flag.
  2. Define rules, such as geolocation targeting or gradual rollouts (e.g., enabling a feature for 10% of users).
  3. 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:

  1. Create a flag (eu-countries) and define rules for EU countries in the console.

  2. 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: "/",
};
  1. 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>;
}
  1. Deploy the application on Vercel or another edge-capable platform.

Best Practices for Using Edge Flags

  1. 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.
  2. Start with Small Rollouts:

    • Gradually enable features for a subset of users to monitor performance and feedback.
  3. Use Geolocation Rules:

    • Target users by region, country, or city for localized feature rollouts.
  4. 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!


This content originally appeared on DEV Community and was authored by mehmet akar


Print Share Comment Cite Upload Translate Updates
APA

mehmet akar | Sciencx (2025-01-24T14:46:21+00:00) Edge Flags. Retrieved from https://www.scien.cx/2025/01/24/edge-flags/

MLA
" » Edge Flags." mehmet akar | Sciencx - Friday January 24, 2025, https://www.scien.cx/2025/01/24/edge-flags/
HARVARD
mehmet akar | Sciencx Friday January 24, 2025 » Edge Flags., viewed ,<https://www.scien.cx/2025/01/24/edge-flags/>
VANCOUVER
mehmet akar | Sciencx - » Edge Flags. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/24/edge-flags/
CHICAGO
" » Edge Flags." mehmet akar | Sciencx - Accessed . https://www.scien.cx/2025/01/24/edge-flags/
IEEE
" » Edge Flags." mehmet akar | Sciencx [Online]. Available: https://www.scien.cx/2025/01/24/edge-flags/. [Accessed: ]
rf:citation
» Edge Flags | mehmet akar | Sciencx | https://www.scien.cx/2025/01/24/edge-flags/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.