Real architecture · Junior
DICT: token bucket against Pix key scanning
Shows real parameters, published in a regulation. Individuals: 100 tokens, −1 per valid query, −20 per invalid query, +1 per paid Pix, +2 per minute. Participants: categories A–H. The regulation includes a worked example: a debt balance of 15 tokens leads to an 8-minute wait.
Short answer
Shows real parameters, published in a regulation. Individuals: 100 tokens, −1 per valid query, −20 per invalid query, +1 per paid Pix, +2 per minute. Participants: categories A–H. The regulation includes a worked example: a debt balance of 15 tokens leads to an 8-minute wait.
01 · CONTEXT
When querying is the attack
The DICT, the Directory of Transactional Account Identifiers, is the base of Pix keys. The Central Bank is the maintainer of the database, and the participants, with direct or indirect access, are the requesters of the queries.
The diagram shows the user, the participant's app, and the DICT.
Participants query the DICT as requesters, while the Central Bank maintains the database.
A scan attack can come from a user or from an intruder using the participant's infrastructure.
The regulation defines a scan attack as the capture of existing links in the DICT for purposes other than making payments. It can be fast and intense, within a few hours, or slow, over days or months.
To contain this abuse, the DICT limits Pix key queries based on two indicators: queries without a payment order and invalid queries, that is, queries made for keys not registered in the DICT.
02 · THE TOKEN BUCKET
How the DICT counts queries
The limiting mechanism uses the Token Bucket algorithm. To count, the participant states in the PI-PayerId header who originates the query, and all queries from the same end user through that participant carry the same identifier. This way, the DICT controls queries both per end user and per participant.
The DICT controls queries per end user and per participant, each with its own bucket.
Each valid key query takes one token from the bucket.
Each invalid query, to an unregistered key, takes twenty tokens from the user's bucket.
For the participant, a valid query takes one token and an invalid one takes three.
After the SPI receives the payment order, the bucket gets tokens back per key query.
With an empty bucket, the query gets a rate-limit error, HTTP code 429, and is blocked until tokens are replenished.
03 · PUBLISHED PARAMETERS
Individual, legal entity, and participant
For the individual, the bucket for queries by mobile phone or e-mail has a maximum size of one hundred tokens, and the bucket size for queries by CPF, CNPJ, or random key is defined separately. In each bucket, a valid query takes one token and an invalid one takes twenty.
| Individual | Legal entity | |
|---|---|---|
| Replenishment over time | 2 tokens per minute | 20 tokens per minute |
| Return per query after the payment order | 1 token per query | 2 tokens per query |
| Cost of an invalid query | 20 tokens | 20 tokens |
For the participant, the maximum bucket size and the per-minute increment depend on the category. The Central Bank places each participant in one of the categories, from A to H. Each valid query takes one token from the participant's bucket and each invalid one takes three.
| Maximum bucket size | Increment per minute | |
|---|---|---|
| Categoria A | 50.000 | 25.000 |
| Categoria B | 40.000 | 20.000 |
| Categoria C | 30.000 | 15.000 |
| Categoria D | 16.000 | 8.000 |
| Categoria E | 5.000 | 2.500 |
| Categoria F | 500 | 250 |
| Categoria G | 250 | 25 |
| Categoria H | 50 | 2 |
04 · THE REGULATION'S EXAMPLE
When an invalid query creates debt
When the bucket is already empty, the requester receives the rate-limit error, with HTTP code 429, and cannot query until tokens are replenished. The manual itself includes a worked example for the individual.
The individual in the example has only five tokens in the bucket and makes one query.
The queried key is not registered: the DICT responds NOT FOUND, HTTP code 404, with a penalty of twenty tokens.
The bucket now has a debt balance of fifteen tokens.
New queries will only succeed after eight minutes, with replenishment of two tokens per minute.
The example makes the cost asymmetry visible: a valid query costs one token and an invalid one costs twenty.
05 · DEFENSE IN LAYERS
What the participant does before the DICT says no
The regulation requires that each participant have an internal query-limiting policy equal to or stricter than the DICT's Token Bucket. This policy is an earlier layer of protection, which limits excessive sending of requests, and the control can sit in the application or in the infrastructure.
The diagram shows the query path at the participant: internal limiting, cache, checkKeys, DICT, and monitoring.
The participant's internal policy is equal to or stricter than the DICT's bucket.
The participant does not forward to the DICT a query that would exceed the user's or the participant's limit, which prevents the rate-limit error.
Queries to legal-entity keys may have the response cached, for at most one hundred eighty seconds, to reduce the load on the DICT.
It is suggested to use checkKeys to confirm that keys exist before the requests, which avoids large numbers of invalid queries.
Monitoring tracks users' requests, and once well-founded suspicion of misuse is verified, the participant must block the user immediately.
Monitoring has two ratios. The first compares queries to the DICT with orders sent to the SPI: a value slightly above one is expected, and values close to two or greater are typically indicative of an anomaly, although the threshold may vary. The second compares unregistered keys with found keys, and the regulation notes that this relationship is affected by the user journey and behavior.
The regulation usually gives references for suspicion: percentages of unregistered keys above seven percent for the participant and above twenty percent for users who have made at least one hundred queries in the last twenty-four hours.
06 · READINGS FOR THE DESIGN
What to take into an interview
Three ideas sum up the case. The cost of a query depends on its result, since an invalid one costs much more than a valid one. The token credit depends on payment, since the order received by the SPI returns tokens to the bucket. And the defense has layers, because the participant limits before the DICT does.
Frequently asked questions
What is DICT: token bucket against Pix key scanning?
Shows real parameters, published in a regulation. Individuals: 100 tokens, −1 per valid query, −20 per invalid query, +1 per paid Pix, +2 per minute. Participants: categories A–H. The regulation includes a worked example: a debt balance of 15 tokens leads to an 8-minute wait.
What does the DICT: token bucket against Pix key scanning design cover?
It is organized in these sections: When querying is the attack, How the DICT counts queries, Individual, legal entity, and participant, When an invalid query creates debt, What the participant does before the DICT says no, and What to take into an interview.
How long is DICT: token bucket against Pix key scanning and what level is it?
It takes about 12 minutes to read and is rated Junior level.
Now design it yourself
Run this case as an interview in the System Design school: you make the decisions, the tutor challenges them and you see how your design scores.