Skip to content

Commit d12f16f

Browse files
guiwritehtessaro
andauthored
docs: Performance section (#5880)
Co-authored-by: htessaro <heitor@writechoice.io>
1 parent a4450e6 commit d12f16f

4 files changed

Lines changed: 319 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"label": "Performance",
3+
"position": 80,
4+
"collapsed": true
5+
}

docs/docs/performance/edge-api.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
---
2+
title: Edge API
3+
sidebar_label: Edge API
4+
sidebar_position: 30
5+
---
6+
7+
If you are a Flagsmith SaaS customer, you are using our Edge API by default.
8+
9+
[The Flagsmith Architecture](/clients#remote-evaluation) is based around a server-side flag engine. This comes with a
10+
number of benefits, but it can increase latency, especially when the calls are being made from a location that is far
11+
from the EU; the location of our current API. It also provides a single point of failure in the event of an AWS
12+
region-wide outage.
13+
14+
The Edge API solves both of these issues. It provides a datastore and Edge compute API that is replicated across 8 AWS
15+
regions, with latency-based routing and global failover in the event of a region outage.
16+
17+
The Edge API provides API service from the following AWS regions:
18+
19+
- Europe (London) - `eu-west-2`
20+
- US East (Ohio) - `us-east-2`
21+
- US West (N. California) - `us-west-1`
22+
- Asia Pacific (Mumbai) - `ap-south-1`
23+
- Asia Pacific (Sydney) - `ap-southeast-2`
24+
- South America (São Paulo) - `sa-east-1`
25+
- Asia Pacific (Seoul) - `ap-northeast-2`
26+
- Asia Pacific (Singapore) - `ap-southeast-1`
27+
28+
## Enabling the Edge API
29+
30+
:::tip
31+
32+
Existing organisations created before **7th June 2022** have their projects deployed to the Core API. You can migrate your project over to our Edge API by going to the **Project Settings** page and hitting the **Start Migration** button. The migration will take between 1 minute and an hour depending on how many identities your project has.
33+
34+
The Core API will continue to work normally during and following the migration. See the
35+
[Migration Steps](#migration-steps) for more info.
36+
37+
:::
38+
39+
Once you have had your projects migrated to Edge, all you will need to do is point your SDK to a new Flagsmith Edge API
40+
URL at `edge.api.flagsmith.com`. This domain points to our Edge CDN. That's it!
41+
42+
The easiest way to do this is to upgrade to the latest version of the Flagsmith SDK for your language.
43+
44+
If you are unable to upgrade, you can manually point the existing SDK to the Edge API endpoint. So for example, in the
45+
Java SDK we just add the `withApiUrl` line:
46+
47+
```java
48+
FlagsmithClient flagsmithClient = FlagsmithClient.newBuilder()
49+
.setApiKey("aaa"))
50+
.withApiUrl("https://edge.api.flagsmith.com/api/v1/")
51+
.build();
52+
```
53+
54+
Note that the Edge API URL is: `https://edge.api.flagsmith.com/api/v1/`.
55+
56+
Check the docs for your language SDK on how to override the endpoint URL prefix.
57+
58+
## Migration Steps
59+
60+
The migration process will carry out a one-way sync of your `identity` data, from the Core API to the Edge API. All your `identity` data will continue to exist within the Core API, and you can continue to write `identities` to the Core API if you wish.
61+
62+
The goal is to get all of your applications running against the Edge API, where you will benefit from global low latency as well as multi-region failover and fault tolerance.
63+
64+
### Step 1 - Prepare your applications
65+
66+
Set your applications up to point to the Flagsmith Edge API. This means going from `api.flagsmith.com` to `edge.api.flagsmith.com`. You can either set this explicitly in our SDK or just ensure you are running the latest version of the SDK; by default the _latest version_ of the SDKs will point to `edge.api.flagsmith.com`.
67+
68+
### Step 2 - Migrate your data
69+
70+
You can now trigger a one-way sync of data for each of your Flagsmith projects within the Flagsmith Dashboard. You can migrate your project over to our edge API by going to the **Project Settings** page and hitting the **Start Migration** button. This will start a job that can take between 1 minute and 1 hour, depending on how much data you have. Once the job is complete, all the identities that were present in your Core API will be present in the Edge API.
71+
72+
The Core API will continue to work normally during and following the migration.
73+
74+
:::caution
75+
76+
As of Dec 1st, 2023, we will no longer be replicating identities from the Core API to our Edge API. Please ensure your applications are fully migrated before this point in time.
77+
78+
:::
79+
80+
If you have a product like a mobile app, where you cannot immediately force your users to upgrade (as opposed to a web app, for example), you will likely generate identity writes to the old Core API.
81+
82+
Following and during the migration, if we receive a request to an `identity` endpoint that results in a write to the Core API, we will persist the data in the Core API _and replay the request into the Edge API_. You can then update your API endpoints/SDKs in your own time to gradually move over the Edge API. This will give you time to migrate your users over to the new version of your application.
83+
84+
Note that writes to the Core API will still work into the future, but the data will not be synchronised across the two platforms (Core and Edge).
85+
86+
### Step 3 - Deploy your applications
87+
88+
Once your data has been copied onto the Edge API datastore, you can now deploy your applications that point to the new endpoint `edge.api.flagsmith.com` and benefit from global low latency!
89+
90+
## Things you should know
91+
92+
### Some of the Secure Identity Endpoints Have Changed
93+
94+
If you are using our REST API to manipulate/update/list your identities, some of these endpoints have changed. Please get in touch if you need any help related to this.
95+
96+
### New Identity Overrides Will Only Apply to the Edge API
97+
98+
After the migration, any new identity overrides you apply to feature flag values for specific identities will only be applied to the Edge API.
99+
100+
### Increment and Decrement endpoints are deprecated
101+
102+
You probably didn't know these existed though, right?
103+
104+
### Bulk Trait endpoint is deprecated
105+
106+
But you can still achieve the same functionality using our POST /identities endpoints.
107+
108+
### The API responses have been slimmed down
109+
110+
Our core API responses are quite verbose, and the SDKs ignore a lot of the fields they receive. We've taken the
111+
opportunity to remove these additional, unused fields. This wont affect the SDKs but if you are using these values via
112+
the REST API, things have changed. The list of removed fields is as follows:
113+
114+
```txt
115+
trait.id
116+
flag.feature.created_data
117+
flag.feature.description
118+
flag.feature.initial_value
119+
flag.feature.default_enabled
120+
flag.environment
121+
flag.identity
122+
flag.feature_segment
123+
```
124+
125+
## Architecture
126+
127+
You can see our SaaS architecture [here](/system-administration/architecture.md#saas).
128+
129+
## How It Works
130+
131+
### Lambda@Edge
132+
133+
Our core [Rules Engine](https://github.com/Flagsmith/flagsmith-engine) has been factored out of our REST API. This
134+
allows us to use it as a dependency within both the Flagsmith API, but also within a set of Lambda functions that
135+
service SDK API calls. You can point your SDK clients to our global CDN `edge.api.flagsmith.com` which will serve your
136+
request using a Lambda function running in an AWS data-centre near your client. This is how we reduce latency!
137+
138+
### DynamoDB Global Tables
139+
140+
We store state within our API - both related to the environments for your projects, but also for the identities within those environments. Our Edge design sees us write this data through to DynamoDB global tables, which are replicated globally. Our Lambda functions then connect to the nearest DynamoDB table to retrieve both environment and identity data.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: Edge Proxy
3+
sidebar_label: Edge Proxy
4+
sidebar_position: 10
5+
---
6+
7+
The Flagsmith Edge Proxy is a service that you host yourself, that allows you to run an instance of the Flagsmith Engine close to your servers. If you are running Flagsmith within a server-side environment and you want to have very low latency flags, you have two options:
8+
9+
1. Run the Edge Proxy within in your own infrastructure and connect to it from your server-side SDKs
10+
2. Run your server-side SDKs in [Local Evaluation Mode](/clients#local-evaluation).
11+
12+
The main benefit to running the Edge Proxy is that you reduce your polling requests against the Flagsmith API itself.
13+
14+
The main benefit to running server side SDKs in [Local Evaluation Mode](/clients#local-evaluation) is that you get the lowest possible latency.
15+
16+
## How does it work
17+
18+
:::info
19+
20+
The Edge Proxy has the same [caveats as running our SDK in Local Evaluation mode.](/clients/#local-evaluation).
21+
22+
:::
23+
24+
You can think of the Edge Proxy as a copy of our Python Server Side SDK, running in [Local Evaluation Mode](/clients#local-evaluation), with an API interface that is compatible with the Flagsmith SDK API.
25+
26+
The Edge Proxy runs as a lightweight Docker container. It connects to the Flagsmith API (either powered by us at `api.flagsmith.com` or self hosted by you) to get environment flags and segment rules. You can then point the Flagsmith SDKs to your Edge Proxy; it implements all the current SDK endpoints. This means you can serve a very large number of requests close to your infrastructure and users, at very low latency. Check out the [architecture below](#architecture).
27+
28+
The proxy also acts as a local cache, allowing you to make requests to the proxy without hitting the Core API.
29+
30+
## Performance
31+
32+
The edge proxy can currently serve ~2,000 requests per second (RPS) at a mean latency of ~7ms on an M1 MacBook Pro with a simple set of feature flags. Working with more complex environments with many segment rules will bring this RPS number down.
33+
34+
It is stateless and hence close to perfectly scalable being deployed behind a load balancer.
35+
36+
## Managing Traits
37+
38+
There is one caveat with the Edge Proxy. Because it is entirely stateless, it is not able to persist trait data into any sort of datastore. This means that you _have_ to provide the full complement of traits when requesting the flags for a particular identity. Our SDKs all provide relevant methods to achieve this. An example using `curl` would read as follows:
39+
40+
```bash
41+
curl -X "POST" "http://localhost:8000/api/v1/identities/?identifier=do_it_all_in_one_go_identity" \
42+
-H 'X-Environment-Key: n9fbf9h3v4fFgH3U3ngWhb' \
43+
-H 'Content-Type: application/json; charset=utf-8' \
44+
-d $'{
45+
"traits": [
46+
{
47+
"trait_value": 123.5,
48+
"trait_key": "my_trait_key"
49+
},
50+
{
51+
"trait_value": true,
52+
"trait_key": "my_other_key"
53+
}
54+
],
55+
"identifier": "do_it_all_in_one_go_identity"
56+
}'
57+
```
58+
59+
Note that the Edge Proxy will currently _not_ send the Trait data back to the Core API.
60+
61+
## Deployment and Configuration
62+
63+
Please see the [hosting documentation](/deployment/hosting/locally-edge-proxy).
64+
65+
## Architecture
66+
67+
The standard Flagsmith architecture:
68+
69+
![Image](/img/edge-proxy-existing.svg)
70+
71+
With the proxy added to the mix:
72+
73+
![Image](/img/edge-proxy-proxy.svg)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
title: Real-time flag updates
3+
sidebar_label: Real-time Flags
4+
sidebar_position: 20
5+
---
6+
7+
When an application fetches its current feature flags, it usually caches the flags for a certain amount of time to make [efficient use](/guides-and-examples/efficient-api-usage) of the Flagsmith API and network resources. In some cases, you may want an application to be notified about feature flag updates without needing to repeatedly call the Flagsmith API. This guide explains how to achieve this by subscribing to real-time flag updates.
8+
9+
## Prerequisites
10+
11+
- Real-time flag updates require an Enterprise subscription.
12+
- Real-time flag updates are only available on the public SaaS Flagsmith instance. Self-hosted and private cloud Flagsmith installations do not support real-time flag updates.
13+
14+
## Setup
15+
16+
To enable real-time flag updates for your Flagsmith project:
17+
18+
1. Log in to the Flagsmith dashboard as a user with project administrator permissions.
19+
2. Navigate to **Project Settings > SDK Settings**.
20+
3. Enable **Real-time updates**.
21+
22+
By default, applications using a supported Flagsmith SDK do not subscribe to real-time flag updates. Refer to your SDK's documentation for subscribing to real-time flag updates.
23+
24+
## How it works
25+
26+
The following sequence diagram shows how a typical application would use real-time flag updates. [Billable API requests](/billing) are highlighted in yellow.
27+
28+
```mermaid
29+
sequenceDiagram
30+
rect rgb(255,245,173)
31+
Application->>Flagsmith: Fetch initial flags
32+
Flagsmith->>Application: #nbsp
33+
end
34+
Application->>Flagsmith: Connect to update stream
35+
Flagsmith->>Application: #nbsp
36+
Flagsmith Administrator->>Flagsmith: Update flag state
37+
Flagsmith->>Flagsmith Administrator: #nbsp
38+
Flagsmith-->>Application: Flag update event
39+
rect rgb(255,245,173)
40+
Application->>Flagsmith: Fetch latest flags
41+
Flagsmith->>Application: #nbsp
42+
end
43+
Application-->Application: Store latest update timestamp
44+
```
45+
46+
Your application subscribes to real-time flag updates by opening a long-lived [server-sent events (SSE)](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) connection to Flagsmith, which is specific to its current environment.
47+
48+
When the environment is updated in some way, either via the Flagsmith dashboard or the [Admin API](/clients/rest#private-admin-api-endpoints), all clients connected to that environment's real-time stream will receive a message containing the latest update's timestamp. If your application's latest flags are older than the received timestamp, it requests the latest flags from Flagsmith. When your application receives the latest flags, you must propagate the latest flag state throughout your application as necessary.
49+
50+
## Limitations
51+
52+
Real-time flag update events only contain a timestamp indicating when any flag in the environment was last updated. Applications must still call the Flagsmith API to get the actual flags for their current environment or user.
53+
54+
Only changes made to environments or projects result in flag update events. For example, the following operations will cause updates to be sent:
55+
56+
- Manually toggling a flag on or off, or changing its value.
57+
- A [scheduled Change Request](/advanced-use/scheduled-flags) for a feature goes live.
58+
- Creating or updating segment overrides for a feature.
59+
- Changing a segment definition.
60+
61+
Identity-level operations _will not_ cause updates to be sent:
62+
63+
- Updating an identity's traits.
64+
- Creating or updating an identity override.
65+
66+
The following SDK clients support subscribing to real-time flag updates:
67+
68+
- JavaScript
69+
- Android
70+
- iOS
71+
- Flutter
72+
- Python
73+
- Ruby
74+
75+
## Implementation details
76+
77+
The event source URL used by Flagsmith SDKs is:
78+
79+
```
80+
https://realtime.flagsmith.com/sse/environments/ENVIRONMENT_ID/stream
81+
```
82+
83+
Each real-time flag event message is a JSON object containing a Unix epoch timestamp of the environment's last update:
84+
85+
```json
86+
{
87+
"updated_at": 3133690620000
88+
}
89+
```
90+
91+
You can test real-time flag updates by using cURL to connect to the event source URL:
92+
93+
```
94+
curl -H 'Accept: text/event-stream' -N -i https://realtime.flagsmith.com/sse/environments/ENVIRONMENT_ID/stream
95+
```
96+
97+
## What's next?
98+
99+
- [Efficient API Usage](/guides-and-examples/efficient-api-usage) – Tips for reducing API calls and making the most of your integration.
100+
- [Edge Proxy](/performance/edge-proxy) – Run the Flagsmith engine closer to your infrastructure for even lower latency.
101+
- [Integration Approaches](/guides-and-examples/integration-approaches) – Explore different ways to integrate Flagsmith into your applications.

0 commit comments

Comments
 (0)