Quick revision
The short answer
We define one documented order for the platform's aspects, for example security outermost, then tracing and metrics, then retry, with the transaction innermost, and publish the order values as constants in the starter so teams can place their own aspects between them. The key rule is that retries wrap transactions so every attempt gets a new transaction. We test the combined order in a sample application and review it whenever a new cross-cutting feature joins the platform.
What to remember
In this answer, we designed a single order for the platform's cross-cutting aspects: security outermost, then tracing and metrics, a reserved range for teams, then retry, with the transaction innermost so each retry gets a fresh transaction. We published the values as constants with gaps between them, used them for both our own aspects and framework features, tested the combination in a sample application on every change and upgrade, and made any change to the order a reviewed, announced decision.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We define one documented order for the platform's aspects, for example security outermost, then tracing and metrics, then retry, with the transaction innermost, and publish the order values as constants in the starter so teams can place their own aspects between them. The key rule is that retries wrap transactions so every attempt gets a new transaction. We test the combined order in a sample application and review it whenever a new cross-cutting feature joins the platform.
Why this question is asked
When five shared starters each add an aspect to the same service methods, the order in which they run decides real behaviour: whether a rejected call is traced, whether a retry gets a fresh transaction, whether metrics count attempts or calls. If each starter picks its own number, or none, every service gets slightly different behaviour and nobody can explain it. Interviewers ask this design question to see whether we can turn ordering from an accident into a platform contract. In this answer, we will recap how aspect order works, design the layers and their reasons, publish them as constants, test the combination, and set up a review rule for new features.
1A Quick Recap of Aspect Order
Spring AOP applies every aspect and every annotation-driven feature, such as @Transactional or method security, as an advisor in a proxy's interceptor chain. Each advisor has an order value, and a lower value means higher precedence: that advisor is the outer layer, entered first and left last. Advisors without an order get Ordered.LOWEST_PRECEDENCE, and the relative order of advisors with equal precedence is undefined.
So the problem is not technical difficulty. Spring already supports exactly what we need. The problem is that, without a platform decision, the order is set by whoever wrote each starter, or by bean registration order, and it changes when dependencies change.
2Choosing the Layers and Why
We design the order from the outside in, asking for each concern what it needs to see. Security goes first: a call that is not allowed should be rejected before we spend effort on it, and it should not be retried. Tracing and metrics come next, so that they measure the whole logical call including all retry attempts and the time spent in transactions. Retry follows, and the transaction is the innermost layer, right next to the business method, so that each retry attempt runs in its own transaction and rolls back on its own.
- Security (100)reject unauthorised calls before any other work
- Tracing (200)one span for the whole logical call
- Metrics (300)timing and outcome of the call, including retries
- Team aspects (400 to 499)slot reserved for service-specific concerns
- Retry (500)repeats everything below it
- Transaction (600)a new transaction for each attempt
- Business methodthe code the team wrote
7 layers, from the top down. Step through them one at a time.
Lower values are outer layers. Gaps between the numbers leave room for later additions.
Some teams prefer tracing outside security so that rejected calls also appear in traces. That is a valid choice; what matters is that the platform makes it once, writes down the reason, and applies it everywhere. If we want both, the security layer can add a span event itself when it rejects a call.
Notice the gaps between the numbers. Using 100, 200 and so on lets us add a new layer later, such as a rate limiter at 150, without renumbering everything. Reserving a range for teams tells them exactly where their own aspects belong.
3Publishing the Order as Constants
Numbers written into five starters drift apart. We define them once, in a small module every starter depends on, and use the constants both in our own aspects and to configure framework features that have their own order setting.
For framework features we do not own, we look up how their order is configured. Transaction management takes an order on @EnableTransactionManagement. Spring Security's method security uses its own interceptor order values and an offset attribute on @EnableMethodSecurity, and observation aspects such as Micrometer's are ordinary beans whose order we can set when we declare them. The exact attributes vary between versions, so the platform team should check the version in use and pin the values in the starter rather than leave them at defaults.
4Testing the Combined Order
Ordering is a property of the combination, so a unit test of one starter cannot prove it. We keep a small sample application that depends on all the platform starters and has one service method annotated with everything: secured, traced, timed, retried and transactional. Tests on that application assert the behaviour each layer promises.
These tests check behaviour rather than the internal list of advisors, so they survive library changes in class names. We run this sample in the platform's pipeline on every starter change and every framework upgrade, because an upgrade can change a default order without any code change on our side.
5Reviewing the Order When Features Are Added
The order is a contract, so changing it needs the same care as changing an API. We keep a short document next to the constants that states each layer, its value and the reason for its position. Adding a new cross-cutting feature, such as a circuit breaker or a bulkhead, requires a review that answers where it goes and why: a circuit breaker, for example, usually sits outside retry so an open circuit stops all attempts at once.
When the order changes, the release notes of the starter say so, the sample application's tests are updated in the same change, and teams with aspects in the reserved range are told. That keeps the order predictable for a hundred services instead of being rediscovered by each of them.
6Follow-up questions
Why must retry wrap the transaction and not the other way round?
If the transaction were outside, one transaction would surround all attempts. The first failure marks it rollback-only, so later attempts run in a transaction that can never commit, and the call ends with an UnexpectedRollbackException. With retry outside, each attempt passes through the transaction interceptor again and gets a fresh transaction and persistence context.
Should metrics count each retry attempt or the whole call?
Usually both, but at different layers. The metrics layer outside retry measures what the caller experienced: one call, its total time and its final outcome. The retry layer itself should publish a counter of attempts and retries, so we can see that a call succeeded only on its third try. Putting the main timer inside retry would hide that cost from dashboards.
What do you do when a team needs an aspect outside security?
First ask why, because running code before authorization is a security decision. If there is a real need, such as request correlation that must exist even for rejected calls, we can give it a documented value below the security layer, but it should be a platform decision recorded with its reason, not a team choosing a smaller number on its own.
7Conclusion
In this answer, we designed a single order for the platform's cross-cutting aspects: security outermost, then tracing and metrics, a reserved range for teams, then retry, with the transaction innermost so each retry gets a fresh transaction. We published the values as constants with gaps between them, used them for both our own aspects and framework features, tested the combination in a sample application on every change and upgrade, and made any change to the order a reviewed, announced decision.
Open with
Since all of these concerns become advisors in the same proxy chain, and the lower order value is the outer layer, I would make the order a documented platform contract instead of leaving it to each starter.
Then, if they want more
From the outside in: security first, so unauthorised calls are rejected before any work and never retried; then tracing and metrics, so they measure the whole logical call including retries; then a reserved range for team aspects; then retry; and the transaction innermost, so each retry attempt runs in its own new transaction.
I would publish the values as constants in a small shared module, with gaps like 100, 200, 300 so we can add layers later, and use them both in our own aspects and to configure framework features, for example the order attribute on EnableTransactionManagement and the method security settings, checking how each library version exposes its order.
Then I would keep a sample application that uses all the starters on one method and test the behaviour: a failed first attempt gets a second transaction, a rejected call is not retried, metrics see one call. That runs on every starter change and framework upgrade, because defaults can shift.
Finally, adding a new cross-cutting feature, like a circuit breaker, requires a review of where it sits and why, and the change is announced in the release notes.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Core technologies
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Your platform provides aspects for tracing, metrics, retries, transactions and security...": https://scrivio-interview-library.netlify.app/spring-boot/aop/e427c1dde4/