Plugin Development - Caching Custom Entities

Your plugin may need to frequently access custom entities (explained in the previous chapter) on every request and/or response. Usually, loading them once and caching them in-memory dramatically improves the performance while making sure the datastore is not stressed with an increased load.

Think of an api-key authentication plugin that needs to validate the api-key on every request, thus loading the custom credential object from the datastore on every request. When the client provides an api-key along with the request, normally you would query the datastore to check if that key exists, and then either block the request or retrieve the Consumer ID to identify the user. This would happen on every request, and it would be very inefficient:

  • Querying the datastore adds latency on every request, making the request processing slower.
  • The datastore would also be affected by an increase of load, potentially crashing or slowing down, which in turn would affect every Kong node.

To avoid querying the datastore every time, we can cache custom entities in-memory on the node, so that frequent entity lookups don’t trigger a datastore query every time (only the first time), but happen in-memory, which is much faster and reliable that querying it from the datastore (especially under heavy load).

Modules

  1. kong.plugins.<plugin_name>.daos

Cache custom entities

Once you have defined your custom entities, you can cache them in-memory in your code by using the kong.cache module provided by the Plugin Development Kit:

  1. local cache = kong.cache

There are 2 levels of cache:

  1. L1: Lua memory cache - local to an Nginx worker process. This can hold any type of Lua value.
  2. L2: Shared memory cache (SHM) - local to an Nginx node, but shared between all the workers. This can only hold scalar values, and hence requires (de)serialization of a more complex types such as Lua tables.

When data is fetched from the database, it will be stored in both caches. If the same worker process requests the data again, it will retrieve the previously deserialized data from the Lua memory cache. If a different worker within the same Nginx node requests that data, it will find the data in the SHM, deserialize it (and store it in its own Lua memory cache) and then return it.

This module exposes the following functions:

Function nameDescription
value, err = cache:get(key, opts?, cb, …)Retrieves the value from the cache. If the cache does not have value (miss), invokes cb in protected mode. cb must return one (and only one) value that will be cached. It can throw errors, as those will be caught and properly logged by Kong, at the ngx.ERR level. This function does cache negative results (nil). As such, one must rely on its second argument err when checking for errors.
ttl, err, value = cache:probe(key)Checks if a value is cached. If it is, returns its remaining ttl. It not, returns nil. The value being cached can also be a negative caching. The third return value is the value being cached itself.
cache:invalidate_local(key)Evicts a value from the node’s cache.
cache:invalidate(key)Evicts a value from the node’s cache and propagates the eviction events to all other nodes in the cluster.
cache:purge()Evicts all values from the node’s cache.

Bringing back our authentication plugin example, to lookup a credential with a specific api-key, we would write something similar to:

  1. -- handler.lua
  2. local BasePlugin = require "kong.plugins.base_plugin"
  3. local kong = kong
  4. local function load_credential(key)
  5. local credential, err = kong.db.keyauth_credentials:select_by_key(key)
  6. if not credential then
  7. return nil, err
  8. end
  9. return credential
  10. end
  11. local CustomHandler = BasePlugin:extend()
  12. CustomHandler.VERSION = "1.0.0"
  13. CustomHandler.PRIORITY = 1010
  14. function CustomHandler:new()
  15. CustomHandler.super.new(self, "my-custom-plugin")
  16. end
  17. function CustomHandler:access(config)
  18. CustomHandler.super.access(self)
  19. -- retrieve the apikey from the request querystring
  20. local key = kong.request.get_query_arg("apikey")
  21. local credential_cache_key = kong.db.keyauth_credentials:cache_key(key)
  22. -- We are using cache.get to first check if the apikey has been already
  23. -- stored into the in-memory cache. If it's not, then we lookup the datastore
  24. -- and return the credential object. Internally cache.get will save the value
  25. -- in-memory, and then return the credential.
  26. local credential, err = kong.cache:get(credential_cache_key, nil,
  27. load_credential, credential_cache_key)
  28. if err then
  29. kong.log.err(err)
  30. return kong.response.exit(500, {
  31. message = "Unexpected error"
  32. })
  33. end
  34. if not credential then
  35. -- no credentials in cache nor datastore
  36. return kong.response.exit(401, {
  37. message = "Invalid authentication credentials"
  38. })
  39. end
  40. -- set an upstream header if the credential exists and is valid
  41. kong.service.request.set_header("X-API-Key", credential.apikey)
  42. end
  43. return CustomHandler

Note that in the above example, we use various components from the Plugin Development Kit to interact with the request, cache module, or even produce a response from our plugin.

Now, with the above mechanism in place, once a Consumer has made a request with their API key, the cache will be considered warm and subsequent requests won’t result in a database query.

The cache is used in several places in the Key-Auth plugin handler. Give that file a look in order to see how an official plugin uses the cache.

Update or delete a custom entity

Every time a cached custom entity is updated or deleted in the datastore (i.e. using the Admin API), it creates an inconsistency between the data in the datastore, and the data cached in the Kong nodes’ memory. To avoid this inconsistency, we need to evict the cached entity from the in-memory store and force Kong to request it again from the datastore. We refer to this process as cache invalidation.

Cache invalidation for your entities

If you want your cached entities to be invalidated upon a CRUD operation rather than having to wait for them to reach their TTL, you have to follow a few steps. This process can be automated for most entities, but manually subscribing to some CRUD events might be required to invalidate some entities with more complex relationships.

Automatic cache invalidation

