• Product
  • Pricing
  • Docs
  • Using PostHog
  • Community
  • Company
  • Login
  • Table of contents

  • Handbook
  • Getting started

    • Start here
    • Meetings
    • Story
    • Team
    • Investors
    • Strategy overview
    • Business model
    • Objectives
    • Roadmap
    • Brand
    • Culture
    • Values
    • Goal setting
    • Diversity and inclusion
    • Communication
    • Management
    • Offsites
    • Security
    • Brand assets
      • Team structure
      • Why Small Teams
      • Team App East
      • Team App West
      • Team Platform
      • Team Ingestion
      • Team Infrastructure
      • Team Marketing
      • Team Website and Docs
      • Team People and Ops
      • Team Customer Success
    • Compensation
    • Share options
    • Benefits
    • Time off
    • Spending money
    • Progression
    • Training
    • Feedback
    • Onboarding
    • Offboarding
      • Product Manager ramp up
    • Merch store
      • Overview
      • Engineering hiring
      • Marketing hiring
      • Operations hiring
      • Design hiring
      • Exec hiring
      • Developing locally
      • Tech stack
      • Project structure
      • How we review PRs
      • Frontend coding
      • Backend coding
      • Support hero
      • Feature ownership
      • Releasing a new version
      • Bug prioritization
      • Event ingestion explained
      • Making schema changes safely
      • How to optimize queries
      • How to write an async migration
      • How to run migrations on PostHog Cloud
      • Working with ClickHouse materialized columns
      • Deployments support
      • Working with cloud providers
      • Breaking glass to debug PostHog Cloud
      • Developing the website
      • MDX setup
    • Shipping things, step by step
    • Feature flags specification
    • Setting up SSL locally
    • Tech talks
  • Product

    • Overview
    • Product metrics
    • User feedback
    • Scale features prioritization
    • Paid features
    • Releasing as beta
  • Design

    • Overview
    • Overview
    • Personas
    • Testimonials
    • Value propositions
      • Content & SEO
      • Sponsorship
      • Paid ads
      • Email
      • Press
    • Growth strategy
    • Customer support
    • Inbound sales model
    • Sales operations
      • Managing our CRM
      • YC onboarding
      • Demos
      • Billing
      • Who we do business with
  • Table of contents

  • Handbook
  • Getting started

    • Start here
    • Meetings
    • Story
    • Team
    • Investors
    • Strategy overview
    • Business model
    • Objectives
    • Roadmap
    • Brand
    • Culture
    • Values
    • Goal setting
    • Diversity and inclusion
    • Communication
    • Management
    • Offsites
    • Security
    • Brand assets
      • Team structure
      • Why Small Teams
      • Team App East
      • Team App West
      • Team Platform
      • Team Ingestion
      • Team Infrastructure
      • Team Marketing
      • Team Website and Docs
      • Team People and Ops
      • Team Customer Success
    • Compensation
    • Share options
    • Benefits
    • Time off
    • Spending money
    • Progression
    • Training
    • Feedback
    • Onboarding
    • Offboarding
      • Product Manager ramp up
    • Merch store
      • Overview
      • Engineering hiring
      • Marketing hiring
      • Operations hiring
      • Design hiring
      • Exec hiring
      • Developing locally
      • Tech stack
      • Project structure
      • How we review PRs
      • Frontend coding
      • Backend coding
      • Support hero
      • Feature ownership
      • Releasing a new version
      • Bug prioritization
      • Event ingestion explained
      • Making schema changes safely
      • How to optimize queries
      • How to write an async migration
      • How to run migrations on PostHog Cloud
      • Working with ClickHouse materialized columns
      • Deployments support
      • Working with cloud providers
      • Breaking glass to debug PostHog Cloud
      • Developing the website
      • MDX setup
    • Shipping things, step by step
    • Feature flags specification
    • Setting up SSL locally
    • Tech talks
  • Product

    • Overview
    • Product metrics
    • User feedback
    • Scale features prioritization
    • Paid features
    • Releasing as beta
  • Design

    • Overview
    • Overview
    • Personas
    • Testimonials
    • Value propositions
      • Content & SEO
      • Sponsorship
      • Paid ads
      • Email
      • Press
    • Growth strategy
    • Customer support
    • Inbound sales model
    • Sales operations
      • Managing our CRM
      • YC onboarding
      • Demos
      • Billing
      • Who we do business with
  • Handbook
  • Engineering
  • Feature flags specification

Feature flags specification

Last updated: Mar 15, 2022

On this page

  • A note: simple flags
  • Pseudocode Implementation
  • Ensuring consistency with isSimpleFlagEnabled

Client-side PostHog libraries (like posthog-js) are able to have a rather simple feature flags implementation, given that they only ever have to deal with one user. As such, we can make a request at the start of a session to fetch feature flags that are enabled for a user, keep those on memory, and periodically check for updates.

