gitlab-org--gitlab-foss/doc/development/polling.md
Rémy Coutable 028bca7bc5
Fix typo in doc/development/polling.md: If-None-Modified -> If-None-Match
Signed-off-by: Rémy Coutable <remy@rymai.me>
2017-03-15 09:01:16 +01:00

1.8 KiB

Polling with ETag caching

Polling for changes (repeatedly asking server if there are any new changes) introduces high load on a GitLab instance, because it usually requires executing at least a few SQL queries. This makes scaling large GitLab instances (like GitLab.com) very difficult so we do not allow adding new features that require polling and hit the database.

Instead you should use polling mechanism with ETag caching in Redis.

How to use it

  1. Add the path of the endpoint which you want to poll to Gitlab::EtagCaching::Middleware.
  2. Implement cache invalidation for the path of your endpoint using Gitlab::EtagCaching::Store. Whenever a resource changes you have to invalidate the ETag for the path that depends on this resource.
  3. Check that the mechanism works:
    • requests should return status code 304
    • there should be no SQL queries logged in log/development.log

How it works

  1. Whenever a resource changes we generate a random value and store it in Redis.
  2. When a client makes a request we set the ETag response header to the value from Redis.
  3. The client caches the response (client-side caching) and sends the ETag as the If-None-Match header with every subsequent request for the same resource.
  4. If the If-None-Match header matches the current value in Redis we know that the resource did not change so we can send 304 response immediately, without querying the database at all. The client's browser will use the cached response.
  5. If the If-None-Match header does not match the current value in Redis we have to generate a new response, because the resource changed.

For more information see: