Hi guys, I'm processing multi-tenant events with a...
# troubleshooting
m
Hi guys, I'm processing multi-tenant events with a single queue. I have some configuration flags per tenant which are saved in postgres. What would be a good solution to load those configurations into flink runtime? I thought about using broadcast but not sure if it's the intended purpose
t
Hi Matan, Let me just understand the flow, Stream records consumed from a single queue containing event per tenant, i.e TenantId, EventData and you need to get the configuration per tenant to do more work where the configurations are stored in postgres? is that the case?
m
yes, correct
t
What I suggest is to use Async IO operator you can connect Source -> TenantId, EventData --> Async IO operator
Copy code
public class AsyncTenantConfigOperator extends RichAsyncFunction<Tuple2String,EventData, Tuple2EventData, TenantData> {


   @Override
    public void asyncInvoke(final Tuple2String, EventData input,
            final ResultFuture<Tuple2EventData, TenantData> resultFuture) throws Exception {
        .
        .
        .
    }
}
in that operator you can use postgress client to fetch configuration based on the tenant id and forward a new stream record where each configuration is attached to its tenant. the output can be something like Tuple2EventData, TenantData where TenantData includes tenant id + configuration or alternatively Tuple3 TenantId, EventData, TenantConfig. 1. You better cache the configurations (tenantId, TenantConfig) 2. you can create a config provider which handles the cache and the client(i.e TenantConfigProvider). then just expose a method getTenantConfig(tenantId) 3. Keep in mind the parallelism of that instance + each client maintains connection pool internally. (The cache helps here to avoid overwhelming the external system) Alternatively, if you have a service that maintains these configurations (i.e TenantConfigService) you can communicate with that service instead via REST client and not directly with the DB.(Same idea) https://nightlies.apache.org/flink/flink-docs-master/docs/dev/datastream/operators/asyncio/
πŸ‘€ 1
m
Thanks! I thought maybe Flink has a mechanism to implement
TenantConfigProvider
which will provide built-in cache handling and such, but seems like I just need plain old java
πŸ’― 1
t
You can store the config in the operator state if needed then you check if the config exists in state before pulling it, yet keep in mind that storing in state may impact the checkpoint process and the state size(depends how complex is the config object). The idea of the provider is a class that handles both the cache and fetching the config. it may be used elsewhere(outside Flink also). in Flink, you use such object to get the config per tenant in AsyncIO Operator.