On server-side libraries, however, we need to be able to handle n distinct IDs. As such, we are unable to fetch user-specific flags proactively, since there is an infinite number of possible distinct IDs that one can call isFeatureEnabled with. As a result, we've developed an approach to implementing feature flags on our server-side libraries, described in this doc.

A note: simple flags

To improve performance of feature flag methods on the server, all PostHog feature flags have a property is_simple_flag. A simple flag is one that does not rely on any filters, so calculating if it is on for a given user or not depends entirely on two things: rollout percentage and user distinct ID.

Distinct ID is passed in by the user, so all we need is the rollout percentage to determine if a flag is on without having to offload the calculation to the PostHog instance.

Pseudocode Implementation

class FeatureFlagsPoller:
constructor():
# Check if there's a Personal API Key available in addition to the Project API Key
poll() -> void:
# 1. Set up a poller to fetch a list of available flags from api/feature_flag using the Personal API Key
# 2. Store the result
isFeatureEnabled(key: string, distinctId: string, defaultValue: boolean) -> boolean:
flag = flagObjectOrNull(key)
if !flag:
return defaultValue
result = defaultValue
if flag['is_simple_flag']:
result = isSimpleFlagEnabled(key, distinctId, flag['rollout_percentage'])
else:
# Send a POST request to /decide passing the user distinct_id in the request data
# Authenticate with the Project API Key
response = post('/decide/', data={ 'distinct_id': distinctId })
result = key in response.data.featureFlags
# Capture a posthog event called $feature_flag_called, passing the properties $feature_flag (the key) and $feature_flag_response
client.capture('$feature_flag_called', { '$feature_flag': key, '$feature_flag_response': result })
return result
isSimpleFlagEnabled(key: string, distinctId: string, rolloutPercentage: int) -> boolean:
if (!rolloutPercentage):
return true
# 1. Get the SHA1 hash of the a string in the following format: key.distinctId
# 2. Convert it to hex format and take the first 15 chars
# 3. Cast it to an integer from base16
# 4. Divide it by the following hexadecimal number: 0xfffffffffffffff (1152921504606847000)
# 5. Return: (hashToInteger / 0xfffffffffffffff) <= (rolloutPercentage / 100)

Ensuring consistency with isSimpleFlagEnabled

To check if your isSimpleFlag implementation is in accordance with others, the following should be true:

  1. SHA1('a.b') should equal '69f6642c9d71b463485b4faf4e989dc3fe77a8c6'
  2. The floating point value for the hash of 'a.b' should equal 0.4139158829615955

Questions?

Was this page useful?

Next article

Setting up SSL locally

Setting up HTTPS locally can be useful if you're trying to debug hard to replicate issues (e.g cross domain cookies, etc). There are two ways you can get HTTPS locally: ngrok NGINX and a local certificate. The easiest option is to use ngrok. Set up SSL via ngrok Make sure you have ngrok installed . Sign up for an ngrok account (or sign in with GitHub) and run ngrok authtoken <TOKEN> Edit $HOME/.ngrok2/ngrok.yml and add the following after the line with authtoken: <TOKEN> : Start ngrok…

Read next article

Authors

  • Michael Matloka
    Michael Matloka
  • Yakko Majuri
    Yakko Majuri

Share

Jump to:

  • A note: simple flags
  • Pseudocode Implementation
  • Ensuring consistency with isSimpleFlagEnabled
  • Questions?
  • Edit this page
  • Raise an issue
  • Toggle content width
  • Toggle dark mode
  • About
  • Blog
  • Newsletter
  • Careers
  • Support
  • Contact sales

Product OS suite

Product overview

Analytics
  • Funnels
  • Trends
  • Paths

Pricing

Features
  • Session recording
  • Feature flags
  • Experimentation
  • Heatmaps

Customers

Platform
  • Correlation analysis
  • Collaboration
  • Apps

Community

Discussion
  • Questions?
  • Slack
  • Issues
  • Contact sales
Get involved
  • Roadmap
  • Contributors
  • Merch
  • PostHog FM
  • Marketplace

Docs

Getting started
  • PostHog Cloud
  • Self-hosted
  • Compare options
  • Tutorials
  • PostHog on GitHub
Install & integrate
  • Installation
  • Docs
  • API
  • Apps
User guides
  • Cohorts
  • Funnels
  • Sessions
  • Data
  • Events

Company

About
  • Our story
  • Team
  • Handbook
  • Investors
  • Careers
Resources
  • FAQ
  • Ask a question
  • Blog
  • Press
  • Merch
  • YouTube
© 2022 PostHog, Inc.
  • Code of conduct
  • Privacy
  • Terms