perf: accelerate the creation of the consumer cache#11840
Conversation
Signed-off-by: xuruidong <[email protected]>
|
To purge local cache need to restart apisix? |
Signed-off-by: xuruidong <[email protected]>
|
ping @membphis |
| } | ||
| end | ||
|
|
||
| local cached_consumer = consumers_id_cache[val.value.id] |
There was a problem hiding this comment.
I do not know why we have to add consumers_id_cache
it seems useless
There was a problem hiding this comment.
The purpose of using consumer_id_cache is to avoid unnecessary table copies. I will refactor this part of the code to replace the table with an LRU cache.
yes |
Signed-off-by: xuruidong <[email protected]>
|
The code introduces a variable When the number of consumers exceeds twice the size of the LRU cache, the process of creating cache during the execution of the 代码中引入了一个变量 consumers_count_for_lrucache , 用来设置 lrucache 的大小。实际使用的时候,这个值需要配置为大于 consumer 的实际数量,并且如果一个consumer 中存在多个认证插件,则会被认为是不同的consumer 缓存到 lrucache 中。 当 consumer 数量大于2倍的lrucache 大小的时候,在执行 |
|
ping @membphis |
membphis
left a comment
There was a problem hiding this comment.
I will check it carefully next week, I am busy this week
| }) | ||
|
|
||
| local function fill_consumer_secret(consumer) | ||
| consumer.auth_conf = secret.fetch_secrets(consumer.auth_conf, false) |
There was a problem hiding this comment.
it is not safe to update the original consumer object.
more safe way:
local new_consumer = table.clone(consumer)
new_consumer.auth_conf = secret.fetch_secrets(consumer.auth_conf, false)
return new_consumer|
many thx @xuruidong 's contribution, merged your PR right now |
| if consumer.labels then | ||
| consumer.custom_id = consumer.labels["custom_id"] | ||
| local consumer = consumers_id_lrucache(val.value.id .. name, | ||
| val.modifiedIndex, construct_consumer_data, val, config) |
There was a problem hiding this comment.
We cannot cache the result of construct_consumer_data here, because even when there is no change in credential (val.modifiedIndex has not changed), the consumer might have been modified. We need to design different versions for consumer and credential.


Description
In my case, there are approximately 17,000+ consumers. Any update from a consumer will trigger a long-tail request. I found the methods
consumer.pluginandconsumer.consumers_kvcost too much time.Test steps
Update one consumer, send a request. Repeat 5 times.
Test result:
Before optimization:
unit: second
After optimization:
unit: second
Fixes # (issue)
Checklist