Cache invalidation can be provided out of the box for your entities if you rely on the cache_key property of your entity’s schema. For example, in the following schema:

  1. local typedefs = require "kong.db.schema.typedefs"
  2. return {
  3. -- this plugin only results in one custom DAO, named `keyauth_credentials`:
  4. keyauth_credentials = {
  5. name = "keyauth_credentials", -- the actual table in the database
  6. endpoint_key = "key",
  7. primary_key = { "id" },
  8. cache_key = { "key" },
  9. generate_admin_api = true,
  10. admin_api_name = "key-auths",
  11. admin_api_nested_name = "key-auth",
  12. fields = {
  13. {
  14. -- a value to be inserted by the DAO itself
  15. -- (think of serial id and the uniqueness of such required here)
  16. id = typedefs.uuid,
  17. },
  18. {
  19. -- also interted by the DAO itself
  20. created_at = typedefs.auto_timestamp_s,
  21. },
  22. {
  23. -- a foreign key to a consumer's id
  24. consumer = {
  25. type = "foreign",
  26. reference = "consumers",
  27. default = ngx.null,
  28. on_delete = "cascade",
  29. },
  30. },
  31. {
  32. -- a unique API key
  33. key = {
  34. type = "string",
  35. required = false,
  36. unique = true,
  37. auto = true,
  38. },
  39. },
  40. },
  41. },
  42. }

We can see that we declare the cache key of this API key entity to be its key attribute. We use key here because it has a unique constraints applied to it. Hence, the attributes added to cache_key should result in a unique combination, so that no two entities could yield the same cache key.

Adding this value allows you to use the following function on the DAO of that entity:

  1. cache_key = kong.db.<dao>:cache_key(arg1, arg2, arg3, ...)

Where the arguments must be the attributes specified in your schema’s cache_key property, in the order they were specified. This function then computes a string value cache_key that is ensured to be unique.

For example, if we were to generate the cache_key of an API key:

  1. local cache_key = kong.db.keyauth_credentials:cache_key("abcd")

This would produce a cache_key for the API key "abcd" (retrieved from one of the query’s arguments) that we can the use to retrieve the key from the cache (or fetch from the database if the cache is a miss):

  1. local key = kong.request.get_query_arg("apikey")
  2. local cache_key = kong.db.keyauth_credentials:cache_key(key)
  3. local credential, err = kong.cache:get(cache_key, nil, load_entity_key, apikey)
  4. if err then
  5. kong.log.err(err)
  6. return kong.response.exit(500, { message = "Unexpected error" })
  7. end
  8. if not credential then
  9. return kong.response.exit(401, { message = "Invalid authentication credentials" })
  10. end
  11. -- do something with the credential

If the cache_key is generated like so and specified in an entity’s schema, cache invalidation will be an automatic process: every CRUD operation that affects this API key will be make Kong generate the affected cache_key, and broadcast it to all of the other nodes on the cluster so they can evict that particular value from their cache, and fetch the fresh value from the datastore on the next request.

When a parent entity is receiving a CRUD operation (e.g. the Consumer owning this API key, as per our schema’s consumer_id attribute), Kong performs the cache invalidation mechanism for both the parent and the child entity.

Note: Be aware of the negative caching that Kong provides. In the above example, if there is no API key in the datastore for a given key, the cache module will store the miss just as if it was a hit. This means that a “Create” event (one that would create an API key with this given key) is also propagated by Kong so that all nodes that stored the miss can evict it, and properly fetch the newly created API key from the datastore.

See the Clustering Guide to ensure that you have properly configured your cluster for such invalidation events.

Manual cache invalidation

In some cases, the cache_key property of an entity’s schema is not flexible enough, and one must manually invalidate its cache. Reasons for this could be that the plugin is not defining a relationship with another entity via the traditional foreign = "parent_entity:parent_attribute" syntax, or because it is not using the cache_key method from its DAO, or even because it is somehow abusing the caching mechanism.

In those cases, you can manually setup your own subscriber to the same invalidation channels Kong is listening to, and perform your own, custom invalidation work.

To listen on invalidation channels inside of Kong, implement the following in your plugin’s init_worker handler:

  1. function MyCustomHandler:init_worker()
  2. -- listen to all CRUD operations made on Consumers
  3. kong.worker_events.register(function(data)
  4. end, "crud", "consumers")
  5. -- or, listen to a specific CRUD operation only
  6. kong.worker_events.register(function(data)
  7. kong.log.inspect(data.operation) -- "update"
  8. kong.log.inspect(data.old_entity) -- old entity table (only for "update")
  9. kong.log.inspect(data.entity) -- new entity table
  10. kong.log.inspect(data.schema) -- entity's schema
  11. end, "crud", "consumers:update")
  12. end

Once the above listeners are in place for the desired entities, you can perform manual invalidations of any entity that your plugin has cached. For instance:

  1. kong.worker_events.register(function(data)
  2. if data.operation == "delete" then
  3. local cache_key = data.entity.id
  4. kong.cache:invalidate("prefix:" .. cache_key)
  5. end
  6. end, "crud", "consumers")

Extending the Admin API

As you are probably aware, the Admin API is where Kong users communicate with Kong to setup their APIs and plugins. It is likely that they also need to be able to interact with the custom entities you implemented for your plugin (for example, creating and deleting API keys). The way you would do this is by extending the Admin API, which we will detail in the next chapter: Extending the Admin API.


Next: Extending the Admin API ›