# Data freshness: live and cached

> How data freshness works on CNPJá: the cache, live queries, max age, partial results, when a query reaches the source and good practices to keep data current.

Every item on CNPJá arrives in one of two ways: from a live query, made at the source at the time of the request, or from the cache, the latest reading of the source that CNPJá already stored. The rule is the same in the app and in the API, and each item's date tells when it was read.

## Live and cached

|  | Live | Cache |
| --- | --- | --- |
| Origin | The source, at the time of the request | The latest reading of the source, stored by CNPJá |
| Response time | Can take a few seconds | The fastest |
| Consumption | [Live query](https://cnpja.com/en-us/docs/glossary#consulta-em-tempo-real--live-query), which costs more | [Cached query](https://cnpja.com/en-us/docs/glossary#consulta-em-cache--cached-query) |

The choice is made for each part of the data, which CNPJá calls a [resource](https://cnpja.com/en-us/docs/glossary#recurso--resource): the CNPJ registration, the tax regime, the state registrations, the SUFRAMA registrations, the coordinates. A single request can bring some parts live and others from the cache, and consume both units, as explained in [Queries and quota](https://cnpja.com/en-us/docs/billing/queries-and-quotas).

## What is in the cache

The cache holds:

- the CNPJs of the latest [CNPJ open data](https://cnpja.com/en-us/docs/glossary#dados-abertos-do-cnpj--cnpj-open-data) release, with establishments, companies, partners and the Simples Nacional and SIMEI opt-in;
- everything already queried live, by any customer: CNPJs not yet in the release, state registrations, SUFRAMA registrations, the IBS and CBS regular regime option and certificates;
- the regime declared in the latest ECF released;
- the CEPs of the current edition of the Correios DNE;
- the coordinates already calculated.

The cache is shared by every customer: a live query made by anyone updates the item for everyone. That is why an item's date can be more recent than your organization's last request.

A CNPJ opened after the latest open data release, and that nobody has queried live, is not in the cache yet: it enters with the first lookup, as explained in [A CNPJ's first lookup](#first-lookup). Likewise, data that only exists live, such as state registrations, only reaches the cache after someone queries it.

Searches use the cache only, and a CNPJ that has just entered it takes some time to show up in results, as explained in [When a new CNPJ appears](https://cnpja.com/en-us/docs/data/search-matching#index-lag). Where each item comes from is in [Data sources](https://cnpja.com/en-us/docs/data/sources#table).

## Live queries

In a live query, CNPJá reads the source at the time of the request, delivers the result and stores it in the cache, except the map and street view images, which are generated anew on every request. It brings the data available at the source at the time of the query, as the source records it at that moment.

| Source | What is queried live |
| --- | --- |
| Receita Federal | The CNPJ registration, with the company data and the board of partners |
| Simples Nacional | The Simples Nacional, SIMEI and IBS and CBS regular regime options |
| CCC | State registrations, state by state |
| SUFRAMA | SUFRAMA registrations |
| [Geolocation](https://cnpja.com/en-us/docs/data/sources#google-maps) | An address's coordinates and precision and the map and street view images |

Certificates are also obtained live, at each one's source, as explained in [CNPJ card and other certificates](https://cnpja.com/en-us/docs/data/certificates).

The sources of a single request are queried at the same time, so it takes as long as the slowest source, not the sum of all of them. A slow or unavailable source may not answer in time; in that case, the rest arrives as usual, as explained in [Partial result](#partial).

A live query costs more than a cached query and is charged even when the data has not changed since the previous reading. The consumption of each case is in the [Metering Rules](https://cnpja.com/en-us/legal/metering#units). On the platform, only organization members run live queries, as explained in [Guests, accounts and organizations](https://cnpja.com/en-us/docs/app/access-levels).

## Max age

An item's age is the time since the source was last read. For the CNPJ registration, the most recent reading between the open data release and the latest live query counts.

The [max age](https://cnpja.com/en-us/docs/glossary#idade-máxima--maximum-data-age) is the oldest age a request accepts. When the stored item is within it, it comes from the cache; when it is past it, or was never read, it is queried live before the response. The max age ranges from 5 minutes to 1 year, with a default of 45 days in the API and of 7 days in exports and bulk lookups.

In the API, some parts accept a max age of their own, which applies to them only: the tax regime, the state registrations and the SUFRAMA registrations. So a single request can accept a registration up to 45 days old and require state registrations up to 1 day old.

Choosing the age means choosing between cost and freshness. A longer age brings more data from the cache, with an immediate response and lower consumption; a shorter age sends more requests to the source, which are slower and cost more.

On the platform, the max age is the **Maximum age** option of exports, bulk lookups and certificates, and on a CNPJ's record the automatic updates decide, as the next section shows. In the API, the max age is a parameter of each request, described in [Syntax](https://cnpja.com/en-us/docs/api/max-age#syntax).

## Live or cache: who decides

| Where | Who decides | Default |
| --- | --- | --- |
| A CNPJ's record, in the app | Each source's **Automatic updates** rule, when the record opens, and the refresh buttons, when you use them | **Older than 7 days**; for **Geolocation**, **When never queried** |
| Export and bulk lookup | The **Maximum age** chosen before exporting | 7 days |
| Certificates, in the app | The **Maximum age** chosen before issuing | 7 days |
| API | Each request's max age, in the operations that accept it; without an API key, always the default | 45 days |
| Searches, partner and CEP records and the **Network** | Nobody: the data comes from the cache only | Does not apply |

A CNPJ's record opens with cached data, whatever its age. Only a CNPJ not yet in the cache is read live at the Receita Federal on opening, and only when whoever opens it is a member of an organization, as explained in [A CNPJ's first lookup](#first-lookup). For a member, the automatic updates then query live each source older than the rule chosen for it, or never queried, without asking for confirmation. **Update all** and each source's button, in **Sources**, query live when you use them; when the refresh consumes more than one live query, the **Run live queries?** confirmation shows the total first.

> **Important:** automatic updates are on for every member, with the **When never queried** rule on **Geolocation** and **Older than 7 days** on the other sources. Opening a record whose sources are older than the rule, or were never queried, consumes the organization's live queries, without confirmation. An address that cannot be located stays without coordinates, and its record queries and charges **Geolocation** again every time it opens, until you choose **Never** for it. To change a source's rule, or turn it off with **Never**, use **Automatic updates**, in **Sources**; the choice applies only to the browser where it was made. See [Automatic updates](https://cnpja.com/en-us/docs/companies/live-refresh#automatic-updates), [Address not located](https://cnpja.com/en-us/docs/data/location#address-not-located) and the [Metering Rules](https://cnpja.com/en-us/legal/metering#auto-refresh).

## Resources with their own rules

Some parts do not follow the general max age rule:

| Part | Rule |
| --- | --- |
| Coordinates | Calculated once per address and again only when the address changes or when **Geolocation** is queried on a CNPJ's record. They carry no date and do not follow the max age: in automatic updates, **Geolocation** only has **When never queried** and **Never**. While the address cannot be located, the calculation is redone on every request, live |
| Map and street view | Generated on every request, live, from the address already stored in the cache. A CNPJ outside the cache is first queried at the Receita Federal |
| Detailed tax regime | The age counts only from the latest live query at the Simples Nacional, so it can require a live query when the summary would come from the cache |
| Declared regime | Follows the latest ECF released and has no live query |
| State registrations, summary level | Only the establishment's state is queried. If it has a registration there, the same query refreshes the other states; if not, the others stay as they were |
| State registrations, detailed level | Each state has its own age and is queried separately. A state with no registration never counts as refreshed and is queried live every time |
| Receita Federal certificates | The CNPJ card and the QSA come from the same live query and are refreshed only by live queries, not by open data |
| CEPs | Follow the DNE editions and have no live query |
| People and participations | Come from the cache. A live query of a CNPJ refreshes the company's board, and each person's participations can wait for the next open data release |

> **Important:** at the detailed level of state registrations, each requested state where the establishment has no registration goes to the source on every request, whatever the max age. Asking for every state of an establishment with no registration at all multiplies that consumption by the 27 states. Ask only for the states you need, as explained in [All states: mind the cost](https://cnpja.com/en-us/docs/data/state-registrations#all-states-cost).

The details of each part are in [Address coordinates and imagery](https://cnpja.com/en-us/docs/data/location), [Tax regime](https://cnpja.com/en-us/docs/data/tax-regime), [State registration and CCC](https://cnpja.com/en-us/docs/data/state-registrations#detail-levels), [CNPJ card and other certificates](https://cnpja.com/en-us/docs/data/certificates), [CEPs, streets and IBGE codes](https://cnpja.com/en-us/docs/data/postal-codes) and [Partners and administrators board](https://cnpja.com/en-us/docs/data/partners-and-qsa#current-only).

## Partial result

When the live query of a part fails or does not finish in time on the record, in certificates or in the API, the request is not aborted: the other parts arrive as usual, and the affected part keeps its cached version, which can be older than the requested age, or comes back empty, when it was never read. In certificates, the affected document comes in the version issued earlier or is left out, when there is no earlier version.

The affected part is not charged, even when it comes from the cache, as stated in the [Metering Rules](https://cnpja.com/en-us/legal/metering#not-charged). A partial result is a valid result: check the date of the part your decision depends on and, if it is old, repeat the request later.

- *On the record:* each source is refreshed separately, and a failing source does not hold back the others. Its section keeps the previous reading and that reading's age, and a notice names the source, such as **The live query failed, try again in a moment** or **The source took too long to answer, try again in a moment**.
- *In certificates:* the notice **Some certificates could not be reissued and are older than the chosen maximum age.** and the **Partial** stamp mark the partial result, as explained in [Issue certificates as PDF](https://cnpja.com/en-us/docs/companies/certificates#partial).
- *In the API:* the response comes with a status of its own for partial success, described in [Partial success](https://cnpja.com/en-us/docs/api/max-age#partial).
- *In exports and bulk lookups:* there is no partial result. The failing part is queried again until it updates, and the spreadsheet is ready only after that, which can delay the delivery.

## A CNPJ's first lookup

A CNPJ outside the cache is read live at the Receita Federal the first time a member of an organization opens its record in the app or someone looks it up with an API key, and from then on it stays in the cache for everyone.

On the platform, that first reading is a live query of the organization of whoever opens the record, not a cached query. With the live quota used up and no overage budget to pay for it, it is refused before reaching the Receita Federal, and the quota used up notice shows in place of the record.

If the Receita Federal fails on that first lookup, there is no earlier reading to deliver instead: the request fails rather than bringing a partial result. Try again later.

In the API, looking up a CNPJ the Receita Federal does not find is charged as well, as explained in [What is charged even without a useful result](https://cnpja.com/en-us/legal/metering#charged-without-result).

- *In the app:* for a [guest](https://cnpja.com/en-us/docs/glossary#visitante--guest), who has not signed in, a CNPJ outside the cache shows the **CNPJ not found** notice, even if it exists at the Receita Federal, until a member or an API key looks it up. For a member, the notice appears when the Receita Federal does not find the CNPJ. What to do is in [When a record won't open](https://cnpja.com/en-us/docs/companies/lookup#not-found).
- *In the API without a key:* public access does not query the Receita Federal for a CNPJ outside the cache and answers that it was not found. With an API key, the query is made, as explained in [Public access](https://cnpja.com/en-us/docs/api/public-access).

## Good practices

- *Choose the age by use.* To enrich records and analyze many CNPJs, a longer age, such as 90 days, reduces consumption and waiting time. Short ages suit one-off checks, such as confirming a state registration before issuing an invoice.
- *Refresh only what the decision needs.* On the record, refresh the source that matters instead of **Update all**. In the API, shorten the age of the critical part only.
- *Do not repeat live queries without a reason.* The result only changes when the source changes, and each live query is charged.
- *Adjust the automatic updates.* If you open many records, you can switch each source's rule to **Older than 30 days**, **When never queried** or **Never**, and the **Geolocation** rule to **Never**.
- *Ask only for the states you need* at the detailed level of state registrations.
- *Keep the date with the data.* When you copy an item into another system, also keep the reading date of the part it came from, which tells how current it was.
- *Treat a partial result as valid* and check the date of the affected part before deciding.

## Where it appears in the app

- *Colors:* across the app, teal marks live queries and grey marks cached queries: in the dots beside each source's age, in the [quota meters](https://cnpja.com/en-us/docs/billing/usage), in the [estimated consumption](https://cnpja.com/en-us/docs/lists/export#estimate) and in the charts of the **Usage** page. The color tells the kind of query, never the age of the data.
- *A CNPJ's record:* each section shows the source and the age of its reading, with the dot of its kind and the **Outdated** or **Never queried** notice when that applies; **Geolocation**, which carries no date, shows **Updated** or **Never queried**. **Sources** lists the five sources, each with its age, or state, and automatic updates rule, and **Update all** queries all of them live, **Geolocation** only when there are no coordinates yet. See [Source and age of each item](https://cnpja.com/en-us/docs/companies/record#sources) and [Refresh data live](https://cnpja.com/en-us/docs/companies/live-refresh).
- *Automatic updates:* on by default, with the **Older than 7 days** rule, or **When never queried** for **Geolocation**, and set per browser. See [Automatic updates](https://cnpja.com/en-us/docs/companies/live-refresh#automatic-updates).
- *Export and bulk lookup:* the **Maximum age**, in the **Resources and age** step, chooses between live and cache for every establishment, starting at 7 days, and the consumption shown in the same step follows each age. See [Export to a spreadsheet](https://cnpja.com/en-us/docs/lists/export#max-age) and [Bulk CNPJ lookup](https://cnpja.com/en-us/docs/lists/bulk-lookup).
- *Certificates:* the **Maximum age** option sets how old an already issued certificate can be. See [Issue certificates as PDF](https://cnpja.com/en-us/docs/companies/certificates#max-age).
- *Other apps:* the CNPJ search and the **Partners**, **CEP** and **Network** apps use the cache only. See [Partner record](https://cnpja.com/en-us/docs/partners/record) and [CEP record and companies](https://cnpja.com/en-us/docs/postal-codes/record).

## In the API

The max age is the `maxAge` parameter, accepted by Lookup CNPJ, Lookup CPF and the certificate operations, with ages of their own for the tax regime, state registrations and SUFRAMA. The partial result is the `206` response, and each resource reports its latest reading in `updatedAt`. Syntax, operations, examples and errors are in [Max age and partial success](https://cnpja.com/en-us/docs/api/max-age).

## Limitations

- *The max age is not a schedule.* CNPJá does not watch a CNPJ for you: an item only goes back to the source when a request requires a newer reading than the stored one, or when the source releases new data.
- *The source defines what exists.* A live query reflects the source's registry at that moment, and a change the source has not processed yet does not appear, whatever the max age.
- *The Receita Federal sets the pace of the open data.* The interval between releases does not depend on CNPJá.
- *Not everything gets newer with a shorter age.* The declared regime, the coordinates of an address that did not change, CEPs and people's participations follow their own rules, described in [Resources with their own rules](#special-rules).
- *Sources can fail.* A slow or unavailable source leads to a [partial result](#partial), delays the spreadsheet of an export or fails the first lookup of a CNPJ.
- *The data is reproduced as the source records it*, errors included, and is informational, as stated in the [Terms of Use](https://cnpja.com/en-us/legal/terms). Wrong data is corrected at the source, as explained in [How to correct data at the source](https://cnpja.com/en-us/docs/data/sources#correct-at-source).
