My account

Interview questions

1Service lifetimes in DIEasy–Dependency Injection

Question: You see this code in code review. The ReportCache service is registered as Singleton. The AppDbContext service is registered with the AddDbContext method.

builder.Services.AddDbContext<AppDbContext>(o => o.UseSqlServer(cs));
builder.Services.AddSingleton<ReportCache>();

public class ReportCache(AppDbContext db)
{
    public Task<Report> LoadAsync(CancellationToken ct)
        => db.Reports.FirstAsync(ct);
}

This service is used in every request. What is the difference between Singleton, Scoped and Transient? What do you think about this code? Does it have a problem in Production?

Short answer: A Singleton service lives until the end of the app. So the DbContext inside it also stays until the end of the app. As a result, all requests use this one DbContext at the same time. But DbContext is not built for this.

Hint (if the candidate says there is no problem): In the Development environment, the app gives this error at run time: “Cannot consume scoped service from singleton”. In Production this error does not appear. But after a few hours, this error sometimes shows up in the log: “A second operation was started on this context instance before a previous operation completed”. Users also sometimes see old data. Why?

First: the three lifetimes

Type How many instances are made? Good example
Singleton Only one instance for the whole app Settings, in-memory cache
Scoped One instance for each request DbContext
Transient A new instance every time it is asked for A light service with no state

Why does this happen? (step by step)

  1. The DI system builds ReportCache only once. At that moment it also builds one AppDbContext and gives it to it.
  2. The ReportCache service is never destroyed. So that AppDbContext is never disposed either. This mistake is called a Captive Dependency.
  3. Now all requests share one ReportCache, so they also share one AppDbContext.
  4. When two requests run a query at the same moment, DbContext gives the “second operation” error. The reason is that DbContext is not thread-safe.
  5. Every record that DbContext reads stays in its own memory (the change tracker). If another service changes that record in the database, this DbContext still returns the old version. So the user sees old data. Memory also grows slowly.
  6. Why does only Development give an error? Because ASP.NET Core turns on the ValidateScopes check by default only in Development.

Simple rule: A service can only depend on a service whose lifetime is equal to or longer than its own.

Solution: In the Singleton service, each time we need it, we create a short-lived DbContext and then dispose it:

public class ReportCache(IDbContextFactory<AppDbContext> dbFactory)
{
    public async Task<Report> LoadAsync(CancellationToken ct)
    {
        await using var db = await dbFactory.CreateDbContextAsync(ct);
        return await db.Reports.AsNoTracking().FirstAsync(ct);
    }
}

Another way is to create a separate scope with IServiceScopeFactory. This way is common in a BackgroundService.

Follow-up question: How do you use HttpClient? Why is creating a new HttpClient in every request bad?

Follow-up answer:

  • Each new HttpClient opens its own TCP connections. Even after Dispose, the operating system keeps that socket in the TIME_WAIT state for a few minutes.
  • Under heavy load, the free ports run out and we get a SocketException error. This is called Socket Exhaustion.
  • There is also the opposite mistake: one static HttpClient forever. In this case ports do not run out, but the app never sees DNS changes.
  • First right way: IHttpClientFactory. This tool pools the connections and refreshes them every few minutes.
  • Second right way: one shared HttpClient with SocketsHttpHandler and the PooledConnectionLifetime setting.

Red flag: The candidate only recites the definition of the three lifetimes, but cannot explain what problem a wrong mix of them creates.

Report a problem or send a better text

Edit on GitHub

2Working with async/await and the Thread PoolEasy–async/await and the thread pool

Question: In an ASP.NET Core API, one place in the code is written like this:

var price = _httpClient.GetStringAsync(url).Result;

You see this code in code review. On campaign day, this endpoint gets several times its normal traffic. What do you think about this code? Does it have a problem? And what does async/await actually do? Does it make code faster?

Short answer: Async code does not make a single request faster. Its job is to free the thread while we wait for I/O (network, database, file). But the Result property blocks that same thread. Under heavy load, all threads of the thread pool sit and wait. So new requests get stuck in the queue. This is called Thread Pool Starvation.

Hint (if the candidate says there is no problem): With low traffic, everything is fine. On campaign day, response time goes from 50 milliseconds to more than 10 seconds. But the server CPU is only at 15%. The number of threads in the process also grows slowly and steadily. Why?

A simple example: Threads are like waiters in a restaurant.

  • In sync code (or with Result): the waiter gives the order to the kitchen and stands right there until the food is ready. With 8 waiters, only 8 tables get served at the same time.
  • In async code: the waiter gives the order and goes to take the order of the next table. When the food is ready, any free waiter carries it.

Why does this happen? (step by step)

  1. Each request runs on a thread from the thread pool.
  2. With await: when we reach the network call, the method returns for a while and the thread goes back to the pool. When the network answer arrives, the rest of the method runs on a free thread. For this, the compiler turns the method into a state machine.
  3. With Result: the thread stays blocked until the answer arrives and does no other work.
  4. Suppose the pool starts with 8 threads (usually the number of CPU cores). Each outside call also takes 1 second. So with Result, at most 8 requests per second get an answer. The rest wait in the queue.
  5. The thread pool sees the queue has grown and adds new threads. But it does this slowly (about one or two threads per second). That is why the number of threads grows slowly.
  6. These threads only wait and do no computing work. So CPU stays low. “Low CPU but a slow system” is the sign of this exact problem.

In apps that have a SynchronizationContext (like WinForms and old ASP.NET), using Result can even cause a deadlock.

Solution: Make the code async from start to end (async all the way):

public async Task<decimal> GetPriceAsync(string url, CancellationToken ct)
{
    var text = await _httpClient.GetStringAsync(url, ct);
    return decimal.Parse(text);
}

Note: for heavy CPU work (for example, a long calculation), async does not help. Because there the thread is really busy working, not waiting.

Follow-up question: If we want to send 100 HTTP requests, but at most 10 at the same time, what do you do?

Follow-up answer: One way is the Parallel.ForEachAsync method with a limit of 10:

await Parallel.ForEachAsync(urls,
    new ParallelOptions { MaxDegreeOfParallelism = 10, CancellationToken = ct },
    async (url, token) => await CallAsync(url, token));

Another way is to use a SemaphoreSlim with a capacity of 10.

Why is running all 100 requests with Task.WhenAll not good? Because all 100 requests go out together. The other service may get overloaded, or we may hit its rate limit.

Red flag: The candidate says “async means the code runs on a new thread” or “async makes code faster”.

Report a problem or send a better text

Edit on GitHub

3The N+1 problem in EF CoreMedium–Entity Framework Core

Question: We have an endpoint that returns the list of the last 50 orders, together with the customer name of each order. Lazy Loading is turned on in the project, and the code is this:

var orders = await db.Orders
    .OrderByDescending(o => o.CreatedAt)
    .Take(50)
    .ToListAsync();

return orders.Select(o => new OrderDto(o.Id, o.Total, o.Customer.Name));

You see this code in code review. What do you think? Does it have a problem? How many queries does this code send to the database?

Short answer: This problem is called N+1: 1 query for the list of orders, and then N queries (one for each order) for the customer. The cause is Lazy Loading. This means EF Core reads the customer of each order at the exact moment the code touches it for the first time, and it does this with a separate query.

Hint (if the candidate says there is no problem): The tables are small, but the answer of this endpoint takes about 2 seconds. We turn on the SQL log in EF Core (or use SQL Server Profiler or an APM tool). We see that for one call of this endpoint, 51 queries go to the database: one query for the orders table, and 50 separate queries for the customers table (one customer each time). The API output is correct and returns the same 50 orders. So the problem is not the number of records, it is the number of queries. Why?

What Lazy Loading means:

  • When Lazy Loading is on, EF Core reads only the columns of the orders table in the first query. The customer of the order stays empty.
  • Whenever the code reaches the customer of an order, EF Core sees it is not loaded yet. So at that moment it sends a query for that one customer.
  • Without Lazy Loading, the customer of the order would just be null and we would get a NullReferenceException error. That is why some teams turn on Lazy Loading and do not see the N+1 problem.

Why does this happen? (step by step)

  1. The ToListAsync method runs one query: 50 orders, without customers.
  2. Then Select runs on the in-memory list. For each order, it reads the customer name.
  3. Each time, Lazy Loading sends a separate query. So 50 orders means 50 queries.
  4. Each query, even if it takes 5 milliseconds, has one network round trip. 50 round trips one after another make the endpoint slow. With a list of 1000 it gets even worse.

The same problem also happens without Lazy Loading, if the developer runs a query inside a foreach loop.

Solution:

  1. With Include, the customer comes with a JOIN in the same first query. So 51 queries become 1 query.
var orders = await db.Orders.Include(o => o.Customer)
    .OrderByDescending(o => o.CreatedAt).Take(50).ToListAsync();
  1. A better way is Projection: we write Select before ToListAsync. In this case EF Core builds the JOIN itself and reads only the needed columns. Lazy Loading and change tracking are not involved either.
var result = await db.Orders
    .OrderByDescending(o => o.CreatedAt).Take(50)
    .Select(o => new OrderDto(o.Id, o.Total, o.Customer.Name))
    .ToListAsync();
  1. If we really need the entity itself and we only read, we add the AsNoTracking method.
  2. Many teams turn Lazy Loading off completely, so this problem does not stay hidden.

One more important point: As long as the query is an IQueryable, it runs in the database (SQL). If we call the AsEnumerable or ToList method in the middle, the later filters and Selects run in memory. This means all the data comes from the database first.

Follow-up question: Now only one query goes out, but it is still slow. What do you do next?

Follow-up answer:

  1. The candidate takes the real SQL query from the log.
  2. They look at its execution plan.
  3. For example, if the whole table is scanned to sort by creation date, they add an index on the CreatedAt column of the orders table.
  4. They measure the time before and after the change.

Red flag: The candidate has never looked at the SQL that EF Core builds, or does not know when Lazy Loading runs a query.

Report a problem or send a better text

Edit on GitHub

4Caching with RedisMedium–RedisCaching patterns

Question: The user profile page is read from the database in every request, and the database CPU is high. We decided to cache the profile in Redis. Three questions:

  1. How do you design reading and writing the cache?
  2. When the user edits their profile, how do you make sure old data is not shown?
  3. If Redis goes down, what happens?

We also have another key: “home page settings”. This key is read in every home page request (about 2000 requests per second) and its TTL is 10 minutes. How do you see this design? What happens at the moment this key expires?

Short answer:

  • We use the Cache-Aside pattern and always set a TTL.
  • After an edit, we delete the cache key.
  • If Redis goes down, we read directly from the database.
  • The name of the last problem is Cache Stampede: when a popular key expires, all requests go to the database at the same time.

Hint (if the candidate says there is no problem): In monitoring we see that the “home page settings” key expires every 10 minutes. At exactly that moment, the database CPU reaches 100% for a few seconds. Why?

Part 1: How does the Cache-Aside pattern work?

  1. First we look for the key in Redis (for example, the profile key of user number 42).
  2. If it is there, we return it.
  3. If it is not there, we read from the database. Then we put it in Redis with a TTL (for example, 10 minutes) and return it.

Why is a TTL needed? If something goes wrong somewhere and the cache is not deleted, the TTL makes sure old data stays only a few minutes, not forever.

Part 2: When the data changes

  • First we save in the database, then we delete the cache key. The next request reads fresh data from the database and caches it again.
  • Why delete and not update the cache? Suppose we have two edits at the same time:
    1. The database may be updated in the order A then B.
    2. But the cache may be updated in the order B then A.
    3. Now the cache has wrong data until the TTL ends.
  • Deleting the cache does not have this problem.

Part 3: If Redis goes down

  • The cache must be “optional”. This means we catch the Redis error and read directly from the database.
  • We set a short timeout for Redis. Otherwise each request waits a few seconds for a dead Redis, and the whole site gets slow.
  • We must keep in mind that the database must also handle the load without the cache, or we must have a plan for this case.

Part 4: Why does the database CPU go up at the moment of expiry? (Cache Stampede)

  1. While the key is in the cache, for example 2000 requests per second get their answer from Redis.
  2. At the moment the key expires, all these requests see the cache empty.
  3. So they all send the same query to the database together, until one of them fills the cache again.

Ways to fix it:

  • Only one request builds the data, and the others wait for its result (one lock for each key). In .NET, the HybridCache library does this inside each server with the GetOrCreateAsync method.
  • A little before expiry, we refresh the cache in the background.
  • If many keys expire together, we add a small random number to the TTL (jitter). This way they do not all expire at one moment.

What should we not cache? Data that must always be exact (like an account balance), or data that changes very quickly.

Follow-up question: What is the difference between a local (In-Memory) cache and a Redis cache? If we use a local cache instead of Redis and we have 5 servers, what problem happens?

Follow-up answer:

  • A local cache is in the memory of the same server. It is very fast, because there is no network. But each server has its own separate copy.
  • The problem: the user edits their profile. Only the server that got this request deletes its own cache. The other 4 servers show old data until the TTL ends. So with each refresh, the user may see old or new data.
  • First way: a short TTL for the local cache.
  • Second way: send a “delete this key” message to all servers (for example, with Redis Pub/Sub).
  • Third way: combine two levels, meaning a local cache in front of Redis.

Red flag: The candidate has not thought at all about deleting the cache after a data change, or about Redis going down.

Report a problem or send a better text

Edit on GitHub

5Messaging with Kafka: lost messages and duplicate messagesMedium to hard–KafkaIdempotencyOutbox and Inbox

Question: After an order is placed, we send the “order placed” message (OrderCreated) to Kafka. The Notification service reads this message and sends an SMS to the customer. The order code is this:

db.Orders.Add(order);
await db.SaveChangesAsync();
await producer.ProduceAsync("orders", new Message<string, string> { Key = order.Id, Value = json });

The services run on Kubernetes with several pods, and we deploy several times each week. You see this code and this design in code review. What do you think? Does the customer always get exactly one SMS? Why?

Short answer:

  • Problem 1 (the message gets lost): writing to the database and writing to Kafka are two separate jobs and do not share a transaction. The fix is the Outbox Pattern.
  • Problem 2 (duplicate message): Kafka delivers a message “at least once”, not “exactly once”. The fix is to make the consumer Idempotent.

Hint (if the candidate says there is no problem): We have two complaints from customers. First, some customers placed an order but got no SMS. The order is in the database, but its message is not in the topic. Second, some customers got two SMS messages for one order. Why?

Problem 1: Why does the message get lost? (Dual Write)

  1. Saving to the database succeeds and the order is stored.
  2. Right after that, the pod restarts (for example, because of a deploy), or Kafka is not reachable for a few seconds.
  3. Sending the message to Kafka does not run or gives an error. The order exists, the message does not, and nobody notices.
  4. If we change the order (Kafka first, then the database), the problem flips: a message goes out for an order that is not in the database.

Solution: Outbox Pattern

  1. In the same database transaction, we save both the order and a row in the Outbox table (the message text). The database makes sure that either both are saved, or neither.
  2. A BackgroundService reads the unsent Outbox rows and sends them to Kafka. Then it marks them as “sent”.
  3. Even if Kafka is down for a few minutes, the messages stay in the Outbox and are sent later. So no message is lost.

Note: if the worker sends the message and crashes before marking it, next time it sends the same message again. So the Outbox also creates duplicate messages. This brings us to problem 2.

Another way instead of the Outbox is a CDC tool (like Debezium) that reads the database changes.

Problem 2: Why are two SMS messages sent?

  1. The Notification service reads the message and sends the SMS.
  2. Before it tells Kafka “this message is done” (that is, commits the offset), the pod dies or a rebalance happens.
  3. So Kafka thinks the message was not processed and gives it again. A second SMS is sent.
  4. This behavior is not a bug in Kafka. Delivery in Kafka is at-least-once. So we must always expect duplicate messages.

Solution: an Idempotent consumer

Idempotent means this: if one message is processed two times, the result is the same as processing it once.

  1. Each message has a unique ID (for example, MessageId or OrderId).
  2. Before sending the SMS, we record this ID in the ProcessedMessages table. This table has a unique constraint on the ID.
  3. If the insert fails with a unique error, it means the message was processed before. So we do not send the SMS.
  4. We commit the offset after successful processing, not before it. Because if we commit before processing and then crash, the message is lost.

Follow-up question: If the order of the messages of one order matters (for example, “placed” before “canceled”), how does Kafka keep the order?

Follow-up answer: Kafka guarantees order only inside one partition. If we set the message Key to the order ID, all messages of one order go to one partition. So they are also read in the same order.

Red flag: The candidate says “Kafka is exactly-once by itself and we have no duplicate messages”, or their only fix for problem 1 is “try/catch and retry”.

Report a problem or send a better text

Edit on GitHub

6Fixing slowness in ProductionHard–Distributed tracingStructured logging

Question: At 10 AM we deployed a new version of the API. Since then, the monitoring dashboard shows that the API response time went from about 100 milliseconds to 3 seconds. But server CPU is only 20%, memory is normal, and there is no special error in the log. What do you do, step by step?

Short answer: First we save the user (rollback). Then we find the cause with data, not with guesses. The main clue is this: “low CPU but slow” means the app is not busy computing; it is waiting for something.

Steps and the reason for each one:

  1. Roll back the version (Rollback). The problem started exactly with the deploy. So the fastest way for the user is to go back to the previous version. Finding the cause is still possible later.
  2. See what changed. Look at the diff of the new version: a new query, a new call to a service, a new library, or a settings change.
  3. Find where the time is spent, with tracing. A distributed tracing tool (like OpenTelemetry) shows how much of these 3 seconds is in the database, how much in another service, and how much inside the app itself.
  4. See what the app is waiting for. We have four main candidates:
    • Thread pool starvation (like question 2): in the dotnet-counters tool, the thread pool queue length is high and the number of threads grows slowly.
    • A slow query or a lock in the database: in the database itself, look at the list of running queries and locks.
    • A slow outside service: in tracing we see that the HTTP call to that service took most of the time.
    • The connection pool runs out, for the database or HttpClient: requests wait for a free connection. For example, when the new code does not dispose the connection.
  5. Take a dump, if the cause is still not clear. Take a dump of the process with the dotnet-dump tool. Then see on which line of code all threads are waiting. If hundreds of threads are waiting on Result or on a lock, the cause is found.
  6. After finding the cause: fix, load test, deploy again, and a short report (postmortem). The goal is to find out sooner next time (for example, with an alert on latency).

Follow-up question: How do you know the problem is from GC (garbage collection)?

Follow-up answer: In the dotnet-counters tool, the candidate looks at two numbers:

  1. The percent of time in GC. If it is very high, the app keeps pausing for GC.
  2. The number of generation 2 GCs. This is the most expensive kind of GC.

If memory also keeps growing after each GC, we probably have a memory leak (question 35). Of course, in this scenario CPU is low, so GC is less likely. Because heavy GC usually pushes CPU up.

Red flag: Their first answer is “we add more servers”, or they start changing code based on guesses, without data.

Report a problem or send a better text

Edit on GitHub

7Choosing between Monolith and MicroservicesHard–Architecture stylesMicroservices

Question: A team of 4 people wants to build a new online shop. One team member suggests building 10 separate microservices from the start (user, product, inventory, order, payment, shipping, and so on). Each one has its own database. What do you think? When do you split a system into microservices, and when not?

Short answer: For a small team and a new product, a Modular Monolith is usually better. The reason is simple:

  1. Microservices solve an organizational problem: several teams that want to work independently.
  2. In return, they have a large technical cost.
  3. A team of 4 does not have this organizational problem. So it only pays the cost and gets no benefit.

The cost of microservices, with a real example: Placing an order must also reduce the inventory stock.

  • In a Monolith: both jobs are done in one database transaction. Either both succeed, or neither. It is only a few lines of code.
  • In microservices: order and inventory are two separate services with two separate databases. We have no shared transaction. If the order is saved and then the call to the inventory service fails, the data becomes inconsistent. So we must build messaging, an Outbox, a Saga and compensating actions (questions 5 and 25).

Other costs of microservices:

  • Every call goes over the network. So it is slower and can fail.
  • To find one bug, you must look at the logs of 10 services. So tracing is needed.
  • Instead of one pipeline and one deploy, we have 10.
  • DevOps work grows a lot.

What does Modular Monolith mean?

  • We have one app and one deploy. But the code is split into separate modules (for example, each module is one project).
  • Each module has its own tables. Another module is not allowed to touch these tables directly. It talks to the module only through a public interface.
  • First benefit: today the system is simple.
  • Second benefit: if one day a module really needs to be split out, its boundary is already clear. So splitting it is easy.

When do we split out a service? Only when we have a real reason:

  • We have several separate teams that do not want to wait for each other to deploy.
  • One part has a very different load and must scale separately. For example, product search has 100 times the load of the rest.
  • One part has a different technical need. For example, it needs a different technology, or it must work even when the rest is broken.

Where do service boundaries come from? From the Bounded Context in DDD, not from database tables. Example:

  • In the catalog part, “product” means name, picture and description.
  • In the inventory part, “product” means count and shelf.
  • So these are two separate models with two separate owners.

A common mistake: a shared database between services.

  1. Suppose two services use one table.
  2. If one column changes, both break.
  3. So we must deploy both together.
  4. Result: we have all the costs of microservices and none of the benefits. This is called a Distributed Monolith.

Follow-up question: When two services talk to each other, when do you choose sync communication (like HTTP or gRPC) and when async communication (messages with Kafka or RabbitMQ)?

Follow-up answer:

  • Sync communication is for when we need the answer right now. Example: the payment page must know the final price right now.
  • Async communication is for when the job can be done a few seconds later. Example: sending an email after an order. If the email service is down for a few minutes, placing the order must not break.
  • Why is a long sync chain bad?
    1. Suppose each service is healthy 99.9% of the time.
    2. One request passes through 5 services one after another.
    3. The probabilities are multiplied. So the total success probability becomes about 99.5%.
    4. The longer the chain, the more fragile the system.

Red flag: The candidate thinks microservices are always better and cannot name any specific cost for them.

Report a problem or send a better text

Edit on GitHub

8System design: Flash SaleDesignWeight ×2–System designRedis

Question: At 12 noon, an online shop sells 1000 phones at a discount. In the first minute, about 200 thousand users come in and press the buy button many times. Design the order system. We have three conditions:

  1. No more than 1000 units are sold.
  2. The system does not go down.
  3. Each user buys only one unit.

Ask the candidate to draw it on the whiteboard. If they ask questions before designing (for example, “is payment also in this same flow?”), that itself is a plus.

Short answer: We have three main ideas:

  1. Most users (199 thousand people) will not buy anything anyway. So we must reject their requests cheaply and early.
  2. The most important part is reducing the stock with an atomic operation. This way two people do not buy the last phone together.
  3. Heavy jobs (creating the order, the invoice, the email) are done later with a queue.

First, let’s see the main problem (overselling). This code is wrong:

var stock = await db.Products.Where(p => p.Id == id).Select(p => p.Stock).FirstAsync();
if (stock > 0)
{
    await db.Database.ExecuteSqlAsync($"UPDATE Products SET Stock = Stock - 1 WHERE Id = {id}");
}

Why is it wrong? Step by step:

  1. Suppose only one phone is left.
  2. Two requests arrive at the same moment.
  3. Both read the stock and see the number 1.
  4. Both see the condition as true, and both reduce the stock.
  5. The stock becomes -1 and one extra phone is sold.

This is called a Race Condition. There is a gap between “read” and “write”, and another request comes in the middle of this gap.

A good design (step by step):

  1. Make the needs clear. Is payment in this same flow? How much time does the user have to pay?
  2. Reduce the load before it reaches the server. The product page and pictures should be on a CDN. Set a limit on the number of requests (Rate Limiting) for each user and each IP. Why? Because most requests are refreshes and repeated clicks, and they must not reach the database.
  3. Reduce the stock atomically (the most important part). We have two ways:
    • First way, with Redis: the stock is kept in Redis and reduced with the DECR command. Redis runs commands one by one. So two DECR commands never run in the middle of each other. If the number goes negative, we answer “sold out”.
    • Second way, with the database: checking the stock and reducing it are done in one UPDATE statement. The condition “stock greater than zero” is inside that same statement (code below). The database runs this one statement atomically. If the number of changed rows is zero, it means the stock is finished.
UPDATE Products SET Stock = Stock - 1
WHERE Id = @id AND Stock > 0;
  1. One purchase per user. Put a unique constraint on the pair of user and flash sale. Or in Redis, create a key for each user that is created only if it does not exist (the SET command with the NX option). Important point: the user check and the stock reduction must be done together (with a Lua script in Redis or one database transaction). Otherwise the stock may be reduced, but the purchase is rejected because the user is a duplicate. In this case one phone gets lost.
  2. Separate the reservation from the heavy work. After a successful reservation, only send one message to the queue (Kafka or RabbitMQ) and tell the user “reserved”. Other services build the order, the invoice and the email at their own speed. Why? A reservation in Redis takes a few milliseconds, but building a full order is slower. The queue smooths out the peak load.
  3. Payment with a deadline. A reservation is valid for, for example, 10 minutes. If it is not paid, the phone goes back to the stock.
  4. Handle duplicate requests. The user may click twice. The client sends an Idempotency Key. For a repeated key, the server returns the same earlier answer (question 21).
  5. Monitoring and testing. Before the sale day, run a load test. Have a dashboard for stock and errors. Also add a switch to turn off the sale quickly (feature flag).

Follow-up question 1: If Redis restarts in the middle of the sale, what happens to the stock?

Answer: If Redis has no persistence, the stock number is lost. We have two ways:

  • The database is the main source of truth. Each reservation is also recorded in the database. This way we can calculate the stock again (1000 minus the number of reservations).
  • Or we run Redis with persistence and a replica.

Follow-up question 2: If the order “first come, first served” must be exact, what do you do?

Answer: We build an entry queue (virtual waiting room). Each user gets a turn number and is let into the buy page in order.

Red flag: The candidate checks the stock with two separate commands (first read, then reduce), or their only answer is “more servers and pods”.

Report a problem or send a better text

Edit on GitHub

9Changing one record at the same time: Lost UpdateMedium–Transactions and isolation levels

Question: In the support panel, several staff members work together. When saving, the order edit page sends all of the order data with one PUT request. The server writes all fields to the database. Suppose two staff members open the page of one order at the same time. The first one changes the address and saves. The second one, a few seconds later, changes the status to “shipped” and saves. How do you see this design? What data stays in the database at the end? Does it have a problem?

Short answer: The form of the second staff member still had the old address. The PUT request sends all fields. So the old address was written over the new address. The solution is Optimistic Concurrency: each record has a version number. If the version has changed, the save is rejected.

Hint (if the candidate says there is no problem): We have seen this in Production: the first staff member changed the address and got the message “saved”. But after the second staff member saved, the new address was gone. The package was sent to the old address. There is no error in the log either.

What happened? (in time order)

Time Staff A Staff B Data in the database
1 Opens the form Address: Tehran, Status: New
2 Opens the form (address Tehran) Address: Tehran, Status: New
3 Changes the address to Tabriz and saves Address: Tabriz, Status: New
4 Changes the status to “shipped” and saves (the form still has address Tehran) Address: Tehran, Status: Shipped

The last write won (last write wins). The change of A was lost silently.

Why does “we add a transaction” not help?

  • These are two separate requests.
  • There are a few minutes between opening the form and saving.
  • Each save on its own is successful and correct.
  • The problem is that B saves with stale data.

Solution: Optimistic Concurrency with EF Core

  1. We add a version column to the table. With each change to the row, SQL Server changes its value automatically:
public class Order
{
    public int Id { get; set; }
    public string Address { get; set; } = "";
    [Timestamp] public byte[] RowVersion { get; set; } = [];
}
  1. When the form opens, it also gets the version value (RowVersion). When saving, it sends it back.
  2. When saving, EF Core adds a condition to the UPDATE statement: “only if the version is still the same old version”.
  3. In the example above, the save of A changed the version. So the condition is not true for B. No row is updated, and EF Core gives a DbUpdateConcurrencyException error.
  4. In this case the API returns a 409 Conflict response. The form tells B: “Someone else changed this order, please open it again.” What we do next (reload or merge the changes) is a business decision.

Other ways and their limits:

  • Pessimistic Lock: lock the record while the form is open. This is not good for a form that the user keeps open for several minutes. For example, if the user closes the browser, what happens to the lock?
  • Sending only the changed fields (PATCH): in this example it solves the problem. But if both people change the same field, one change is still lost.

Follow-up question: In a REST API, with what standard way do the server and the client know that the data the client has is stale?

Follow-up answer: With the ETag header. Steps:

  1. In the GET response, the server sends the version in the ETag header.
  2. On PUT, the client sends the same value back in the If-Match header.
  3. If the version has changed, the server returns a 412 Precondition Failed response.

Red flag: The candidate says “we add a transaction” or “we add a lock” and does not notice that these are two separate requests with time between them.

Report a problem or send a better text

Edit on GitHub

10Paging on a big tableMedium–SQL and indexes

Question: The orders table has 50 million records. The order list API pages with OFFSET and FETCH (in EF Core, the same as Skip and Take). Each page has 20 records, and the newest orders come first. Several new orders are placed every second. The code is this:

var page = await db.Orders
    .OrderByDescending(o => o.CreatedAt)
    .Skip((pageNumber - 1) * 20)
    .Take(20)
    .ToListAsync(ct);

How do you see this paging method? With this amount of data and this number of new orders, what happens in Production?

Short answer: For OFFSET, the database must read all records before that page and throw them away. The duplicates happen because, between two requests, a new order was added and all records moved down by one place. The solution is Keyset Pagination (or Cursor Pagination).

Hint (if the candidate says there is no problem): We have seen two things in Production. First, the first pages are fast, but page 50000 takes several seconds. Second, users say that when they open the next page, sometimes the last order of the previous page shows up again at the top of the new page.

Problem 1: Why are the last pages slow?

  1. Page 50000 means “skip 999980 rows, then give 20 rows”.
  2. The database cannot jump directly to row 999980. It must start from the beginning of the index.
  3. It counts about one million rows and throws them away. Then it returns 20 rows.
  4. So the bigger the page number, the more work and the slower it is.

Problem 2: Why is a duplicate record shown?

  1. The user sees page 1: orders 1 to 20.
  2. At that moment a new order is placed and sits at the top of the list. Now all orders have moved down by one place.
  3. The user asks for page 2 (skip 20 rows). Row 21 is now the old order 20. So it is shown again.
  4. The other way around, if a record is deleted, one record is never shown at all.

Solution: Keyset Pagination

Instead of “which page number?”, we say “after the last record I saw, give me the next 20”:

var page = await db.Orders
    .Where(o => o.CreatedAt < lastCreatedAt
             || (o.CreatedAt == lastCreatedAt && o.Id < lastId))
    .OrderByDescending(o => o.CreatedAt).ThenByDescending(o => o.Id)
    .Take(20)
    .ToListAsync(ct);

Why is this way better?

  • With an index on the creation time and the order ID, the database jumps directly to the right place in the index (seek). It reads only 20 rows. So page 1 and page 50000 have the same speed.
  • A new order at the top of the list does not move our starting point. So duplicates do not happen.
  • Why is the order ID also needed? Because two orders may have the same creation time. Without the ID, the order is not unique, and a record may still be skipped or repeated.
  • We give the client a “cursor”. For example, the same two values (time and ID) as a base64 string. The client sends it back in the next request.

Another point: counting all rows (to show “page 1 of 2.5 million”) is also expensive on a big table. Usually an approximate number is enough.

Follow-up question: With your method, can the user go directly to page 47? How do you explain this to the Product Owner?

Follow-up answer: No. The Keyset method only has “next page” and “previous page”. It is great for infinite scroll and APIs. We tell the Product Owner: “Nobody wants page 47 because of its number. They are looking for a specific order. It is better to give a date filter and search.” If page numbers are really needed, we keep OFFSET only for the first few pages (for example, at most 100 pages).

Red flag: The candidate only says “we add an index” and does not know why OFFSET is slow even with an index.

Report a problem or send a better text

Edit on GitHub

11Safe shutdown in KubernetesMedium to hard–KubernetesBackground services

Question: Our service runs in Kubernetes. It both answers HTTP requests and reads messages from Kafka. Traffic is high, and we deploy several times a day with a rolling update. We have no special settings for app shutdown. Everything is on the default settings of Kubernetes and ASP.NET Core. During a deploy, when an old pod shuts down, what happens to the requests and messages that are being processed? How do you design a safe shutdown (graceful shutdown)?

Short answer: When Kubernetes shuts down a pod, the app must finish its half-done work and then close. The main problem is this:

  1. At one moment, it tells the app “shut down”.
  2. At the same time, it tells the load balancer “do not send traffic to this pod anymore”.
  3. But the second one takes effect a few seconds later.

Hint (if the candidate says there is no problem): We have seen this in Production: each time we deploy, in the first few seconds some users get a 502 error. Some Kafka messages are also processed twice.

First, let’s see how Kubernetes shuts down a pod:

  1. It sends the SIGTERM signal to the process. It means: “Please finish your work and close.”
  2. At the same time, it removes the pod from the service’s Endpoints list. This change takes a few seconds to reach kube-proxy and the ingress.
  3. It has a deadline called the termination grace period (default 30 seconds). If the app has still not closed after this deadline, it sends SIGKILL and the process is killed at once.

Why do we see a 502 error?

  • Steps 1 and 2 start at the same time.
  • With SIGTERM, the app starts to close.
  • But the load balancer still sends new requests to this pod for a few seconds.
  • These requests reach an app that no longer accepts requests.

Why do we see duplicate messages? A message that was in the middle of processing did not finish, or its offset was not committed, and the process was killed. So another pod gets the same message again.

Solution (step by step):

  1. Wait a few seconds before closing. We add a preStop hook. Kubernetes runs this first, then sends SIGTERM. In these few seconds the load balancer has time to remove the pod from the list:
lifecycle:
  preStop:
    sleep:
      seconds: 5
  1. Finish the current requests. With SIGTERM, the ASP.NET Core app no longer takes new requests and waits for the running requests. The maximum time for this wait is set by the ShutdownTimeout setting in HostOptions.
  2. Do the time math. The preStop time plus ShutdownTimeout must be less than the termination grace period. For example, 5 + 30 = 35, so we set the grace period to 45 seconds. Otherwise SIGKILL arrives in the middle of the work.
  3. Background work. In a BackgroundService, we pass the stop token (stoppingToken) to all jobs. This way, with SIGTERM, no new job starts.
  4. The Kafka consumer. First the current message finishes. Then its offset is committed. Then the consumer is closed with the Close method. This way Kafka quickly gives the partitions to another pod.

Even with all of this, a pod sometimes dies suddenly (for example, OOM or a node failure). So message processing must be idempotent (question 5).

Follow-up question: What is the difference between a readiness probe and a liveness probe? If the liveness probe also checks the database, what problem happens?

Follow-up answer:

  • The readiness probe asks: “Can I take traffic right now?” If the answer is no, traffic just does not go to this pod.
  • The liveness probe asks: “Is the app alive, or is it stuck?” If the answer is no, Kubernetes restarts this pod.
  • Now suppose liveness checks the database and the database becomes slow for a few seconds. In this case all pods restart together. A small database problem turns into an outage of the whole service.
  • So the liveness probe must check only the process itself.

Red flag: The candidate does not know what SIGTERM is, or thinks Kubernetes waits by itself until all work is finished.

Report a problem or send a better text

Edit on GitHub

12Hot Path code and memoryHard–Performance and SpanMemory and the Garbage Collector

Question: A service receives price messages as text and parses them. Each message has three parts separated by commas: symbol, price and volume (for example, AAPL and 123.45 and 100). This code runs 50 thousand times per second:

var parts = line.Split(',');
var quote = new Quote(parts[0], decimal.Parse(parts[1]), int.Parse(parts[2]));

You see this code in code review. What do you think? With this load, what happens in Production? Would you change anything in it?

Short answer: Step by step:

  1. Each run of this code creates several new objects on the heap (one array and three strings).
  2. 50 thousand times per second means hundreds of thousands of objects per second that become garbage at once.
  3. The GC system must keep collecting them. Some GCs pause the threads.
  4. Solution: reduce allocation with tools like Span.

Hint (if the candidate says there is no problem): We have seen this in Production: processing time is good most of the time. But every few seconds, everything stops for a few hundred milliseconds. The dotnet-counters tool also shows that the number of GCs is very high.

Why does a lot of allocation cause pauses? (step by step)

  1. Each time we create a new object (and each time we call the Split or Substring method), an object is created on the heap.
  2. A new object goes into generation 0 (Gen0). Generation 0 is small, and at this speed it fills up very quickly.
  3. When it is full, GC runs. A generation 0 GC is fast. But if it runs very often, its total time becomes significant.
  4. Objects that are still alive during GC move to generations 1 and 2. When generation 2 fills up, a full and expensive GC runs. This GC can take a few hundred milliseconds. These are the same “pauses” we saw.
  5. So the less garbage we create, the less often GC runs.

Solution:

  1. Measure first. Find where most of the allocation is with the dotnet-trace tool or PerfView. Compare before and after with BenchmarkDotNet and its MemoryDiagnoser feature.
  2. Parse without creating strings and arrays. For this, use ReadOnlySpan. A Span is only a “window” on the same original string and copies nothing:
ReadOnlySpan<char> s = line;
int i = s.IndexOf(',');
var symbol = s[..i];
s = s[(i + 1)..];
int j = s.IndexOf(',');
var price = decimal.Parse(s[..j], CultureInfo.InvariantCulture);
var volume = int.Parse(s[(j + 1)..]);
  1. Also look at other common sources of allocation:
    • Joining strings in a loop.
    • Using LINQ and lambdas in hot code.
    • Turning a struct into an object (boxing).
    • Creating a List without an initial capacity.
  2. Reuse buffers. With ArrayPool, borrow an array (Rent) and give it back after the work (Return). This is especially important for arrays bigger than about 85 kilobytes. Because these arrays go directly to the Large Object Heap, and collecting them is expensive.
  3. Check GC settings last. Look at settings like Server GC after fixing the code, not before.

Follow-up question: When do you use ValueTask instead of Task? What things must you not do with ValueTask?

Follow-up answer:

  • The Task type is a class. Usually, each call of an async method creates an object on the heap.
  • Sometimes a method finishes without waiting most of the time (for example, it answers from the cache 99% of the time) and it is in hot code. In this case ValueTask removes this allocation.
  • Limits of ValueTask:
    • Await it only once.
    • Do not await it from two places at the same time.
    • Do not read its result (Result) before the work is finished.
    • If you need any of these, first turn it into a Task with the AsTask method.
  • The default is still Task, because it is simpler and safer.

Red flag: The candidate starts “optimizing” without measuring, or cannot explain why a lot of allocation causes slowness.

Report a problem or send a better text

Edit on GitHub

13A Job that runs again on several PodsHard–Background servicesRedis

Question: Every night at 2 AM, a BackgroundService builds a financial report and emails it to customers. To handle more load, we want to raise the number of pods of this service from 1 to 3. We do not change the Job code. Does this change cause a problem? Why?

Short answer: Each pod runs a full copy of the app. So each pod has its own BackgroundService. 3 pods means 3 schedulers. Each one starts the job at 2 AM. Solution: a shared mechanism between the pods that lets only one of them run.

Hint (if the candidate says there is no problem): We have seen this in Production: since the day after we made it 3 pods, each customer gets 3 identical emails every night.

Ways, from simple to complex:

  1. Separate the Job from the service. Create a CronJob in Kubernetes. This CronJob runs a short-lived pod every night. Set its concurrencyPolicy to Forbid, so two runs do not start together. This is the simplest way, because we no longer have several copies.
  2. Record the run in the database with a unique constraint. We have a table for runs. We put a unique constraint on “Job name and date”. Before starting, each pod adds a row for today to this table:
INSERT INTO JobRuns (JobName, RunDate) VALUES ('daily-report', '2026-10-04');
-- UNIQUE (JobName, RunDate)
  • Only the first write succeeds. The other two pods get a unique error and drop the work. This way is simple and safe, because the database itself guarantees it.
  1. A database lock. In SQL Server the sp_getapplock feature, and in PostgreSQL the advisory lock feature, do this. This lock is tied to the connection. If the pod dies, the connection closes and the lock is released automatically.
  2. A Redis lock with the SET command and the NX option (only if the key does not exist) and an expiry time. Or a ready-made tool like Hangfire or Quartz.NET in cluster mode.
  • In all cases, it is better if sending the email itself is also idempotent. This means for each customer and each date we record “sent”. If the Job fails in the middle and runs again, nobody gets an email twice.

Follow-up question: Suppose you took a lock with Redis and the lock TTL is 30 seconds. The first pod stops for 40 seconds in the middle of the work (for example, because of a long GC or a network problem). What happens?

Follow-up answer:

Time pod A pod B
0 Takes the lock until second 30 and starts the work Waiting
5 Stops (GC pause)
30 Still stopped; the lock expires
31 Takes the lock and starts the work
45 Wakes up and thinks it still has the lock; continues Working at the same time
  • Now two pods work at the same time.
  • A longer TTL only lowers the chance. It does not solve the problem.
  • Renewing the lock during the work also helps. But the renewal itself may not happen because of this same pause.
  • The right solution is a Fencing Token. Step by step:
    1. Each time someone takes the lock, they also get an increasing number. For example, A gets the number 33 and B the number 34.
    2. Each write to the database carries this number with it.
    3. The database keeps the biggest number it has seen. It rejects a write whose number is smaller.
    4. So after B starts (number 34), the writes of A (number 33) are rejected.
  • If the candidate knows the debate about Redlock and the critique by Martin Kleppmann, that is a sign of very good depth.

Red flag: The candidate only says “we take a lock with Redis” and has never thought about the lock expiring in the middle of the work.

Report a problem or send a better text

Edit on GitHub

14Retry stormHard–Resilience with Polly

Question: Service A calls service B, and B calls service C. In both layers (A to B and B to C), this is set with Polly: 3 retries and a timeout of two seconds for each try. The normal response time of C is about 200 milliseconds. You see this setting in code review. What do you think? Does it have a problem? If C gets slow and its response time reaches about 2 seconds, what happens? What is the right setting?

Short answer: The number of retries in the layers is multiplied. One user request can send up to 16 requests to C. When C is slow, these extra requests make it even slower. A bad cycle starts.

Hint (if the candidate says there is no problem): One day C got a little slow, and its response time went from 200 milliseconds to about 2 seconds. In monitoring we saw that the number of incoming requests to C grew several times, while the number of users was the same. After a few minutes, C went down completely. Then A and B also stopped answering, even for work that has nothing to do with C. Why?

Why does this happen? (step by step)

  1. Retries multiply. Service A calls B up to 4 times (1 original call and 3 retries). In each of these 4 calls, B calls C up to 4 times. This means 4 × 4 = 16 requests to C for one user click.
  2. The timeout is right at the edge. Service C answers in about 2 seconds, and the timeout is also 2 seconds. So most tries time out and a retry starts. Important point: C is still processing the earlier request. This means it does repeated work.
  3. The bad cycle. More load on C means C gets slower. Slower C means more timeouts. More timeouts mean more retries. And more retries mean even more load on C. This goes on until C goes down completely.
  4. Cascading failure. All threads and connections of A and B wait for C. So A and B do not answer other users either. Even for work that has nothing to do with C.

The right setting:

  • Only one layer retries, not all layers.

  • The gap between retries grows step by step. This is called exponential backoff: 200 milliseconds, then 400, then 800. Also add a small random number (jitter), so thousands of clients do not retry at the same moment.

  • Retry only for temporary errors: timeouts and the codes 503 and 429. Do not retry for an error like 400, because repeating does not fix it.

  • The right time budget. The timeout of the upper layer must be longer than the total time of the lower layer (with its retries). Otherwise A gives up, but B is still working on the same request.

  • Use a Circuit Breaker. It is like an electric fuse:

    1. If a large percent of requests to C fail, the circuit opens.
    2. For example, for 30 seconds we do not call C at all and return an error at once.
    3. During this time C gets a chance to recover.
    4. Then we send one test request. If it succeeds, we send requests to C again.
  • Use a Bulkhead, meaning limit the number of concurrent requests to C. This way one slow dependency does not take all threads and connections of B.

  • In .NET, the Microsoft.Extensions.Http.Resilience library (which is built on Polly) has most of these with good default settings:

builder.Services.AddHttpClient<ServiceCClient>()
    .AddStandardResilienceHandler();

Follow-up question: When the Circuit Breaker is open, what answer do you give the user?

Follow-up answer: It depends on the kind of work. This is called a fallback:

  • For reading data (for example, a price list): we show the last cached data and say “it may be a little old”.
  • For non-core parts (for example, product suggestions): we hide that part for a while, but the rest of the page works.
  • For writes and important work (for example, placing an order): we return a 503 error at once, with a Retry-After header. We do not pretend the work is done.
  • The main point: a fast error is better than a long wait, because threads and connections stay free.

Red flag: Their solution is “we increase the number of retries” or “we make the timeout bigger”.

Report a problem or send a better text

Edit on GitHub

15Retrying requests in a payment systemHard–IdempotencyResilience with Polly

Question: For each purchase, our payment service calls the bank gateway (PSP). The timeout of this call is 10 seconds. If no answer comes, we send the same request again with Polly. If no answer comes after all tries, we mark the order as “failed”. You see this design in code review. What do you think? Does it have a problem? On a busy day when the bank answers slowly, what happens? How do you design calling the payment gateway again?

Short answer: A timeout means “I got no answer”, not “the payment did not happen”. Maybe the bank took the money and only its answer did not reach us. So a blind retry means paying again. The right way: one unique ID for each payment, check the status before trying again, an “unknown” status in the database, and a reconciliation job with the bank report.

Hint (if the candidate says there is no problem): A few days after a busy day when the bank was slow, support reports two kinds of complaints. Some customers say money was taken from their account twice, but they have only one order. Some other customers say money was taken, but their order is “failed” in our system. In the bank transaction report we also see two successful transactions for some purchases. Why?

Why was the money taken twice? (step by step)

  1. We send the payment request to the bank.
  2. The bank takes the money, but because it is busy, its answer arrives after 10 seconds.
  3. After 10 seconds we get a timeout and think the payment did not happen.
  4. Polly sends the same request again. The bank sees it as a new payment and takes the money again.
  5. Result: two successful transactions in the bank, but one order in our system.

Why was the money taken but the order is “failed”?

  1. The bank took the money, but all our tries timed out.
  2. After the last try, our code marked the order as “failed”.
  3. This means we treated the answer “I don’t know” as the answer “no”.

The right design:

  1. A unique ID for each payment. Before calling the bank, we create a payment ID and save it in the database. In all tries, we send this same ID to the bank. Most gateways, if this ID is a repeat, do not create a new payment and return the same earlier result.
  2. Three statuses, not two. A payment is not only “successful” and “failed”. An “unknown” status is also needed. A timeout means “unknown”, not “failed”.
  3. First check, then try again. After a timeout, we do not send the payment request again. First we ask the bank with the same ID: “What is the status of this payment?”
    • If the bank says it was successful, we mark the order as successful.
    • If it says it has not seen this payment, only then do we send it again.
    • If the bank still does not answer, the payment stays in the “unknown” status.
  4. A reconciliation job. Every few minutes, a background job checks the “unknown” payments with the bank. Every day it also compares the bank transaction report with our database. If money was taken but we have no order, it either completes the order or gives the money back (refund).
  5. Few retries, with gaps. For payments, the number of tries is small, and the gaps grow step by step (exponential backoff). We do not retry in several layers together (like question 14).
  6. The right message to the user. In the “unknown” status, we do not tell the user “the payment failed”. We say “the payment is being checked”, so they do not pay again themselves.

An important point: We must save the unique ID in the database before calling the bank. If our service restarts in the middle of the work, after it comes back up it knows which payments were started and must be checked.

Follow-up question: If the bank gateway does not support a unique ID and status checks at all, what do you do?

Follow-up answer:

  • In this case, we turn off automatic payment retry. The risk of paying twice is bigger than the risk of one failed purchase.
  • After a timeout, the payment stays in the “unknown” status.
  • Only the daily reconciliation job with the bank report decides the final status.
  • If money was taken twice, we give it back with an automatic refund.
  • We also tell the product team about this risk, because it affects the user experience.

Red flag: The candidate says “we make the timeout longer” or “we add more retries”, or marks the payment as “failed” after a timeout.

Report a problem or send a better text

Edit on GitHub

16A database with a Read ReplicaHard–SQL and indexesConsistency and CAP

Question: To reduce the database load, we added a Read Replica. All writes go to the main database (Primary) and all reads go to the Replica. How do you see this design? Does it work correctly for all parts of the system? For example, a user edits their profile and presses save. Then the profile page loads again. What happens? If there is a problem, what solutions do you have, and what does each one cost?

Short answer: Changes on the Primary reach the Replica with a small delay. This delay is called Replication Lag. Right after saving, the user reads from the Replica. That is, before the change has reached it. But the user expects to see their own change at once. This need is called Read-Your-Writes.

Hint (if the candidate says there is no problem): Users complain: “I edited my profile, pressed save and saw the success message. But the page still shows the old information.” After a few refreshes it is fixed. In busy hours this complaint is more common. Why?

What happens? (in time order)

  1. At time 0, the user presses save. The change is stored on the Primary and the API returns the “success” message.
  2. At 50 milliseconds, the profile page loads again and reads from the Replica.
  3. The change has not reached the Replica yet. Because replication is usually async and takes from a few milliseconds to a few seconds (more under heavy load). So old data comes back.
  4. A few seconds later, the change reaches the Replica and the next refresh is correct.

Solutions and the cost of each one:

Solution How it works Cost
Show from the save response After saving, the page shows the data from the response of that same save request and does not read again Almost nothing; but it only fixes that one page
Read from the Primary for a few seconds For, say, 10 seconds after each write, reads of the same user go to the Primary (with a timed flag in a cookie or cache) Low and simple; the most common way
“My own data” pages from the Primary Pages where users edit their own data always read from the Primary Part of the load stays on the Primary
Wait for the Replica After a write, we keep the log position (like the LSN in PostgreSQL). We read only from a Replica that has reached this position Exact, but complex
Synchronous (sync) replication The Primary server waits until the Replica confirms Writes get slower; if the Replica goes down, writes also get stuck
  • We must also monitor the replication lag and set an alert for it.

Follow-up question: Which parts of the system must never read from the Replica?

Follow-up answer: Any place where we make a decision with the data we read and then write.

  • Example: checking an account balance before a withdrawal. If we read from the Replica, we may not see a withdrawal from a few seconds ago. So we may allow a withdrawal bigger than the balance.
  • Other cases: permissions and password changes, creating unique numbers, and any read that is inside a write transaction.

Red flag: The candidate does not know the idea of replication lag, or says “the problem is the browser cache”.

Report a problem or send a better text

Edit on GitHub

17Changing the database schema without DowntimeDesignWeight ×2–Entity Framework CoreCI/CD

Question: The customers table has a “full name” column (FullName). We want to turn it into two columns: “first name” (FirstName) and “last name” (LastName). The service has 4 pods and deploys are rolling. This means the pods are replaced one by one, and for a few minutes the old and new versions of the code run together. A developer wrote a migration that does three things at once: it creates the two new columns, copies the data, and drops the full name column. You see this migration in code review. Does this migration cause a problem during the deploy? Why? If there is a problem, how do you do it step by step?

Short answer: During a rolling deploy, the old and new code work together. So at every moment, the database must be compatible with both versions of the code. The solution is the Expand and Contract pattern:

  1. First, add the new thing.
  2. Keep both for a few steps.
  3. At the end, remove the old thing.

Hint (if the candidate says there is no problem): We did this same thing in an earlier deploy. In the middle of the deploy, some requests got a 500 error. In the log of the old pods we saw the error “column FullName does not exist”. Then the new version had a bug and we wanted to roll back. But the old version gave the same error and did not work. Why?

Why does a one-shot migration give errors?

  1. When the migration runs, the full name column is dropped.
  2. 3 pods still have the old code and read the full name column. The database gives the error “column does not exist” and the user gets a 500 error.
  3. If we do it the other way around, deploying the code first and then the migration, the new pods look for the first name and last name columns. But these columns do not exist yet.
  4. If the new version has a bug, rollback is also not possible. Because the old code needs the full name column, and that column is dropped.

Solution: Expand and Contract in 6 steps

Step Change Where does the code read from? Where does the code write?
1 Add the new columns as nullable (the code does not change) FullName FullName
2 Deploy code version 1 FullName Both
3 Fill old records in small batches (backfill) FullName Both
4 Deploy code version 2 New columns Both
5 Deploy code version 3 New columns Only the new columns
6 After a few days and when sure, drop the FullName column New columns Only the new columns
  • Why is this way safe? Because at every step, the previous version of the code still works with the database. So the rolling deploy has no errors. And going one step back (rollback) is always possible.

A few important points:

  • Fill the data (backfill) in small batches, for example 1000 rows each time. One big UPDATE on millions of rows locks the table for a long time and fills the transaction log.
  • Run the migration separately from app startup, for example with a Job in the pipeline. If all 4 pods call the Migrate method at startup, they may run at the same time and run into problems.
  • Be careful with heavy ALTERs. Some changes (like changing a column type) on a big table rewrite and lock the whole table.

Follow-up question: After step 4, a bug was found and we must go back to version 1. Does a problem happen? What if it is after step 6?

Follow-up answer:

  • After step 4 there is no problem. Version 1 reads from the full name column. This column still exists and is still being filled.
  • After step 6, the old column is dropped. So going back to version 1 is not possible.
  • That is why step 6 is the last step and is done separately, after some time.

Red flag: The candidate writes a migration with rename or drop and says “we deploy quickly, a few seconds of errors does not matter”.

Report a problem or send a better text

Edit on GitHub

18Exporting a large amount of dataMedium–Entity Framework CorePerformance and Span

Question: In the admin panel, a user clicks “Download Excel” to get 2 million transaction records. The current endpoint does three things: it reads all records at once with the ToListAsync method, builds the Excel file in memory, and returns it in the response. The memory limit of each pod is one gigabyte. You see this code in code review. What do you think? In Production, with this amount of data, what happens? If needed, how do you redesign this feature?

Short answer: We have two separate problems:

  1. All the data is in memory at once.
  2. A job of several minutes is done inside one HTTP request.
  • Solution: move the work to the background, read and write the data piece by piece, and give the ready file to the user later.

Hint (if the candidate says there is no problem): After about 100 seconds, the user sees a timeout error. Sometimes the pod also restarts with the OOMKilled status (out of memory). At that same moment, the requests of other users are also cut. Why?

Why do we get OOM?

  1. The ToListAsync method builds all 2 million records as C# objects in memory. If each object with its strings is about 500 bytes, that is about 1 gigabyte. The change tracker also keeps extra information for each object.
  2. Then the Excel file is also built fully in memory.
  3. If the pod memory limit is, for example, 1 gigabyte, Kubernetes kills it. All other requests that were on the same pod are also lost.

Why do we get a timeout? There are several layers between the browser and the app: the load balancer, the ingress and the reverse proxy. Each of them has a maximum time for one request. A job that takes several minutes should not be inside a request at all.

The right design (step by step):

  1. First, the endpoint only records an “export Job”. Then it returns the code 202 (Accepted) at once, together with a Job ID.
  2. A worker in the background (a BackgroundService or a queue consumer) runs this Job.
  3. It reads the data piece by piece, not all at once. With the AsNoTracking and AsAsyncEnumerable methods, each record is read, written and released from memory:
await foreach (var row in db.Transactions.AsNoTracking().AsAsyncEnumerable().WithCancellation(ct))
{
    writer.WriteRow(row);
}
  1. The file is also written as a stream (for example, with OpenXmlWriter). If the business accepts it, the CSV format is much simpler and lighter.
  2. The file is saved in a storage (like S3 or MinIO), not in the memory or disk of the pod.
  3. So that the load does not go to the main database, we read from a Read Replica.

A good point (a plus if the candidate says it): Each sheet in Excel has at most 1,048,576 rows. So 2 million rows do not fit in one sheet at all.

Follow-up question: How does the user know their file is ready? How do you make the download link secure?

Follow-up answer:

  • To notify: the page asks the server for the Job status every few seconds (polling). Or the server notifies with SignalR, email or a notification.
  • For security: the link is a Pre-signed URL with a short life (for example, 15 minutes). This link is created only after checking that this same user owns this Job.
  • The files are deleted automatically after a few days, because financial data is sensitive.

Red flag: Their answer is “we make the timeout longer” or “we give the pod more memory”.

Report a problem or send a better text

Edit on GitHub

19Request limiting (Rate Limiting) on several serversMedium–Rate limitingRedis

Question: We have a public API. By contract, each customer is allowed at most 100 requests per minute. We set this limit with the built-in ASP.NET Core middleware (the AddRateLimiter one). In Production, the service runs on 10 pods, and a load balancer spreads the requests between them. How do you see this design? In Production, is the same limit of 100 requests per minute kept? If not, how do you fix it?

Short answer: The built-in middleware keeps the counter in the memory of the same pod. So:

  1. Each pod counts up to 100 on its own and knows nothing about the other pods.
  2. The load balancer service also spreads the requests between 10 pods.
  3. So the real limit becomes 10 × 100 = 1000.
  • Solution: the counter must be in a shared place.

Hint (if the candidate says there is no problem): In the test on a laptop, everything worked correctly. But in Production monitoring we see that some customers send about 1000 requests per minute. These customers get no 429 error. Why?

Ways:

  1. A shared counter in Redis. The simplest version is the Fixed Window:
    • The key is built from the customer ID and the current minute. For example, “customer 42 at 10:30”.
    • With the INCR command we add one to the counter. The first time, with the EXPIRE command, we set an expiry time of 60 seconds. It is better to put both in one Lua script, so they run atomically.
    • If the number goes above 100, we return the code 429.
  2. A limit at the edge of the system. An API Gateway or Ingress is in front of all pods. It counts right here, in one place.
  3. An approximate way. The share of each pod becomes 100 ÷ 10 = 10. This way is simple, but it has two problems: it must change when the number of pods changes. And if the load balancer does not spread the load evenly, it is not exact.

The difference between algorithms (an important point):

  • In the Fixed Window, a customer can send 100 requests in second 59 of the first minute and 100 requests in second zero of the next minute. That is 200 requests in two seconds.
  • The Sliding Window and Token Bucket algorithms do not have this problem.
  • In the Token Bucket, each customer has a bucket of tokens. The bucket fills at a fixed speed. Each request takes one token. If there is no token, the request is rejected.

The right details:

  • The right answer is the code 429 (Too Many Requests) with a Retry-After header. With this header, the client knows when to try again.
  • The limit key must be the API Key or the customer ID, not the IP. Because several customers behind one NAT share one IP. And one customer may also have several IPs.

Follow-up question: If the Redis that keeps the counters becomes unavailable, do you accept or reject the requests? Why?

Follow-up answer: It depends on why the limit was set.

  • If the goal is to protect the system, we usually accept the request (fail-open). Together with a simple local limit as a backup. Because we do not want Redis going down to take down the whole API.
  • If the limit is financial or for security, maybe rejecting (fail-closed) is right. For example, when each request costs us money, or the limit stops password guessing.
  • A good candidate says “it depends” and explains why.

Red flag: The candidate does not know that the built-in limiter counts separately in each pod.

Report a problem or send a better text

Edit on GitHub

20Revoking access with JWTMedium–Authentication and authorization

Question: Our system uses JWT for authentication. Each token lives 1 hour. The security manager deactivates the account of an abusive user in the admin panel (the user’s active field becomes false in the database). In your opinion, after this, what happens to this user’s access to the API? Does this design have a problem? If yes, what ways do you have, and what does each one cost?

Short answer: To check a JWT, the server does not look at the database at all. It only checks the signature and the expiry date of the token itself. So deactivating the user in the database has no effect on a token that was already issued. This token works until it expires.

Hint (if the candidate says there is no problem): In API monitoring we see that after being deactivated, this user still calls the API. The responses are also successful. This goes on until about one hour later. Why?

How does JWT work?

  1. At login, the server creates a token. The user information (ID and roles) and the expiry time are inside the token. The server signs the token with its own key.
  2. In each request, the server only checks the signature and the expiry. It does not connect to any database. That is why it is fast. This is called stateless.
  3. This same benefit is the problem here. The token is valid until it expires, no matter what happens in the database.

Ways and the cost of each one:

Way How it works Cost
Short life + Refresh Token The access token is valid for only 5 to 15 minutes. For a new token, the client sends a Refresh Token. At that moment the server checks the user status in the database Simple, but there is still a gap of a few minutes
Denylist We keep the token ID (the jti field) or the user ID in Redis until expiry. We check it in each request One fast Redis check in each request
Security stamp A version number is inside the token. When the user is deactivated, this number changes in the database. Old tokens are rejected Must be checked in each request (with a short cache)
Reference Token The token is only an ID. The server asks the Identity Server each time Exact, but has more load and delay

The usual choice is a mix of the first and second ways. Most of the time a short life is enough. For emergencies (like an abusive user), we use the denylist.

Follow-up question: Where do you keep the Refresh Token? If it is stolen, how do you find out?

Follow-up answer:

  • On the server, we keep it as a hash in the database (like a password). If the database leaks, the tokens themselves do not leak.
  • In the browser, we put it inside a cookie, with the HttpOnly, Secure and SameSite settings. Not in localStorage. The reason is simple:
    1. If the site has an XSS weakness, the attacker’s JavaScript code runs.
    2. This code can read localStorage.
    3. But it cannot read a cookie with HttpOnly.
  • To detect theft, we use Refresh Token Rotation:
    1. Each time a Refresh Token is used, a new Refresh Token is given and the old one is revoked.
    2. If an attacker has stolen the token, sooner or later one of the two sides (the user or the attacker) sends a revoked token.
    3. When the server sees a revoked token, it knows theft has happened. So it revokes all tokens of that session.
    4. The real user only logs in one more time.

Red flag: The candidate thinks that logout, or deleting the token in the browser, also revokes the token on the server.

Report a problem or send a better text

Edit on GitHub

21Two concurrent requests with one Idempotency KeyHard–Idempotency

Question: We built an Idempotency Key for a payment API. This is how it works now: (1) We look in the database to see if this key has a result. (2) If it has one, we return that result. (3) If it does not, we make the payment and save the result with the key. The service runs on several pods. You see this design in code review. What do you think? What happens if two requests with the same key arrive at almost the same moment?

Short answer: The “check first, then do the work, then save” pattern has a race condition. Both requests check before the other one saves anything. The right way is this: first record the key (with a unique constraint), then do the work.

Hint (if the candidate says there is no problem): Because of a bug, the mobile app sometimes sends two requests with the same key at almost the same moment. In these cases, the customer’s account is charged twice. Now, why?

What happens? (in time order)

Time Request 1 Request 2
0 ms Checks the key: not found
2 ms Checks the key: not found
5 ms Starts the payment Starts the payment
800 ms Saves the result Saves the result

Neither request found anything at check time. This is because the result is saved only after 800 ms. So both of them made the payment.

Solution: record the key first

  1. The keys table has a unique constraint on two columns: the user ID and the key.
  2. The first request inserts a row with the status “in progress”. Then it makes the payment.
  3. The second request also tries to insert the same key. The database gives it a unique error. The database guarantees that only one succeeds, even if both arrive in the same millisecond and on two different pods.
  4. The second request returns a 409 (Conflict) response. This means “it is in progress, ask again later”. Or it waits a little and returns the result of the first request.
  5. When the payment ends, the status “done” and the result (status code and response body) are saved.
db.IdempotencyKeys.Add(new IdempotencyKey(userId, key, Status.InProgress, requestHash));
try
{
    await db.SaveChangesAsync(ct);
}
catch (DbUpdateException ex) when (IsUniqueViolation(ex))
{
    return Results.Conflict("Request is already being processed.");
}
// Only one request reaches this point

Two more points:

  • Also save a hash of the request body. If the same key comes with a different amount, return an error (for example, code 422). This is a sign of a bug in the client.
  • Delete keys after some time (for example, 24 hours).

Follow-up question: What happens if the first request crashes in the middle and the key stays “in progress” forever?

Follow-up answer: The user can no longer pay with this key. The solution:

  1. Also save the start time. If a key stays “in progress” longer than a limit (for example, 5 minutes), the work has stopped halfway.
  2. But before repeating the payment, we must find out if the first payment really happened. So we ask the payment gateway with the same ID. This is called reconciliation.
  3. If the payment happened, save the result and return it. If not, do it again.

Red flag: Suggests using the lock statement in C#. This statement works only inside one process. It does not stop two requests on two different pods.

Report a problem or send a better text

Edit on GitHub

22Event order from several serversHard–KafkaConsistency and CAP

Question: Three services on three different servers create the events of an order: “order created”, “payment done”, “shipped”. Each service puts a timestamp on the event using its own server clock. All servers sync with NTP. A reporting service sorts these events by timestamp and then runs the report calculations. What do you think of this design? Is this order always reliable?

Short answer: Server clocks are not exactly the same. This difference is called Clock Skew. When two events happen a few milliseconds apart on two different servers, their timestamps are not reliable. To decide the order, you must use something other than the clock.

Hint (if the candidate says there is no problem): Sometimes in the report output, “payment done” appears before “order created”. In these cases, the report calculations are wrong. Now, why?

A numeric example:

  • The order server clock is 100 ms ahead. The payment server clock is exact.
  • The order is created at the real time “10:00, millisecond 0”. But its timestamp is recorded as “10:00, millisecond 100”.
  • The payment happens 50 ms later, at “10:00, millisecond 50”. It also gets this as its timestamp.
  • Now, if we sort by timestamp, the payment comes before the order.

Even with NTP, clocks differ by a few milliseconds (sometimes more). A server clock may even jump backward during sync.

Options:

  1. A version number from a single source: The order has a version number in its own database (1, 2, 3 and so on). Each event carries the order’s version number. This number is created in only one place, so its order is correct.
  2. Order in Kafka: Send all events of an order to one topic with the order ID as the key. They all go to one partition. The offset number shows the real order of arrival.
  3. An explicit cause and effect link: The “payment” event carries the ID of the event that caused it (the order). This ID is called a causation id. The report knows this event comes after that one, whatever timestamp it has.
  4. A logical clock (Lamport Clock or Vector Clock): It is enough for the candidate to know this option exists.

Rule: A timestamp is good for display and rough analysis. But it is not right for deciding the exact order between servers.

Follow-up question: How do you store time in the database? If a Job must run “at 00:00 in each customer’s local time” for customers in several countries, what should you watch for?

Follow-up answer:

  • Store time in UTC. The DateTimeOffset type is better than DateTime. This is because the offset is kept with the time, so nothing is ambiguous.
  • For “local midnight”, keep each customer’s time zone as a standard IANA ID (for example, the Madrid zone). Then calculate with the TimeZoneInfo class, not with a fixed offset. The reasons:
    1. With daylight saving time, the offset changes.
    2. Sometimes one hour repeats twice.
    3. Sometimes one hour does not exist at all.
  • To make the code testable, use the TimeProvider class instead of reading the current system time directly.

Red flag: Says “we sync all servers with NTP and the problem is solved”.

Report a problem or send a better text

Edit on GitHub

23Hot partition in KafkaHard–Kafka

Question: We have a topic with 12 partitions. A service with 12 pods (in one consumer group) reads it. The message key is the customer ID. One big customer creates about 70% of all messages. What do you think of this design? What happens in Production with this load? If the load grows, is adding more partitions and pods enough?

Short answer: Kafka sends all messages with the same key to one partition. Each partition is also given to only one consumer. So 70% of the work is on one pod. More partitions do not help, because this customer’s key is still one key. We must change the key or spread this customer’s load in a different way.

Hint (if the candidate says there is no problem): In monitoring, we see one pod is several hours behind (consumer lag is high). The other eleven pods are almost idle. The team raised the number of partitions and pods to 24, but nothing changed. Now, why?

Why does this happen? (step by step)

  1. Kafka has a simple formula to choose a partition: it takes the hash of the key and calculates the remainder of dividing it by the number of partitions. So one customer always goes to one partition. This is on purpose, to keep the order of each customer’s messages.
  2. In a consumer group, each partition is given to only one consumer (one pod) at any moment.
  3. So all messages of the big customer, which is 70% of all work, reach only one pod. This problem is called a Hot Partition.
  4. With 24 partitions, the big customer is still one key. So it still has only one partition and one pod.

Options:

  1. A finer key: Maybe order is needed only inside one order, not for the whole customer. In this case, use the order ID as the key. The big customer’s orders spread across all partitions. This is the best and simplest option.
  2. Salting: Use the customer ID plus a number from 0 to N as the key (for example, “customer 42, number 3”). This customer’s load spreads over N partitions. But the overall order of this customer’s messages is lost.
  3. A separate topic for big customers, with its own consumers.
  4. Parallel processing inside the same pod: The pod spreads messages between several internal workers by order ID. This option is harder, because committing the offset gets complex. You can commit only up to a message whose earlier messages are all finished.
  5. Faster processing of each message: For example, writing to the database in batches instead of one by one.

For monitoring, look at the lag of each partition separately, not only the total for the whole group. The total hides this problem.

Follow-up question: Does your solution break the order of this customer’s messages? Does it matter or not?

Follow-up answer:

  • With Salting, yes. The overall order is lost.
  • With the order ID as the key, the order of messages in each order is kept. But the order between different orders of one customer is not kept.
  • A good candidate first asks: “Where is order really needed?” For example, for the customer’s account balance, the overall order may matter. But for shipping, it does not.

Red flag: Their only solution is “more partitions and consumers”.

Report a problem or send a better text

Edit on GitHub

24Data that lives in another serviceDesignWeight ×2–MicroservicesDomain-Driven Design

Question: The “order list” page shows 50 orders. For each order, the customer name and the product name are also needed. Orders are in the Order service, customers in the Customer service, and products in the Catalog service. Each one has its own database. Right now, for each order, the Order service calls the Customer service once and the Catalog service once over HTTP. What do you think of this design? Does it have a problem? If you want to improve it, what options do you have, and which one do you choose?

Short answer: This is the same N+1 problem (question 3), this time with HTTP calls instead of queries. 50 orders times 2 calls is 100 network calls, one after another. If each one takes about 40 ms, the total is 4 seconds. The simplest improvement: instead of 100 calls, make 2 batch calls in parallel.

Hint (if the candidate says there is no problem): Loading this page takes about 4 seconds. Each HTTP call to another service takes about 40 ms.

Options, from simple to complex:

1. Batch calls in parallel (API Composition):

  1. We collect the customer IDs and remove duplicates. We do the same for products.
  2. We get all customers with one request. We also get all products with one request.
  3. We send these two requests at the same time, not one after another.
var customerIds = orders.Select(o => o.CustomerId).Distinct();
var productIds  = orders.Select(o => o.ProductId).Distinct();

var customersTask = customerClient.GetByIdsAsync(customerIds, ct);
var productsTask  = catalogClient.GetByIdsAsync(productIds, ct);
await Task.WhenAll(customersTask, productsTask);

We go from 100 calls one after another to 2 calls at the same time. The time drops from 4 seconds to about 50 ms. A short cache also helps. This is usually the first step.

2. Copy the data at save time (Snapshot): When the order is created, the customer name and the product name and price are saved in the order itself. Then no service is needed to show the list. For invoices and financial documents, this is often the most correct choice. This is because the price at purchase time matters.

3. A local copy with events:

  1. The Customer service publishes a “customer name changed” event on every name change.
  2. The Order service has a small table: customer ID and name.
  3. The Order service updates its table with these events.

Reading is fast and does not depend on the Customer service. But the data is a few seconds late (eventual consistency) and it needs more code.

4. A separate read model (Read Model in CQRS): We have a database or index just for this page (for example, Elasticsearch). This index is filled from the events of all three services. It is good for search and complex filters. But it is the most expensive option.

Which one should we choose?

  • Usually option 1 first.
  • Option 2 for data that must stay “historical”.
  • Option 3 or 4 when the load is high or complex search is needed.
  • If these three pieces of data are almost always needed together, maybe the service boundaries are drawn wrong. A candidate who says this has good depth.

Follow-up question: If a customer changes their name, should their old orders show the new name or the old name? Who makes this decision?

Follow-up answer: This is a business decision, not a technical one. You must ask the Product Owner. Usually, an invoice must keep the name and price at purchase time (option 2). For display in a panel, the new name may be better. The main point is that the candidate should not decide alone.

Red flag: Suggests that the Order service connects directly to the Customer service database, or makes a JOIN between databases.

Report a problem or send a better text

Edit on GitHub

25When compensation fails in a SagaDesignWeight ×2–The Saga pattern

Question: Placing an order in our microservice system is a three-step Saga: (1) the warehouse service reserves the item, (2) the payment service takes the money from the customer’s card, (3) the shipping service books a courier. One day step 3 fails (for example, the address is outside the delivery area). So the Saga must give the money back to the customer (refund) and release the warehouse reservation. But the payment service is also down right now, and the refund fails. What should happen? How do you design the Saga so that no order is lost in an unknown state and no customer’s money is lost?

Short answer: Compensation can also fail. So we must have a plan for it:

  1. The Saga state is saved in the database.
  2. The compensation is retried until it succeeds.
  3. If it still fails, the work goes to manual review.

Best of all is to design the order of the steps so that a refund is needed less often.

First: what is a Saga?

  • In microservices, we have no shared transaction between services.
  • A Saga is several steps, one after another. Each step is a local transaction in its own service.
  • Each step has a “compensating action”. For example, the compensating action of “reserve” is “release the reservation”.
  • If a step fails, the compensating actions of the earlier steps run in reverse order.

When the refund fails, what should happen?

  1. The Saga state is in the database, not only in memory. For example, the order is in the state “waiting for refund”. If the service restarts, it knows where the work stopped.
  2. Retry with growing delays: The refund is tried again after 1 minute, 5 minutes, 30 minutes and so on, even if it takes hours.
  3. The compensation must be idempotent: Each refund has a unique ID. Maybe one attempt succeeded but its response was lost. With this ID, the next attempt does not refund the money twice.
  4. Manual review: If it still has not succeeded after a set limit (for example, 24 hours), the order goes to the state “needs review”. The message goes to a Dead Letter Queue, and the finance team gets an alert.
  5. A time limit (timeout) for each step: Sagas that have stayed in one step for a long time are found and reported.
  6. The real state is shown to the customer: “Order canceled, refund in progress”.
  7. A nightly comparison (reconciliation) between our system and the payment gateway, as the last line of safety.

A better order of steps (the golden point):

  1. In step 2, we do not take the money. We only hold it (Authorize).
  2. After step 3 succeeds, we take the money (Capture).
  3. If shipping fails, only the held money is released. This is much simpler than a refund. It is usually released automatically after a few days too.

General rule: the step that is hardest to undo should come last.

Follow-up question: Do you build the Saga with Orchestration or Choreography? What is the difference?

Follow-up answer:

  • Orchestration: A central service (the orchestrator) is like a project manager. It tells each service what to do and keeps the state. Seeing the state, handling timeouts and compensation are simpler. For multi-step Sagas like this one, it is usually better.
  • Choreography: There is no central manager. Each service reacts to the previous event. For example, the warehouse reserves on “order placed”, and payment takes the money on “reserved”. There is less coupling. But it gets hard to know “where is this order in the process right now?”.
  • In .NET, tools like MassTransit (with State Machine Saga) have this pattern ready.

Red flag: Suggests a distributed transaction (2PC) between services, or has no plan for when the compensation itself fails.

Report a problem or send a better text

Edit on GitHub

26Scaling in Kubernetes: horizontal and verticalMedium to hard–Kubernetes

Question: We have a .NET API in Kubernetes. These are the settings of each pod:

  • The CPU request is 500m (half a core).
  • The CPU limit is 1 core.
  • The Memory request and the Memory limit are both 512Mi.

We also have an HPA. When CPU use reaches 70%, it adds a pod. At peak hours, we see these three things in monitoring:

  1. Response time goes up. But the HPA does not create any new pod, because CPU is about 40%.
  2. Some pods restart with the status OOMKilled.
  3. Sometimes new pods are created, but they stay in the Pending status for a few minutes.

What is the difference between horizontal and vertical scaling? What exactly do request and limit do? How do you explain each of these three events, and what do you change?

Short answer: Horizontal means more pods. Vertical means more resources for each pod. The request is a reservation and the limit is a cap on use. The three events happen for these reasons:

  1. Event 1: CPU is not the bottleneck. So the HPA, which looks at CPU, does nothing.
  2. Event 2: Memory use went over the limit.
  3. Event 3: No node has enough free space for the request.

First: horizontal and vertical

Horizontal (HPA) Vertical (VPA)
What it does Adds more pods Gives each pod more CPU and memory
Condition The app must be stateless (session in memory or a local file causes problems) Usually the pod must restart
Limit Almost no cap (up to cluster capacity) Has a cap: the size of one node

Note: Do not use HPA and VPA together on the same metric (for example, both on CPU). They conflict with each other.

Second: request and limit

  • The request is what Kubernetes reserves for the pod. The pod is placed only on a node that has this amount free.
  • Important point: the CPU percent in the HPA is calculated relative to the request, not relative to the limit.
  • The limit is the cap on use. CPU and memory behave differently at this cap:
    • When CPU reaches the limit: throttling happens. The pod is not killed, it only gets slower.
    • When memory goes over the limit: OOMKilled happens. The pod is killed.

Explaining all three events

Event 1 (slow with CPU at 40%):

  1. When CPU is low but the app is slow, the app is waiting for something. For example, the database, another service, or the thread pool (like question 6).
  2. The HPA does not see this waiting, because it looks only at CPU.
  3. If the bottleneck is the database, more pods only add more load to the database.
  4. Another cause can be throttling. The average CPU is low, but in short moments the pod reaches the limit and is paused.
  5. To see this, check the throttling metric in Prometheus (the cfs throttled periods metric for the container).
  6. Solution: first find the real cause. If load goes up with the number of requests, put the HPA on a better metric: requests per second, latency, or queue length (for example, with KEDA).

Event 2 (OOMKilled):

  1. In .NET, the GC sees the container memory limit. By default, it uses about 75% of it for the heap.
  2. The rest of the memory is for native memory, stacks and other things.
  3. First check for a memory leak (question 35).
  4. If there is no leak and the app really needs more memory, raise the limit based on real use.

Event 3 (Pending):

  1. The Pending status means the scheduler found no node with this request amount free.
  2. One option: the Cluster Autoscaler adds a new node. This takes a few minutes.
  3. Another option: make the request realistic. A very large request wastes capacity. A very small request makes many pods pile up on one node.

Some .NET points and a common setup

  • In .NET, the number of processors (ProcessorCount) comes from the CPU limit. With a limit of 1, the thread pool and the GC think there is only one core.
  • Common setup: the Memory request equals the Memory limit, so behavior is predictable. The CPU request is based on real use.
  • Some teams set the CPU limit high, or do not set it at all, to avoid throttling. This is debated. A good candidate explains its costs and benefits.
  • There are three QoS classes: Guaranteed (when the request equals the limit), Burstable and BestEffort. When a node is short on memory, BestEffort pods are evicted first, then Burstable pods.

Follow-up question: When do you prefer vertical scaling over horizontal? If the traffic peak is every day at 9 AM and the HPA reacts late, what do you do?

Follow-up answer:

  • Choose vertical for things that cannot have several copies. For example, a database, a stateful service, or an old app that keeps state in memory.
  • For a predictable peak: a little before 9, raise the minimum number of pods on a schedule (for example, with the cron scaler in KEDA).
  • Reduce pod startup time: a smaller image, ReadyToRun, and warm-up before readiness turns positive.
  • The reason: the HPA always reacts with some delay by design.

Red flag: Mixes up throttling and OOM. Or thinks the HPA percent is relative to the limit. Or their only answer is “more pods”.

Report a problem or send a better text

Edit on GitHub

27How Kafka partitions relate to podsMedium–KafkaKubernetes

Question: We have a topic with 6 partitions. A consumer service with 4 pods reads it. All pods are in one consumer group. Monitoring shows two pods are busier than the others. How do partitions and pods relate? How is the load split between these 4 pods? What happens if the HPA raises the number of pods to 10? What if only 1 pod is left?

Short answer: The main rule is this:

  1. In a consumer group, each partition is given to only one consumer at any moment.
  2. But one consumer can have several partitions.
  3. So the largest useful number of pods equals the number of partitions.

Three cases

Case What happens
6 partitions and 4 pods Two pods get 2 partitions each, and two pods get 1 partition each. The load is uneven. That is why two pods are busier.
6 partitions and 10 pods Six pods get one partition each, and four pods are idle. Only when a pod dies does an idle one take its place. It does not get faster.
6 partitions and 1 pod That one pod gets all 6 partitions. It works, only slower.

Practical results

  • The maximum number of pods in the HPA (the maxReplicas setting) should not be more than the number of partitions. Extra pods only waste resources.
  • For an even split, choose a partition count that divides evenly by common pod counts. For example, 12 partitions split evenly with 2, 3, 4, 6 or 12 pods.
  • To scale consumers, the right metric is consumer lag, which means the number of unread messages. CPU is not the right metric. With KEDA, you can scale based on lag.
  • You can add partitions later, but you cannot remove them.
  • Adding partitions moves some keys to other partitions. So the order of messages for those keys may get mixed up at that moment.
  • So leave some room for growth from the start.

The idea of rebalance

  1. Every time the number of pods changes (scale, deploy or crash), Kafka splits the partitions between the pods again. This is called a rebalance.
  2. During this time, consuming messages slows down or stops.
  3. To reduce its effect, use the CooperativeSticky strategy. With it, only the needed partitions move, not all of them.
  4. For short restarts, turn on static membership. This means each pod has a fixed ID in the group.

A .NET point: in the Confluent.Kafka library, you cannot use one consumer from several threads at the same time. If we create several consumers in one pod, each one counts as a separate member of the group and gets its own partitions.

Follow-up question: In the middle of processing a message, a rebalance happens and the partition is taken from this pod. What problem comes up, and how do you handle it?

Follow-up answer:

  1. The problem: a message that was processed but whose offset was not yet committed is given to another pod. So it is processed twice.
  2. First solution: make processing idempotent (question 5).
  3. Second solution: Kafka calls a handler before taking the partition (in Confluent.Kafka, with SetPartitionsRevokedHandler). In this handler, finish the current work and commit the offset.

Red flag: Thinks two pods in one group can read one partition together and split the load in half. Or thinks more pods always means more speed.

Report a problem or send a better text

Edit on GitHub

28Using Task or ThreadMedium–async/await and the thread pool

Question: Think about two cases:

  • Case A: An API gets a very large number of requests (for example, 10 thousand concurrent requests). Each request does some I/O work (database, HTTP). Is it better to use Task (that is, async/await) or to create a Thread for each request? Why?
  • Case B: A service has 10 background jobs that are always running. For example, listening to a socket or reading a price stream. Are 10 Tasks better for these, or 10 always-running Threads?

Short answer:

  1. For case A, always async/await. This is because no thread is held while waiting for I/O.
  2. For case B, the answer depends on whether the code inside the loop is async or blocking.
  3. If the code is async: a normal Task.
  4. If the code is blocking: a dedicated thread, not the thread pool.

The difference between Thread and Task, in simple words

  • A Thread is an operating system resource. Each thread has its own stack memory (usually about 1 MB reserved). Creating it has a cost. Switching between threads (context switch) also takes CPU time.
  • A Task is a “job”, not a thread. Tasks run on threads from the thread pool. When a Task is waiting for I/O (await), it holds no thread.

Case A: why async/await?

  1. If we create a thread for each request, 10 thousand requests means 10 thousand threads.
  2. This means about 10 GB of stack space. The CPU also spends most of its time switching between threads.
  3. Most of these threads are only waiting for the database and do nothing.
  4. With async/await, when a request is waiting for the database, the thread is freed and moves another request forward.
  5. So a few dozen threads can serve thousands of concurrent requests.
  6. ASP.NET Core itself uses the thread pool. Our job is only to write the code as async and not block it by reading Result (question 2).

Case B: it depends on whether the code is async or blocking

If the code is async (like the ReceiveAsync methods on a socket, ReadAsync on a stream, or the System.IO.Pipelines library):

  • Ten normal Tasks (for example, 10 BackgroundServices) are the best choice.
  • When no data has arrived, no thread is held. So these 10 Tasks cost almost nothing.

If the code is blocking (a library with only sync methods, a blocking Read method, or an endless loop of heavy calculation):

  • You should not run it as a normal Task on the thread pool. The reason, in a few steps:
    1. These 10 jobs hold 10 threads of the pool forever.
    2. If the pool has, for example, 8 initial threads, nothing is left for API requests.
    3. On the other hand, the thread pool adds each new thread slowly.
    4. The result: the whole app gets slow. This is called thread pool starvation.
  • The right way: create a dedicated thread for each job. There are two ways:
// Way 1: a background Thread
var t = new Thread(Listen) { IsBackground = true };
t.Start();

// Way 2: a separate thread, outside the pool
Task.Factory.StartNew(Listen, TaskCreationOptions.LongRunning);

If the work is CPU-heavy:

  • Having more active threads than cores does not help. Ten heavy threads on 2 cores only create more context switches.
  • A better pattern has three parts: one part that reads the data, an in-memory queue (System.Threading.Channels), and a number of workers equal to the number of cores.

In all cases: A CancellationToken is needed for a safe shutdown (question 11).

Follow-up question: Why is this code a common mistake?

Task.Factory.StartNew(async () => await ListenAsync(ct), TaskCreationOptions.LongRunning);

Follow-up answer:

  1. The LongRunning option creates a dedicated thread. But the code runs on that thread only until the first await.
  2. After the first await, the rest of the code moves to the thread pool. The dedicated thread ends up useless.
  3. So LongRunning makes sense only for blocking code.
  4. Second problem: StartNew with an async lambda returns a nested Task (a Task that has another Task inside it).
  5. If we do not open it with the Unwrap method, await waits only until the first inner await. Later errors are also lost.
  6. For async code, it is enough to call the method directly, or to use the Run method of the Task class. This method unwraps the nested Task by itself.

Red flag: Says “Task is faster than Thread” or “Task always means a new thread”. Or suggests one thread for each request.

Report a problem or send a better text

Edit on GitHub

29Moving to Redis ClusterHard–Redis

Question: We are moving from a single Redis to a Redis Cluster with 3 primary nodes. The current system has these two things:

  1. We have a Lua script that lowers the stock of several products together, atomically (the stock keys of products 1, 2 and 3). It works correctly on a single Redis.
  2. We have one very popular key: the home page settings that every request reads.

After moving to the Cluster, how do these two behave? Will there be a problem? If yes, what is the cause and how do you fix it?

Short answer: In a Cluster, each key lives on one specific node.

  1. Problem 1: the keys of this script are on different nodes. So Redis cannot run it atomically.
  2. Problem 2: all requests for one key go to that same node. The Cluster does not spread the load of one key.

Hint (if the candidate says there is no problem): After the move, the Lua script returns a CROSSSLOT error. The error text says the keys of this request are not in one slot. In monitoring, we also see the CPU of one node has reached 100%, but the other two nodes are almost idle.

How Redis Cluster spreads keys

  1. The key space is split into 16384 parts. Each part is called a hash slot. Each node owns some of the slots.
  2. For each key, Redis takes a hash of the key name (with CRC16) and calculates its remainder when divided by 16384. This number is the key’s slot.
  3. So the stock key of product 1 and the stock key of product 2 usually have different slots and different nodes.
  4. Multi-key commands (like MGET, a MULTI transaction and a Lua script) run only on one node.
  5. If the keys are not in one slot, Redis gives a CROSSSLOT error.

Solution to problem 1: Hash Tag

If part of the key name is inside curly braces, only that part is used to calculate the slot:

stock:{sale42}:p1
stock:{sale42}:p2
stock:{sale42}:p3
  • Now all three keys are in one slot, and the script works again.
  • Be careful: a hash tag gathers all those keys on one node. If you use it too much, it creates a hot node by itself.
  • Another option: design the data so that the atomic operation is on only one key. For example, one Hash with several fields, one field for each product.

Problem 2: Hot Key

One key is always on one node. If all requests read the same key, they all go to that node. Adding nodes does not help either. We have three options:

  1. A short local cache in each pod (for example, 5 seconds): most reads never reach Redis. For data that changes rarely, this is the simplest and best option.
  2. Several copies of the key: for example, we create 8 settings keys numbered 1 to 8 with the same value. Each request reads one of them at random. So the load spreads between nodes.
  3. Reading from a replica: in StackExchange.Redis, with the PreferReplica flag. We must accept a small delay in the data.

To find a hot key: the redis-cli tool with the hotkeys option (if the eviction policy is LFU), or comparing the metrics of each node.

Follow-up question: What is a “Big Key”? Why is deleting a Hash with one million fields using the DEL command dangerous?

Follow-up answer:

  1. In Redis, commands run on one main thread, one after another.
  2. Work on a very large key (like DEL, HGETALL or SMEMBERS) may take a few seconds.
  3. During this time, all clients wait.
  4. Solution: keep keys small.
  5. To delete, use UNLINK instead of DEL. UNLINK deletes in the background.
  6. To read, use HSCAN and SSCAN instead of a full read, so the data is read piece by piece.

Red flag: Thinks the Cluster spreads the load of each key between nodes by itself.

Report a problem or send a better text

Edit on GitHub

30Deadlock in the databaseMedium to hard–Transactions and isolation levels

Question: We have a money transfer service. Each transfer does two things in one transaction:

  1. It lowers the balance of the source account.
  2. It raises the balance of the destination account.

At peak hours, many transfers between accounts run at the same time. What do you think of this design? What happens under this concurrent load? Does it have a problem?

Short answer: Each transaction has locked one of the two accounts and is waiting for the other one. Neither can move forward. This is called a deadlock. The main solution: all transactions take the locks in a fixed order.

Hint (if the candidate says there is no problem): At peak hours, the API returns this error for some transfers: “Transaction was deadlocked on lock resources with another process and has been chosen as the deadlock victim”. The investigation shows this error usually happens when a transfer from account A to B and a transfer from B to A run at the same time.

What happens? (in time order)

Time Transaction 1 (A to B) Transaction 2 (B to A)
1 Updates account A and takes the lock on A Updates account B and takes the lock on B
2 Wants to update B, so it waits for the lock on B Wants to update A, so it waits for the lock on A
3 Both wait for each other forever. The database detects this and picks one as the victim

Solution

  1. A fixed lock order: always update the account with the smaller Id first, whether it is the source or the destination. Now both transactions go to A first. The second one only waits until the first one finishes. No deadlock happens.
  2. Short transactions: slow work (an HTTP call, heavy calculation) should not be inside the transaction. The shorter a lock is held, the lower the chance of a clash.
  3. The right index: without an index, the database reads and locks more rows to find a row.
  4. Retry: deadlocks never go fully to zero. For a deadlock error, retry the whole transaction a few times. The number of this error is 1205 in SQL Server, and the code is 40P01 in PostgreSQL. In EF Core, the execution strategy does this (for example, with the EnableRetryOnFailure option).
  5. Finding the exact cause: in SQL Server, use the deadlock graph (with Extended Events and the default system_health session). In PostgreSQL, the database log records it. This report shows which two queries and which locks were involved.

Follow-up question: Do you know Isolation Levels? What problem does Read Committed Snapshot (RCSI) solve, and what problem still remains?

Follow-up answer:

  • In RCSI, reads use the previous committed version of the data (row versioning). So a read does not wait for a write lock.
  • The result: reads and writes do not block each other. Most deadlocks between reads and writes go away.
  • But two writes on the same row still block each other. So the deadlock in this question is not solved by RCSI.
  • The problem that remains: Write Skew. Two transactions read old data. Each one makes a decision based on it and writes.
  • Example: the balance is 100 and two withdrawals of 80 toman arrive at the same time. Each one sees a balance of 100 and accepts.
  • First solution: a conditional, atomic update. That means lowering the balance and checking that it is enough, in one statement:
UPDATE Accounts
SET Balance = Balance - @x
WHERE Id = @id AND Balance >= @x;
  • If no row was updated, the balance was not enough.
  • Second solution: an explicit lock when reading. In SQL Server with UPDLOCK, and in PostgreSQL with FOR UPDATE.

Red flag: Their solution is “we add NOLOCK”. With NOLOCK, the database returns half-done, uncommitted data (dirty read). It may even return duplicate or missing rows. For money transfers, this is dangerous.

Report a problem or send a better text

Edit on GitHub

31When the user cancels a requestMedium–async/await and the thread poolMiddleware and the pipeline

Question: We have a heavy report. Its query takes about 2 minutes. Users usually do not wait. They close the page or press Refresh several times. You see this endpoint in code review:

app.MapGet("/reports/sales", async (AppDbContext db) =>
    await db.Sales.Where(...).GroupBy(...).ToListAsync());

What do you think? Does it have a problem? When the user closes the page, what happens to the query?

Short answer: When the user closes the page, ASP.NET Core notices. But it only sends a “signal”. This signal is called a CancellationToken. If the code has not given this token to the query, nobody tells the database “stop”. So the query runs to the end. Solution: pass the token through all layers.

Hint (if the candidate says there is no problem): The database administrator (DBA) sees in the monitoring tool that at peak hours, dozens of queries for this same report are running at the same time. Most users of these queries have left. The database has become slow for the other users.

What happens? (step by step)

  1. The user asks for the report. The 2-minute query starts.
  2. After 10 seconds, the user presses Refresh. The previous connection is closed. A new request starts.
  3. The ASP.NET Core framework sees that the first connection was closed. So it cancels the token of that request (the RequestAborted token).
  4. But the query code did not get any token. So the first query runs to the end. Then its result is thrown away.
  5. Now two heavy queries are running for one user. With a few impatient users, we have dozens of useless queries.

Solution:

  1. Pass the cancellation token (CancellationToken) from start to end. In a controller or minimal API, it is enough to take a parameter of this type. ASP.NET Core fills it by itself:
app.MapGet("/reports/sales", async (AppDbContext db, CancellationToken ct) =>
    await db.Sales.Where(...).GroupBy(...).ToListAsync(ct));
  1. When the token is canceled, EF Core and ADO.NET send a cancel command to the database. So the query also stops inside the database. Give the same token to HttpClient and the other async methods too.
  2. Other protections:
    • Set a timeout for the query (the CommandTimeout setting). This way, no query runs forever.
    • If this same report is already running for this same user, do not run it again.
    • For a very heavy report, make it a background job or cache the result (question 18).
  3. An OperationCanceledException caused by closing the page is normal. It should not be logged as an error. It should not create an alert.

Follow-up question: Where should you ignore request cancellation and finish the work to the end?

Follow-up answer: When we are in the middle of a multi-step job and leaving it half-done is bad. Example: money was taken by the payment gateway, and only saving the result in the database is left. The user has left. But if we cancel here, the money is taken and no order is saved. For these steps, we pass an empty token (CancellationToken.None). Or we hand the work to a queue or an Outbox. A good candidate separates these two: “the user no longer wants the answer” and “the work must be completed”.

Red flag: Never passes a CancellationToken. Or thinks closing the browser stops the database query by itself.

Report a problem or send a better text

Edit on GitHub

32Changing message structure (Schema Evolution)DesignWeight ×2–Schema evolution

Question: Five different services read the OrderCreated event from Kafka. Each service has its own team and its own deploy time. Right now, the message looks like this:

{ "orderId": "o-1", "amount": 250000 }

Because of moving to multiple currencies, the Order team wants to change the amount field to this shape:

{ "orderId": "o-1", "amount": { "value": 250000, "currency": "IRR" } }

If they deploy this change tomorrow, what happens? How do you make this change so that no service breaks?

Short answer: Consumers that still expect a number do not understand the new message and get stuck. A message is a contract. Changing a field’s type, removing a field or renaming a field is a breaking change. The right way: add the new field next to the old field. Remove the old field last of all.

Why does it break? (step by step)

  1. The Notification service still has the old code. This service treats the amount field as a decimal number.
  2. The new message arrives. Now amount is an object. So reading the message (deserialize) fails with a JsonException.
  3. The consumer tries the message again (retry) and fails again. In a partition, order matters. So the next messages also get stuck behind this message. Or the message goes to the Dead Letter, and the SMS never reaches the customer.
  4. The reverse problem also exists. If a service moves to the new code earlier, it does not understand the old messages. These messages are still in the topic.
  5. Making five teams deploy at the same moment is practically impossible.

The right way (like Expand and Contract in question 17):

  1. A new field with a new name is added. The old field is still filled:
{ "orderId": "o-1", "amount": 250000, "money": { "value": 250000, "currency": "IRR" } }
  1. The consumers move to reading the money field one by one, whenever they want.
  2. When all have moved and the old messages are no longer needed, the amount field is removed.

Simple rules:

  • Adding an optional field is safe.
  • Removing, renaming or changing the type of a field is a breaking change. Either do it with the method above, or create a new version of the event (for example, OrderCreated version 2) and publish both versions for some time.
  • On the consumer side, use the Tolerant Reader pattern. This means ignore unknown fields and read only the fields you need.
  • Use a Schema Registry tool with Avro or Protobuf. With this tool, an incompatible schema is rejected at registration time, not in Production.

Follow-up question: A new service wants to read all events of the past year from the start (replay). What problem comes up?

Follow-up answer: Messages from one year have several different schema versions. So we have two options:

  1. The new service understands all versions.
  2. Or a conversion layer (upcaster) converts each old version to the new version.

That is why each message must carry its version number (in a header or with a schema id). Another point: the data retention time in Kafka must be long enough. If the data was not kept for one year, a replay is not possible.

Red flag: Says “we tell all teams and everyone deploys together”.

Report a problem or send a better text

Edit on GitHub

33Deadlock in Orleans GrainsHard–Microsoft Orleans

This question is for a candidate who has worked with Orleans or another Actor Model. If they have not, only ask about the idea of a “single-threaded actor” with this example.

Question: In Orleans, we have two grains: the account grain (AccountGrain) and the risk grain (RiskGrain). When the account grain processes a withdrawal, it calls the risk grain to check the risk. For this check, the risk grain needs the current balance. So it calls the GetBalance method on the account grain. What do you think of this design? When a withdrawal comes in, what happens step by step? Does it have a problem?

Short answer: Each grain is single-threaded by default. This means it does not start the next request until its current request is finished. Here, the account grain waits for the risk grain, and the risk grain waits for the account grain. Neither moves forward. The best solution: remove this cycle in the design.

Hint (if the candidate says there is no problem): No withdrawal ever finishes. After 30 seconds, all withdrawals fail with a TimeoutException.

What happens? (step by step)

  1. First, the account grain starts the “withdraw” request.
  2. In the middle of the work, it waits for the answer of the risk check (with await). Important point: for Orleans, the “withdraw” request is not finished yet. So no other request is given to the account grain. This is called non-reentrant.
  3. Then the risk grain sends a GetBalance request to the account grain. This request waits in the queue, behind the “withdraw” request.
  4. Now we have a cycle:
    • “Withdraw” waits for risk.
    • Risk waits for GetBalance.
    • And GetBalance waits for “withdraw” to finish.
  5. This is a deadlock. After the default timeout (30 seconds), the request fails.

Why is Orleans single-threaded? Because of this, we do not need a lock inside a grain. The state is never broken by two concurrent requests. This is the main benefit of the Actor Model.

Options, from better to worse:

  1. Remove the cycle in the design: In this option, the account grain sends the current balance with the request to the risk grain. A call back is no longer needed:
var ok = await riskGrain.Check(amount, currentBalance);
  1. Allow re-entry only for this call chain: In newer versions of Orleans, the AllowCallChainReentrancy method does this. A request that comes back from the same chain is allowed to run.
  2. Mark read-only methods: For example, mark the GetBalance method with the ReadOnly or AlwaysInterleave attributes. This way, it also runs in the middle of other work.
  3. Make the whole grain Reentrant: This is the simplest option, but also the most dangerous. The single-thread safety is lost. Between two awaits, another request can change the state. Example: two withdrawals both see a balance of 100 and both are accepted.

General rule: reduce cycles between grains. Do work that needs no answer with one-way messages (the attribute named OneWay) or with streams.

Follow-up question: We have a global grain, for example a counter of all orders today. All orders call it. Adding silos does not make it faster. Why, and what do you do?

Follow-up answer: Each grain is alive on only one silo and works single-threaded. So all requests are processed one after another. More silos do not help a single grain. Options:

  • Split the counter into several grains. For example, 16 partial counters (by the hash of the order ID) and one grain that adds them up every few seconds.
  • Collect changes in each silo and send them in batches, instead of one call for each order.
  • For work without state, use StatelessWorker. This kind of grain has several copies at the same time.

Red flag: Right away suggests making the grain Reentrant and does not know what protection is lost by doing this.

Report a problem or send a better text

Edit on GitHub

34Following one request across several services (Observability)Medium–Distributed tracing

Question: An order goes along this path: the API, then a Kafka message, then a Worker, then an HTTP call to the payment service. A customer calls and says “my order has been waiting for payment for 2 hours”. The logs are in 4 separate services. Each service has several copies (pods). Each one writes thousands of log lines per minute. Right now, finding where this order got stuck takes several hours. How do you design the system so that you can find the path of this one order in a few minutes?

Short answer: With Distributed Tracing. Each request gets an ID called a Trace Id. This ID travels with the request through all services, even through Kafka. With this one ID, we see the whole path and the time of each part on one page. Next to it, we collect structured logs in one central place.

Why is it hard now?

  • The logs are scattered. No shared ID links them together.
  • We must guess which pod processed this order. Then we must match the logs by their clock times.

The right design:

  1. Distributed tracing with OpenTelemetry: Each request has a Trace Id. Each part of the work (for example, a query or an HTTP call) is a Span. In .NET, ASP.NET Core and HttpClient themselves send and read the standard traceparent header. This is done with the Activity class.
  2. Carrying the Trace Id through Kafka (important point): Kafka does not do this automatically. So two things are needed:
    • When producing a message, we put traceparent in the message header.
    • When consuming a message, we create a new Activity from that header. Or we use a ready-made instrumentation library.
    • If we do not do this, the trace breaks at Kafka.
  3. Structured Logging: A log must be stored as fields, not only as text:
logger.LogInformation("Payment requested for {OrderId} amount {Amount}", order.Id, order.Amount);

With this method, the order ID and the Trace Id are searchable in the central tool (Elasticsearch, Loki or Seq).

  1. A business ID in the trace: Also put the order ID on the spans. Support does not know the Trace Id, but they know the order number. So from the order number we reach the trace, and from the trace we reach all the logs.
  2. An alert before the customer complains: Create a metric that shows the number of stuck orders in each status. When an order stays in one status longer than a limit (for example, 10 minutes), send an alert.

Security note: logs must not contain sensitive data, like passwords, card numbers or tokens.

Follow-up question: Storing a trace for every request is very expensive, because the data volume is large. What do you do?

Follow-up answer: We use Sampling. There are two methods:

  • Head-based: We decide at the start of the request. For example, only 10% of requests are stored. It is simple. But the trace of a failed request that we need may be in the dropped 90%.
  • Tail-based: We decide after the trace finishes (for example, in the OpenTelemetry Collector). All failed and slow traces are kept. Only a small percent of normal traces is kept.

Red flag: Their solution is this: “we search the logs of all services by order number”. And they have not thought about carrying the Trace Id through Kafka.

Report a problem or send a better text

Edit on GitHub

35Memory leak in .NETMedium to hard–Memory and the Garbage CollectorBackground services

Question: On the monitoring dashboard, we see the memory of a service go up slowly and steadily over several days. In the end, the pod restarts with the status OOMKilled. After the restart, the same cycle repeats. A colleague says: “.NET has a GC, so a memory leak is not possible. Let’s just raise the memory limit.” What do you think? How do you find the cause, step by step?

Short answer: The GC only cleans up objects that nothing points to anymore. Sometimes a forgotten reference still points to an object, for example from a static object. That object is never cleaned up. So a memory leak in .NET is fully possible. Raising the limit only delays the OOM.

A real example of a leak:

// PriceFeed is registered as Singleton
public class OrderHandler // Scoped: one instance per request
{
    public OrderHandler(PriceFeed feed)
    {
        feed.PriceChanged += OnPriceChanged; // never unsubscribed
    }
    private void OnPriceChanged(object? s, PriceEventArgs e) { /* ... */ }
}

Why is this a leak? (step by step)

  1. Each event keeps a reference to all its subscribers. So the PriceFeed service points to every OrderHandler.
  2. The PriceFeed service is a Singleton. This means it lives until the end of the app’s life.
  3. So every OrderHandler created in each request also stays alive forever.
  4. After millions of requests, we have millions of handlers in memory.

A second real example: one DbContext forever in a BackgroundService

// Wrong: one scope and one DbContext for the whole life of the service
protected override async Task ExecuteAsync(CancellationToken ct)
{
    using var scope = _scopeFactory.CreateScope();
    var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
    while (!ct.IsCancellationRequested)
    {
        var items = await db.Outbox.Where(x => !x.Sent).Take(100).ToListAsync(ct);
        foreach (var item in items) item.Sent = true;
        await db.SaveChangesAsync(ct);
        await Task.Delay(1000, ct);
    }
}

Why is this a leak? (step by step)

  1. A BackgroundService starts once and lives until the end of the app’s life. So that scope and that DbContext also stay alive until the end.
  2. Each DbContext has a Change Tracker. Every entity read or saved with it stays in the Change Tracker. It does not leave even after SaveChanges.
  3. The loop reads 100 new rows every second. So after one day, we have millions of entities in the Change Tracker.
  4. The GC cannot clean them up, because the DbContext still points to all of them.
  5. Memory goes up slowly until the pod restarts with OOMKilled.
  6. Speed also drops. Before saving, the SaveChanges method runs DetectChanges. This means it checks all entities in the Change Tracker one by one. The more there are, the slower each loop round gets.
// Right: a new scope (and a new DbContext) for each batch
protected override async Task ExecuteAsync(CancellationToken ct)
{
    while (!ct.IsCancellationRequested)
    {
        using (var scope = _scopeFactory.CreateScope())
        {
            var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
            var items = await db.Outbox.Where(x => !x.Sent).Take(100).ToListAsync(ct);
            foreach (var item in items) item.Sent = true;
            await db.SaveChangesAsync(ct);
        } // scope and DbContext are disposed here
        await Task.Delay(1000, ct);
    }
}
  • With this, each loop round has a fresh, empty DbContext. At the end of the round, the DbContext is disposed and the GC cleans up all the entities.
  • For queries that only read and change nothing, use the AsNoTracking method. Then the entity never enters the Change Tracker.
  • If for some reason the same DbContext must stay, call the ChangeTracker.Clear method after each batch. But the first option (a new scope) is cleaner.
  • Instead of a scope, you can also use IDbContextFactory and create a fresh DbContext in each round.

Other common causes:

  • A DbContext that lives forever (for example, in a BackgroundService or inside a Singleton). All entities pile up in its Change Tracker.

  • A static cache or Dictionary that only gets new items and is never cleaned. Or an IMemoryCache with no expiration time and no size limit (SizeLimit).

  • Timers that are not disposed.

  • A disposable Transient service taken from the root container, not from a scope. In this case, the container keeps it until the end of the app’s life, so it can dispose it at the end.

  • Unmanaged resources, like streams and connections, that are not disposed.

How to find the cause (step by step):

  1. With the dotnet-counters tool, see which one goes up: the GC Heap memory (.NET objects) or only the total process memory. If the GC Heap is steady but total memory goes up, we probably have a native leak.
  2. Take two memory snapshots some time apart (for example, one hour). Use the dotnet-gcdump or dotnet-dump tool for this.
  3. Compare the two snapshots (in Visual Studio, PerfView or dotnet-dump). The question is: which object type keeps growing in count? For example, you see 2 million OrderHandlers in memory.
  4. For one of those objects, find the GC Root (with the gcroot command). This command shows the chain of references. That means it tells you what keeps the object alive. Here, the answer is the event inside PriceFeed.
  5. Fix the code and measure again. In this example, unsubscribe from the event in the Dispose method. Or change the design so that events on a Singleton are not used.

Follow-up question: Suppose we have no leak. But after a load peak, the service memory stays high and does not come down. Is this normal? How do you know if it is a leak or not?

Follow-up answer: Yes, it may be normal. The reason:

  • The GC does not always give freed memory back to the operating system right away. It keeps it for later use.
  • Server GC mode also uses more memory by default.

The right measure: look at memory after each full GC (Gen2). If it returns to a steady level each time, there is no leak. If this level goes higher each time, we have a leak. If memory use matters more than speed, you can tune the GC. For example, with the GCConserveMemory setting, or with Workstation GC in small pods.

Red flag: Says “in .NET we have no memory leaks because we have a GC”. Or their only solution is “we raise the memory limit”.

Report a problem or send a better text

Edit on GitHub

36Aggregates and keeping domain rulesMedium to hard–Domain-Driven Design

Question: In the order system, we have a business rule: the total amount of each order must not be more than the customer’s credit limit. The Order class checks this rule in the AddLine method. Now a new endpoint was written to “change the quantity of one order line”:

public async Task ChangeQuantity(int lineId, int newQty)
{
    var line = await _db.OrderLines.FindAsync(lineId);
    line.Quantity = newQty;
    await _db.SaveChangesAsync();
}

You see this code in code review. The tests are green too. What do you think? Does it have a problem? If you wrote it yourself, how would you write it?

Short answer: This code goes around the Aggregate root. The credit limit rule is in Order, but the code changes OrderLine directly. So the rule is never checked. In DDD, only the Aggregate Root is changed from outside. The root keeps all the rules inside its boundary. The whole Aggregate is loaded together and saved together.

Hint (if the candidate says there is no problem): A few weeks later, the finance team reports that some orders have an amount higher than the customer’s credit limit. There is no error in the log of the place-order endpoint. All these orders were changed from the “edit quantity” page after they were placed. Now, why?

Why is this code dangerous? (step by step)

  1. The credit limit rule is about the whole order, not one line. To check it, we must see all lines.
  2. The OrderLine class does not know about the other lines. So it cannot check this rule.
  3. The code takes one line directly from the lines DbSet and changes its quantity. The AddLine method is not called.
  4. So the rule is bypassed. Any new developer can also repeat the same thing somewhere else.
  5. The tests are green, because the tests only tested the AddLine path.

Solution: all changes go through the root

public class Order
{
    private readonly List<OrderLine> _lines = new();
    public IReadOnlyList<OrderLine> Lines => _lines;

    public void ChangeQuantity(int lineId, int qty, Money creditLimit)
    {
        var line = _lines.Single(l => l.Id == lineId);
        var newTotal = Total() - line.Total() + line.Price * qty;
        if (newTotal > creditLimit)
            throw new DomainException("Credit limit exceeded");
        line.SetQuantity(qty); // internal setter
    }
}
var order = await _db.Orders.Include(o => o.Lines)
    .SingleAsync(o => o.Id == orderId);
order.ChangeQuantity(lineId, newQty, customer.CreditLimit);
await _db.SaveChangesAsync();

Some important rules for an Aggregate:

  • Only the root is reachable from outside. For OrderLine, we make no separate repository and no public DbSet. The list of lines is also exposed as read-only.
  • The consistency boundary is the transaction boundary. Everything inside the Aggregate must stay correct together in one transaction. One transaction changes only one Aggregate.
  • Keep it small. A large Aggregate means a heavy load and many clashes between users (locks or concurrency errors). Put inside it only the things that a real rule ties together.
  • Point to other Aggregates by Id. The order keeps only the customer ID, not the customer object itself. This way, the boundaries do not get mixed.
  • Think about concurrency too. If two users edit one order at the same time, with a version column (RowVersion) on the root, EF Core rejects the second change.

Follow-up question: When an order is confirmed, the warehouse stock must also go down. Order and Inventory are two separate Aggregates. Do you change both in one transaction or not?

Follow-up answer:

  • The basic rule: one transaction, one Aggregate. So we usually use Eventual Consistency.
  • When confirmed, the order records a Domain Event named “order confirmed”.
  • This event is saved with the Outbox pattern in the same transaction as the order. So it is not lost.
  • A separate handler takes the event and lowers the stock. If the stock is not enough, it sends a compensating event, and the order is canceled or put on hold.
  • When is a shared transaction acceptable? When both are in one service and one database, the load is low, and the business does not accept even one moment of inconsistency. But it must be a conscious choice. It adds more clashes and locks.
  • A good question for the business: “If the stock goes down a few seconds later, what is the harm?” Often the answer is “none”.

Red flag: Makes a repository for each table and says “we check the rules in the service layer”. Or makes a very large Aggregate that holds the customer, all orders and the warehouse together.

Report a problem or send a better text

Edit on GitHub

37Anemic or Rich domain modelMedium–Domain-Driven Design

Question: In the order project, the Order class and the OrderService are written like this:

public class Order
{
    public int Id { get; set; }
    public string Status { get; set; }
    public decimal Total { get; set; }
    public DateTime? ConfirmedAt { get; set; }
}

public class OrderService
{
    public async Task Confirm(int id)
    {
        var order = await _db.Orders.FindAsync(id);
        if (order.Status != "Draft") throw new Exception("Invalid");
        if (order.Total <= 0) throw new Exception("Empty order");
        order.Status = "Confirmed";
        order.ConfirmedAt = DateTime.UtcNow;
        await _db.SaveChangesAsync();
    }
    // Cancel, Ship, Refund ... the same style, 900 lines
}

This structure repeats across the whole project. What do you think of this design? If it were your project, would you change anything?

Short answer: This is an Anemic Domain Model. The Order class has only data, and all the rules are in the service. Because all setters are public, any code can change the state without checking the rules. In a rich model, the rules are inside the entity itself. For example, the Confirm and Cancel methods. The setters are private. Important values are Value Objects too, like Money. But for a simple CRUD, an anemic model is fully acceptable.

Hint (if the candidate says there is no problem): In Production, we have some orders with the status “confirmed” but no confirmation date. A nightly job also set some canceled orders back to “shipped”. In one month, three bugs of this same kind were reported. Each time in a new place in the code. Now, why?

Why does this design cause problems in a complex domain? (step by step)

  1. The rule “only a draft order can be confirmed” is only in one service method.
  2. All setters are public. So a job, a handler or test code can change the state directly.
  3. Anyone who does this has bypassed the rule. For example, they change the state but forget the confirmation date.
  4. The rules are repeated in several services and slowly become different from each other.
  5. The status is a text string. A typo like “confirmed” with a lowercase letter still compiles.
  6. The service grows to 900 lines. To understand order behavior, you must read all of it.

Solution: move the behavior into the entity

public class Order
{
    public int Id { get; private set; }
    public OrderStatus Status { get; private set; } = OrderStatus.Draft;
    public Money Total { get; private set; }
    public DateTime? ConfirmedAt { get; private set; }

    public void Confirm(DateTime now)
    {
        if (Status != OrderStatus.Draft)
            throw new DomainException("Only draft orders can be confirmed");
        if (Total.Amount <= 0)
            throw new DomainException("Order is empty");
        Status = OrderStatus.Confirmed;
        ConfirmedAt = now;
    }
}
public record Money(decimal Amount, string Currency)
{
    public static Money operator +(Money a, Money b) =>
        a.Currency == b.Currency ? a with { Amount = a.Amount + b.Amount }
        : throw new DomainException("Currency mismatch");
}
  • Now the service only coordinates: it loads the order, calls the Confirm method and saves.
  • The rule is in only one place. No code can bypass it.
  • Testing the rule is simple. You create an Order object and call Confirm. No database and no mock are needed.
  • With EF Core, private setters and private fields are no problem. For a Value Object, we use an Owned Type or a Complex Property.

When is an anemic model good?

  • When the app is only CRUD and has no real rules. For example, an admin panel for a cities table.
  • When the class is only for moving data, like a DTO or a read model.
  • Here, building a rich model is only extra complexity (YAGNI).

Follow-up question: What is the difference between a Value Object and an Entity? Which one do you choose for a customer address?

Follow-up answer:

  • An Entity has an identity. Two orders with the same values are two separate orders if their IDs differ. It also changes over time but stays the same order.
  • A Value Object has no identity. It is known by its value. Two amounts of “100 toman” are fully equal.
  • A Value Object is immutable. To change it, we create a new object.
  • A Value Object checks itself when it is created. For example, an invalid Email is never created. So the rest of the code does not need to check it again.
  • A customer address is usually a Value Object. If the address changes, a new address replaces it. But if the business manages addresses separately (for example, an address book with IDs), then it is an Entity.
  • In C#, the record type is a very good fit for a Value Object. This is because it builds value-based equality by itself.

Red flag: Says “an entity must only hold data and logic always belongs in the service”. Or the opposite: insists on building a rich model and Value Objects even for a simple CRUD.

Report a problem or send a better text

Edit on GitHub

38Bounded Context and shared languageDesign–Domain-Driven Design

Question: In the company, we have three teams: sales, support and finance. All three use one shared Customer class and one shared customers table. This class is in a Shared project, and all services reference it:

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string SalesStage { get; set; }      // Sales
    public decimal? DiscountPercent { get; set; } // Sales
    public int OpenTickets { get; set; }        // Support
    public string SlaLevel { get; set; }        // Support
    public string TaxCode { get; set; }         // Billing
    public decimal CreditLimit { get; set; }    // Billing
    // ... 60 more fields
}

Each team adds fields for its own needs. What do you think of this design? What happens as the company and the teams grow?

Short answer: This model wants everything for everyone. The word “customer” has a different meaning in each team. So one shared class ties all teams together. The DDD solution is to split the system into Bounded Contexts. Each context has its own model of the customer, with its own language (Ubiquitous Language). Contexts connect to each other only through the customer ID and through events. A Context Map shows how they relate.

Hint (if the candidate says there is no problem): The finance team changed the type of one field. The support service stopped working after the deploy. Now, for every small change, three teams must hold a meeting and deploy together. The phrase “active customer” in sales means “bought something in the last 3 months”, but in finance it means “has an open debt”. The reports of the two teams do not match. Now, why?

Why does this design slowly cause problems? (step by step)

  1. Each team needs only a small part of the fields. But all of them depend on the whole class.
  2. When one team changes a field, the code of the other teams also breaks. So teams cannot deploy separately.
  3. One word has a different meaning in each team. But in the code we have only one class and one field. So the meanings get mixed and cause logic bugs.
  4. The class keeps getting bigger. Nobody dares to remove a field, because nobody knows who uses it.
  5. A shared table means shared locks and shared slowness. One team’s heavy query slows down another team.

Solution: each context has its own model

// Sales context
public class Customer { public Guid Id; public string Name; public SalesStage Stage; }

// Support context
public class Requester { public Guid CustomerId; public SlaLevel Sla; }

// Billing context
public class Account { public Guid CustomerId; public TaxCode TaxCode; public Money CreditLimit; }
  • Each context has its own model and database (or at least its own schema). Only the fields it needs. Even the class name can differ. In support it is called a “requester”, and in finance an “account”.
  • Connection by ID. All contexts share one customer ID. But they do not read each other’s data directly.
  • Connection by events. When sales creates a new customer, it publishes a “customer registered” event (for example, with Kafka). Finance and support receive it and build their own models.
  • Anti-Corruption Layer. When a context must connect to an old system or another model, we put a translation layer in between. This layer converts the outside model into our own model. So a change in the outside model changes only this layer, not all our code.
  • Context Map. We write down the relationship between every two contexts. Who is the supplier and who is the consumer? What contract is between them? For example, the event contract has versions and does not break without coordination.
  • Make the change step by step. Everything does not need to change at once. First split one context (for example, finance), fill it with events, and then the others.

Also state the cost of this solution:

  • Data is duplicated, and we have Eventual Consistency. It takes a few seconds for a new customer to show up in finance.
  • Company-wide reports need a separate model, for example a data warehouse.
  • For a small team with a simple product, this split may be too much.

Follow-up question: How do you find the boundaries of contexts?

Follow-up answer:

  • From differences in language. When one word has two meanings in two teams, or there are two words for one thing, you have probably reached a boundary.
  • With Event Storming. All business and technical people come together and place the domain events on a wall. For example, “order placed” and “invoice issued”. Where events form groups and the language changes, there is a context boundary.
  • From the team structure. By Conway’s law, a system ends up like the communication structure of the teams. It is better for each context to belong to one team.
  • From rules and data. Things that change together under one rule stay in one context.
  • From the speed of change. Separate the part that changes every week from the stable part.

Red flag: Says “one shared model is better, because there is no duplication (DRY)”. Or the opposite: right away suggests one microservice for each table, without thinking about the business language and boundaries.

Report a problem or send a better text

Edit on GitHub

39The Mediator pattern and the MediatR libraryMedium to hard–CQRS and MediatorDesign patterns

Question: In an ASP.NET Core API, the constructor of the orders controller looks like this:

public OrdersController(
    IOrderService orders, ICustomerService customers,
    IInventoryService inventory, IPaymentService payments,
    IDiscountService discounts, INotificationService notifications,
    IAuditService audit, IMapper mapper, ILogger<OrdersController> logger)

A teammate suggests moving all controllers to MediatR. This means each action is a Command or a Query with its own Handler. The controller takes only an IMediator. What do you think of this suggestion? What does the Mediator pattern give us, and what problem does it solve? When is it worth it, and when is it not?

Short answer: The Mediator pattern separates the sender of a request from the one that handles it. The controller only sends a message and does not know who runs it. Each use case has a small Handler. Shared work (validation, logging, transactions) is written in one place in the pipeline. But it also has a cost: an extra layer, many classes, and code that is harder to find. More important, 9 dependencies in a controller are a sign of a design problem. MediatR does not solve this problem, it only hides it.

Hint (if the candidate says there is no problem): We did the same thing in another project. After a few months, new developers said it was hard to find the code that runs. With “go to definition” they only reached the Send method. Some Handlers also called other Handlers with Send, and the chain had become long. Now, why?

Where is the main problem? (step by step)

  1. A controller with 9 dependencies means this class knows about many jobs. This goes against the single responsibility principle (SRP).
  2. Each action needs only two or three services. But to build the controller, all 9 services are created.
  3. Testing the controller is also hard. For each test, you must mock 9 dependencies.
  4. With MediatR, the controller takes only an IMediator. Each Handler has only its own dependencies. So the classes become small.
  5. But if the business logic is not split well, the same mess moves into the Handlers. So MediatR does not replace good design.

Benefits of Mediator:

  • Sender separated from receiver: The controller only sends the message. The controller becomes thin.
  • One Handler for each use case: It is similar to CQRS. Classes are small, have one clear job, and are separate from each other.
  • Pipeline behaviors: Shared work like validation, logging, timing and transactions is written once and runs for all requests.
  • Easier testing: You test each Handler separately, with few dependencies.

Costs and risks:

  • An extra layer (indirection): With “go to definition” in the IDE, you do not reach the Handler. You must search for the Handler class.
  • Many classes: For each simple job, you need a Command, a Handler and sometimes a Validator.
  • Hiding the problem: Dependencies are hidden behind IMediator. You can no longer see from the constructor what the class depends on.
  • Send chains: If Handlers call each other with Send, the code flow gets lost.
  • Not needed for simple CRUD: In a small service, a few simple services are enough (YAGNI).
  • License: The MediatR library has a commercial license since version 13 (in 2025). It is paid for larger companies, and only small companies get a free Community edition. You must check this before choosing it.

When is it worth it?

  • When the project is large and has many use cases.
  • When we have a lot of shared work that must run for all requests.
  • When the team works with CQRS and Vertical Slice.

When is it not worth it?

  • When the service is small or is mostly simple CRUD.
  • When the only goal is fewer constructor parameters. In this case, it is better to split the controller into a few smaller controllers, or to split the services more correctly.

Simpler options: For this controller’s problem, these also work:

  1. Split the controller by job (for example, a separate controller for order payment).
  2. Group services that are always used together behind one business service.
  3. In Minimal API, each endpoint takes only its own dependencies.
  4. If we want the Mediator pattern but not the library, a simple internal dispatcher can be written in a few dozen lines.

Follow-up question: Suppose we chose MediatR. We want all Commands to be validated before they run. How do you do this without repeating code in every Handler?

Follow-up answer: With a Pipeline Behavior. This class runs around all Handlers. It gets all Validators for that request from DI and runs them. If there is an error, the Handler does not run at all:

public sealed class ValidationBehavior<TRequest, TResponse>(
    IEnumerable<IValidator<TRequest>> validators)
    : IPipelineBehavior<TRequest, TResponse> where TRequest : notnull
{
    public async Task<TResponse> Handle(TRequest request,
        RequestHandlerDelegate<TResponse> next, CancellationToken ct)
    {
        foreach (var validator in validators)
            await validator.ValidateAndThrowAsync(request, ct);

        return await next(ct);
    }
}

Registering it in DI:

builder.Services.AddMediatR(cfg =>
{
    cfg.RegisterServicesFromAssemblyContaining<Program>();
    cfg.AddOpenBehavior(typeof(ValidationBehavior<,>));
});
builder.Services.AddValidatorsFromAssemblyContaining<Program>();
  • In the same way, logging and transactions are added as separate Behaviors.
  • The order of registering Behaviors matters. The first registered Behavior is the outermost layer.

Red flag: Says “MediatR is always needed and makes the architecture clean” and sees no cost in it. Or does not see that 9 dependencies in the controller are themselves a sign of a design problem.

Report a problem or send a better text

Edit on GitHub

40The Strategy pattern instead of a big switchMedium–Design patterns

Question: We have this code in the payment service:

public class PaymentService(BankAClient bankA, BankBClient bankB, WalletClient wallet)
{
    public async Task<PaymentResult> PayAsync(Order order, string provider, CancellationToken ct)
    {
        switch (provider)
        {
            case "bank-a":
                var token = await bankA.GetTokenAsync(ct);
                return await bankA.PayAsync(token, order.Amount, ct);
            case "bank-b":
                return await bankB.PayAsync(order.Id, order.Amount * 10, ct);
            case "wallet":
                return await wallet.ChargeAsync(order.CustomerId, order.Amount, ct);
            default:
                throw new NotSupportedException(provider);
        }
    }
}

The product team said a few more payment gateways will be added in the coming months. You see this code in code review. What do you think? Does it have a problem? If you want to change it, what design do you suggest?

Short answer: This code works today. But with every new gateway, we must open this same class and change it. This goes against the Open/Closed principle. A better way is the Strategy pattern: one shared interface for all gateways, one class for each gateway, and choosing the gateway with DI. But if we have only 2 or 3 fixed cases, this switch is enough (YAGNI).

Hint (if the candidate says there is no problem): In one year, this class grew from 30 lines to 600 lines and got 15 dependencies. Once, a small change for bank B broke wallet payments. Two developers who were adding two new gateways at the same time had conflicts on this same file every time. To test one gateway, we also had to mock all the clients. Now, why?

Why does this code cause problems over time? (step by step)

  1. Each new gateway means a new case and a new dependency in the constructor. The class keeps getting bigger.
  2. All gateways are in one file. So a change in one may break another.
  3. Each change means testing the whole class again, not only the new gateway.
  4. To test one gateway, you must create or mock all the clients.
  5. Several people working on different gateways at the same time get conflicts on one file.
  6. Usually the same switch is also repeated in other places (refund, status check). So adding a gateway means changing several places in the code.

Solution: Strategy with DI

First, a shared interface:

public interface IPaymentProvider
{
    string Key { get; }
    Task<PaymentResult> PayAsync(Order order, CancellationToken ct);
}

Then, one class for each gateway:

public sealed class BankAProvider(BankAClient client) : IPaymentProvider
{
    public string Key => "bank-a";

    public async Task<PaymentResult> PayAsync(Order order, CancellationToken ct)
    {
        var token = await client.GetTokenAsync(ct);
        return await client.PayAsync(token, order.Amount, ct);
    }
}

And registering all of them in DI:

builder.Services.AddScoped<IPaymentProvider, BankAProvider>();
builder.Services.AddScoped<IPaymentProvider, BankBProvider>();
builder.Services.AddScoped<IPaymentProvider, WalletProvider>();
  • Now, for a new gateway, you only write a new class and add one registration line. The old code is not touched.
  • Each gateway has only its own dependency and is tested separately.
  • The differences of each gateway (for example, converting toman to rial for bank B) stay inside its own class.

When is the switch better?

  • When we have only 2 or 3 cases and they are not going to grow.
  • When each case is only a line or two.
  • In this case, building an interface and several classes is only extra complexity. The YAGNI principle says: do not build it until you need it.

Follow-up question: The user chooses the gateway at run time. How do you find the right gateway without writing a switch again?

Follow-up answer: Three common ways:

  1. Dictionary: Get all IPaymentProviders from DI and put them in a dictionary by Key:
public sealed class PaymentService(IEnumerable<IPaymentProvider> providers)
{
    private readonly Dictionary<string, IPaymentProvider> _map =
        providers.ToDictionary(p => p.Key);

    public Task<PaymentResult> PayAsync(Order order, string key, CancellationToken ct) =>
        _map.TryGetValue(key, out var provider)
            ? provider.PayAsync(order, ct)
            : throw new NotSupportedException(key);
}
  1. Keyed Services: Since .NET 8, DI itself supports registering with a key:
builder.Services.AddKeyedScoped<IPaymentProvider, BankAProvider>("bank-a");

var provider = serviceProvider.GetRequiredKeyedService<IPaymentProvider>(key);
  1. A Factory class: A small class that takes the key and returns the gateway. Inside, it uses one of the two ways above. The rest of the code works only with this Factory.
  • Note: when the key comes from user input, always handle the “unknown key” case properly and return a clear error.

Red flag: Sees no problem in the switch growing. Or the opposite: builds an interface, a Factory and several layers even for 2 simple cases, and cannot say when a switch is enough.

Report a problem or send a better text

Edit on GitHub

41The Decorator pattern and a Repository on EF CoreMedium to hard–Design patternsEntity Framework Core

Question: We have an IProductRepository implemented with EF Core. Now we want to add caching (with Redis) and logging around it. A teammate did it like this:

public async Task<Product?> GetByIdAsync(int id, CancellationToken ct)
{
    _logger.LogInformation("Getting product {Id}", id);
    var cached = await _cache.GetStringAsync($"product:{id}", ct);
    if (cached is not null)
        return JsonSerializer.Deserialize<Product>(cached);

    var product = await _db.Products.FindAsync([id], ct);
    await _cache.SetStringAsync($"product:{id}", JsonSerializer.Serialize(product), ct);
    return product;
}

The same pattern is repeated in all methods of this repository. You see this code in code review. What do you think? If it were you, how would you add caching and logging? And is this repository on EF Core needed at all?

Short answer: This code mixes three jobs in one class: data access, caching and logging. A better way is the Decorator pattern. A new class implements the same interface, holds the original repository inside, and only adds caching around it. The logging class is separate too. These classes are stacked on each other in DI, and the original code is not touched. About the repository: the DbContext is itself a Unit of Work and a Repository. So a generic repository on top of it is often just an extra layer.

Hint (if the candidate says there is no problem): Once Redis went down, and all product read methods failed, even though the database was healthy. When we wanted to change the cache key, we had to change 12 methods one by one, and one was missed. The repository unit tests also did not run without Redis. In one place, a product that did not exist was saved in the cache as an empty value. Now, why?

Where is the problem in this code? (step by step)

  1. Each method has three responsibilities. This goes against the single responsibility principle (SRP).
  2. The cache logic is repeated in all methods. So any change in caching (key, expiration time, error handling) means changing all methods.
  3. If we want to turn the cache off somewhere (for example, in an internal job), there is no simple way.
  4. Testing the data access logic without the cache is not possible.
  5. It also has a small bug: when a product is not found, an empty value goes into the cache. The expiration time is not set either.

Solution: Decorator

The cache class implements the same interface and takes the original repository:

public sealed class CachedProductRepository(
    IProductRepository inner, HybridCache cache) : IProductRepository
{
    public async Task<Product?> GetByIdAsync(int id, CancellationToken ct) =>
        await cache.GetOrCreateAsync($"product:{id}",
            async token => await inner.GetByIdAsync(id, token),
            cancellationToken: ct);

    // Other methods: read from the cache, or just call inner and clear the cache
}
  • Now the original repository works only with EF Core. The cache class knows only about caching.
  • The logging class is a separate Decorator in the same way.
  • The HybridCache class came in .NET 9 and later. It also prevents a rush of concurrent requests for one key (cache stampede).

Registering in DI:

With the Scrutor library, it is simple:

builder.Services.AddScoped<IProductRepository, EfProductRepository>();
builder.Services.Decorate<IProductRepository, CachedProductRepository>();
builder.Services.Decorate<IProductRepository, LoggingProductRepository>();

Without a library, you can also register it by hand:

builder.Services.AddScoped<EfProductRepository>();
builder.Services.AddScoped<IProductRepository>(sp =>
    new LoggingProductRepository(
        new CachedProductRepository(
            sp.GetRequiredService<EfProductRepository>(),
            sp.GetRequiredService<HybridCache>()),
        sp.GetRequiredService<ILogger<LoggingProductRepository>>()));
  • In Scrutor, the last Decorate is the outermost layer. So the run order is: logging, then cache, then EF Core.

Is a Repository on EF Core needed?

  • The DbContext class is itself a Unit of Work. It collects changes and saves them all at once with SaveChanges.
  • Each DbSet is itself similar to a Repository.
  • So a generic repository (with Add, Update, GetAll methods for all tables) is often just an extra layer. Sometimes it also hides EF Core features like Include or projection.
  • But a repository made for one aggregate (like this product repository) is useful in some places:
    • When we want repeated queries to be in one place.
    • When, in DDD, access must go only through the aggregate root.
    • When we want one point for a Decorator (like this cache).
  • In short: a generic repository on EF Core, no. A specific repository with a clear purpose, yes.

Follow-up question: Does the order of Decorators matter? If logging is outside the cache, or inside it, what is the difference?

Follow-up answer: Yes, it matters. Each Decorator only sees the requests that reach its layer.

  1. Logging outside, cache inside: All requests are logged, whether the answer comes from the cache or the database. So we measure the real time the user sees.
  2. Logging inside, cache outside: When the answer is in the cache, the request never reaches logging. So only requests that went to the database are logged. This is good for seeing the database load.
  3. Another example: If we have a Decorator for retry, it must be inside the cache. If it is outside, each retry goes to the cache again, which is useless.
  4. General rule: First decide what each layer must see, then set the order.

Red flag: Copies the cache into every method and sees no problem in it. Or builds a generic repository and a separate Unit of Work on EF Core, and does not know that the DbContext already does this.

Report a problem or send a better text

Edit on GitHub

42Value types and reference typesEasy–Modern C#Memory and the Garbage Collector

Question: A teammate wrote this code to move a point on a map. The test fails: the point does not move.

public struct Point { public int X; public int Y; }

var points = new List<Point> { new() { X = 1, Y = 1 } };
foreach (var p in points.ToArray())
    Move(p);

void Move(Point p) => p.X += 10;

What is the difference between a value type and a reference type? Why does the point not move? Why does this difference matter in real code?

Short answer: A value type (struct, int, DateTime) holds the data itself. When you pass it or assign it, a copy is made. A reference type (class, string, array) holds only an address. When you pass it, both sides point to the same object. Here Move gets a copy of the point, changes the copy, and the original stays the same.

Hint (if the candidate says there is no problem): If we change Point from struct to class, the test passes. Why does one word change the result?

The main differences

Value type Reference type
Examples int, struct, enum, DateTime class, string, array, delegate
What the variable holds The data itself An address of the object
Assign or pass Copies all data Copies only the address
Can be null? No (only with T?) Yes
Where it lives Inline: on the stack or inside its parent On the managed heap

Why does it matter? (step by step)

  1. Surprising copies: Changing a copy does not change the original. This is the bug in the question. A mutable struct is the main source of this bug.
  2. Big struct cost: A struct with many fields is copied on every call. So for big data a class (or in / ref parameters) is better.
  3. Boxing: When a struct is put into an object or a non-generic interface, .NET makes a box on the heap. In a hot loop this makes garbage and work for the GC.
  4. Equality: A struct is compared by its fields. A class is compared by address, unless we override Equals.
  5. “A struct is always on the stack” is wrong. A struct field inside a class lives on the heap with that class.

Solution: Make small structs immutable. Return a new value instead of changing the old one:

public readonly record struct Point(int X, int Y)
{
    public Point Move(int dx) => this with { X = X + dx };
}

points[0] = points[0].Move(10);

When to use a struct? Small (about 16 bytes or less), short-lived, immutable, and it means one value (like Money or a coordinate).

Follow-up question: What is boxing? Show one place in normal code where boxing happens without us seeing it.

Follow-up answer:

  • Boxing means copying a value type into a new object on the heap, so it can be used as object.
  • Hidden example 1: ArrayList or Hashtable (old non-generic collections).
  • Hidden example 2: calling an interface method on a struct through the interface type, like IComparable c = 5;.
  • Hidden example 3: old string.Format("{0}", number) with object parameters. String interpolation in new .NET avoids this.
  • Fix: use generics (List<int>, T where T : struct), so no box is needed.

Red flag: Says “struct is on the stack, class is on the heap” and stops there. Cannot explain copy behavior or boxing.

Report a problem or send a better text

Edit on GitHub

43How garbage collection works in .NETMedium–Memory and the Garbage Collector

Question: An API builds a report. For each request, it reads a file of about 2 MB into a byte[] and makes many small strings. Under load, the p99 response time sometimes jumps from 80 ms to 900 ms, and then comes back. The dashboard shows many Gen 2 collections at the same moments. How does the garbage collector (GC) work in .NET? Why can this code make these pauses?

Short answer: The GC finds objects that nothing points to and frees their memory. To be fast, it splits the heap into generations: Gen 0 (new objects), Gen 1, and Gen 2 (old objects). Most objects die young, so Gen 0 collections are cheap and frequent. But objects of 85,000 bytes or more go to the Large Object Heap (LOH), which is collected only with Gen 2. A 2 MB array per request fills the LOH fast and forces expensive Gen 2 collections.

Hint (if the candidate says there is no problem): When we cut the file into 64 KB chunks, the Gen 2 count dropped a lot and the spikes went away. Why does the size of the array matter?

How the GC works (step by step)

  1. Allocation is cheap: a new object is placed at the end of Gen 0, like moving a pointer forward.
  2. Mark: when Gen 0 is full, the GC starts from the roots (static fields, local variables on thread stacks, CPU registers, GC handles) and marks every object it can reach.
  3. Sweep and compact: unmarked objects are garbage. Live objects are moved together (compacted) and promoted to the next generation.
  4. Generations: a Gen 0 collection only looks at young objects, so it is fast. Gen 2 looks at the whole heap, so it is slow.
  5. LOH: big objects are not moved by default (moving them is costly). So the LOH can become fragmented.
  6. Pauses: during some phases, the GC stops managed threads. Background GC lets Gen 2 run mostly next to the app, but Gen 0 and Gen 1 still pause.

Server GC vs Workstation GC

  • Server GC (default in ASP.NET Core): one heap and one GC thread per core. More throughput, more memory.
  • Workstation GC: made for desktop apps. Less memory, often smaller pauses.
  • In containers, .NET reads the CPU and memory limits and sizes the heaps from them.

Solution for this case

  • Do not load the whole file. Stream it, or use ArrayPool<byte>.Shared to rent and return buffers.
  • Use Span<T> / Memory<T> to slice data without new arrays.
  • Measure first: dotnet-counters (gc-heap-size, gen-2-gc-count, % time in GC) and dotnet-trace for allocation stacks.
  • Do not call GC.Collect() to “fix” it. It usually makes things worse.

Follow-up question: What do IDisposable and a finalizer have to do with the GC? Does Dispose free memory?

Follow-up answer:

  • Dispose does not free managed memory. It frees unmanaged resources (file handles, sockets, DB connections) right away. The GC frees the memory later.
  • A finalizer (~MyClass) runs on a special thread before the object is freed. An object with a finalizer survives at least one more GC, so it is more expensive.
  • The right pattern: using / await using to call Dispose on time. SafeHandle instead of writing your own finalizer.

Red flag: Says “the GC runs when memory is full” and knows nothing about generations or the LOH. Or suggests calling GC.Collect() in production code.

Report a problem or send a better text

Edit on GitHub

44When to use a record instead of a classEasy–Modern C#

Question: A teammate changed all entity classes to records, because “records are modern and shorter”:

public record Order(Guid Id, string Status, List<OrderLine> Lines);

var a = new Order(id, "New", lines);
var b = new Order(id, "New", lines);
Console.WriteLine(a == b); // true

db.Orders.Add(a);

You see this in code review. When should we use a record, and when a class? Is it a good idea for EF Core entities?

Short answer: A record is made for data with value meaning: two records with the same data are equal. It gives value-based Equals, GetHashCode, ToString, and the with copy. This fits DTOs, messages, events and value objects. An entity has an identity and changes over time, so a class is a better fit. EF Core tracks entities by reference and identity, and value equality plus with copies can confuse that.

Hint (if the candidate says there is no problem): A developer used order with { Status = "Paid" } and then called SaveChanges. Nothing was saved. Also, an order stored in a HashSet was “lost” after its status changed. Why?

What a record gives you

  • Value equality: compares all properties, not the address.
  • Positional syntax: record Money(decimal Amount, string Currency); makes a constructor and init-only properties.
  • with expression: makes a new copy with some changes. It does not change the original.
  • Nice ToString: prints all properties, good for logs.
  • record class (default) is a reference type. record struct is a value type.

Why records are a poor fit for entities (step by step)

  1. with creates a new object. EF Core still tracks the old one, so the change is not saved.
  2. Value equality on mutable data: if a property changes, the hash code changes. A record inside a HashSet or as a dictionary key is then “lost”.
  3. Two different orders with the same data become “equal”. But for an entity, only the Id should decide this.
  4. The equality is shallow: List<OrderLine> is compared by reference, so a == b is true only because both use the same list object.

Good uses of record

public record CreateOrderRequest(Guid CustomerId, List<LineDto> Lines);
public record OrderPlaced(Guid OrderId, DateTimeOffset At);
public readonly record struct Money(decimal Amount, string Currency);

Simple rule: It is data that is defined by its values (DTO, message, value object) → record. It has an identity and a life cycle (entity, service) → class.

Follow-up question: Is a record immutable? Can someone change it?

Follow-up answer:

  • Positional properties are init-only, so they cannot be set after creation. This is shallow immutability.
  • If a property is a List<T>, someone can still add or remove items. Use IReadOnlyList<T> or ImmutableArray<T>.
  • You can also add normal set properties to a record, and then it is mutable.
  • Also, the compiler-made equality for collections compares references, not items. For a value object with a list, write your own Equals.

Red flag: Thinks record is just “shorter syntax for a class”. Does not know about value equality or that with makes a new object.

Report a problem or send a better text

Edit on GitHub

45IEnumerable vs IQueryable: the deferred execution trapMedium–LINQ and collectionsEntity Framework Core

Question: The Orders table has 5 million rows. A teammate wrote a shared helper and an endpoint that uses it:

public IEnumerable<Order> GetOrders() => _db.Orders;

var page = _repo.GetOrders()
    .Where(o => o.CustomerId == customerId)
    .OrderByDescending(o => o.CreatedAt)
    .Take(20)
    .ToList();

On a test database it is fast. In production the endpoint takes 40 seconds and memory jumps. What is the difference between IEnumerable<T> and IQueryable<T>? Where is the problem?

Short answer: IQueryable<T> builds an expression tree. EF Core turns it into SQL, so Where, OrderBy and Take run in the database. IEnumerable<T> runs LINQ in memory (LINQ to Objects). Because the helper returns IEnumerable<Order>, the Where, OrderBy and Take after it are in-memory methods. EF sends SELECT * FROM Orders, reads 5 million rows, and filters them in C#.

Hint (if the candidate says there is no problem): SQL Profiler shows a query with no WHERE and no TOP. Why are our filters not in the SQL?

The difference

IEnumerable IQueryable
Where it runs In memory, in .NET In the data source (SQL)
Lambda type Func<T, bool> (code) Expression<Func<T, bool>> (data)
Good for Lists already in memory Database queries

The deferred execution trap (step by step)

  1. Both types are lazy. Nothing runs until we enumerate: foreach, ToList, Count, First.
  2. The static type decides which Where the compiler picks. On IEnumerable<T> it is Enumerable.Where. On IQueryable<T> it is Queryable.Where.
  3. So after the helper “casts down” to IEnumerable, EF cannot see later filters.
  4. Second trap: enumerating twice runs the query twice:
var orders = _db.Orders.Where(o => o.IsOpen);
var count = orders.Count();   // query 1
var list = orders.ToList();   // query 2, may differ
  1. Third trap: a lazy query that outlives its DbContext (returned from a method after the using ends) throws ObjectDisposedException later.

Solution

  • Keep IQueryable<T> inside the data layer, and put the filters before materializing. Or give the repository a specific method:
public Task<List<OrderDto>> GetPageAsync(
    Guid customerId, int take, CancellationToken ct) =>
    _db.Orders.AsNoTracking()
        .Where(o => o.CustomerId == customerId)
        .OrderByDescending(o => o.CreatedAt)
        .Take(take)
        .Select(o => new OrderDto(o.Id, o.Total))
        .ToListAsync(ct);
  • Return a materialized list (List<T>, IReadOnlyList<T>) from the data layer, not a lazy query.

Follow-up question: Then should we always return IQueryable<T> from the repository?

Follow-up answer:

  • Not always. Returning IQueryable<T> lets every caller build any query. The data layer loses control: no one knows which indexes are needed, and queries spread across the code.
  • Also, some C# methods cannot be translated to SQL. With IQueryable, this error shows up far from the data layer, at run time.
  • A common middle way: query methods with a clear purpose that return finished DTOs. Or the Specification pattern if you need flexible filters.

Red flag: Says “they are the same, IQueryable inherits from IEnumerable” and cannot say where the query runs.

Report a problem or send a better text

Edit on GitHub

46What happens when an HTTP request hits your APIMedium–Middleware and the pipelineMinimal APIs and controllers

Question: A user sends POST /api/orders with a JSON body and a JWT token. Our ASP.NET Core API runs in a container behind a load balancer. Tell me, step by step, what happens from the moment the request reaches the server until the response goes back. Where can it fail or become slow?

Short answer: The request goes through layers: the load balancer and TLS, then Kestrel (the web server), then the middleware pipeline (exception handling, HTTPS, routing, authentication, authorization…), then the endpoint: model binding, validation, filters, our handler code, and DI services. The response goes back through the same middleware in reverse order.

Hint (if the candidate says there is no problem): Some requests return 401 before our controller is called, and the log of our controller shows nothing. Some others are slow, but the controller’s own timer says 20 ms. Where did the time go?

The journey (step by step)

  1. Network: DNS, TCP, TLS handshake. Often TLS ends at the load balancer or ingress, and Kestrel gets plain HTTP.
  2. Kestrel: reads the bytes, parses the HTTP request, and makes an HttpContext. It also applies limits like max body size and timeouts.
  3. Request scope: a DI scope is created for this request. Scoped services (like DbContext) live in it.
  4. Middleware pipeline, in the order of Program.cs. A common order:
app.UseExceptionHandler("/error");
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();
app.UseRateLimiter();
app.MapControllers();
  1. Routing picks the endpoint (OrdersController.Create) from the URL and method.
  2. Authentication reads the JWT and builds HttpContext.User. Authorization checks the endpoint’s policy. If it fails: 401 or 403, and the controller is never called.
  3. Endpoint: model binding reads the JSON body into a DTO, validation runs, filters run (for controllers), then our action method. It calls services, the database, other APIs.
  4. Response: the result is serialized to JSON. It goes back out through the middleware in reverse order (e.g. logging, compression), then Kestrel writes the bytes.
  5. At the end, the scope is disposed, and so are scoped services.

Where it gets slow or fails

  • Before the controller: TLS, a slow middleware, auth calling an outside server to get signing keys, rate limiter queue.
  • Model binding of a huge body.
  • Thread pool starvation (sync over async) delays everything.
  • After the controller: JSON serialization of a big object graph.
  • Good tracing (OpenTelemetry) shows each part as a span, so we see where the time goes.

Follow-up question: What is the difference between a middleware and an MVC filter? When do you use each one?

Follow-up answer:

  • A middleware runs for every request in the pipeline. It only knows HttpContext. Good for global things: errors, logging, headers, CORS.
  • A filter runs only inside MVC, around actions. It knows the action, its arguments and the ModelState. Good for action-level things: validation, a specific audit, result changes.
  • Minimal APIs have endpoint filters with a similar role.

Red flag: Jumps directly to “the controller is called”. Cannot name the middleware pipeline or explain why the order matters.

Report a problem or send a better text

Edit on GitHub

47Middleware ordering mistakes that cause real bugsMedium–Middleware and the pipelineAuthentication and authorization

Question: After a “small cleanup” of Program.cs, three bugs appeared: every request to [Authorize] endpoints gets 401 even with a valid token, browser calls fail with a CORS error, and some unhandled errors return an empty 500 with no log. This is the new code:

app.UseRouting();
app.UseAuthorization();
app.UseAuthentication();
app.MapControllers();
app.UseCors("web");
app.UseExceptionHandler("/error");

What is wrong? Why does the order of middleware matter?

Short answer: Middleware runs in the order it is added. Each one can do work, call the next one, and do work after it. So: Authorization runs before Authentication, so User is still empty → 401. CORS is added after the endpoints are mapped, so it never runs for those requests. The exception handler is last, so it cannot catch errors that happen in the middleware before it.

Hint (if the candidate says there is no problem): If we move UseExceptionHandler to the top, the error log comes back. Why does the position of an error handler matter?

How the pipeline works

  • Think of nested functions: A(B(C(endpoint))). Code before next() runs on the way in, code after next() runs on the way out.
  • A middleware can short-circuit: return a response and not call the next one (static files, CORS preflight, auth failure).
  • So a middleware only sees what happens inside it (later in the list).

A safe order (step by step)

app.UseExceptionHandler("/error"); // 1. outermost
app.UseHsts();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseCors("web");                // after routing
app.UseAuthentication();           // who are you?
app.UseAuthorization();            // are you allowed?
app.UseRateLimiter();
app.MapControllers();
  1. Exception handler first, so it wraps everything.
  2. Static files early, so they skip auth and routing work (if they are public).
  3. CORS after routing and before auth, so preflight OPTIONS requests get answered.
  4. Authentication before Authorization, always.
  5. Endpoint-specific middleware (rate limiter with endpoint policies) after routing, because it needs to know the endpoint.

Other real ordering bugs

  • Request logging placed inside the exception handler sees the exception, but not the final 500 status. Placed outside, it sees the final status. Choose on purpose.
  • Response compression placed after a middleware that already wrote the body does nothing.
  • A custom middleware that reads context.GetEndpoint() before UseRouting() always gets null.

Follow-up question: You wrote a middleware that reads the request body for logging. Now model binding gets an empty body. Why, and how do you fix it?

Follow-up answer:

  • The request body is a forward-only stream. After the middleware reads it, the position is at the end, so the next reader sees nothing.
  • Fix: call context.Request.EnableBuffering(), read it, then set Body.Position = 0 before calling next().
  • But be careful: buffering big bodies costs memory. Log only small bodies, and hide sensitive data. The built-in UseHttpLogging() can do this with limits.

Red flag: Thinks the order in Program.cs “does not matter, ASP.NET sorts it”. Cannot explain what happens before and after next().

Report a problem or send a better text

Edit on GitHub

48EF Core or Dapper: how do you decide?Medium–Entity Framework Core

Question: A new team lead says: “EF Core is slow. From now on, all data access must use Dapper with hand-written SQL.” The project is an order management system: many forms with create and update, some complex reports, and one search page that is hot (2,000 requests per second). What do you think? How do you decide between EF Core and Dapper?

Short answer: Both are good tools for different jobs. EF Core is a full ORM: change tracking, migrations, LINQ, relationships, Unit of Work. It saves a lot of time for write-heavy business logic. Dapper is a micro ORM: you write SQL, it maps rows to objects very fast. It is great for hot reads and complex reports. The best answer is often both: EF Core for commands, Dapper (or raw SQL) for a few hot or complex queries. Decide with measurements, not opinions.

Hint (if the candidate says there is no problem): After moving everything to Dapper, the team wrote 300 SQL strings by hand. A column rename broke 14 queries at run time, and nobody noticed until production. Why?

Comparison

EF Core Dapper
You write LINQ (SQL is generated) SQL by hand
Change tracking, Unit of Work Yes No
Migrations Yes No (use another tool)
Speed Good, with care Very close to raw ADO.NET
Type safety on rename Compile errors Run-time errors
Complex SQL (CTE, window functions) Harder Easy

Why “EF Core is slow” is often wrong (step by step)

  1. Most “slow EF” is a usage problem: N+1, tracking for read-only data, loading whole entities instead of projections, missing indexes.
  2. Modern EF Core is fast for reads with AsNoTracking() and Select to a DTO.
  3. Compiled queries remove the LINQ translation cost for a hot query.
  4. EF Core can run raw SQL too: FromSql, and SqlQuery<T> for non-entity types (EF Core 8+).
  5. Bulk updates without loading: ExecuteUpdateAsync / ExecuteDeleteAsync (EF Core 7+).

A practical mix

// Command side: EF Core, tracking, domain rules
var order = await db.Orders.FindAsync([id], ct);
order.Confirm();
await db.SaveChangesAsync(ct);

// Hot search: Dapper, hand-tuned SQL
var rows = await conn.QueryAsync<OrderRow>(
    "SELECT Id, Total FROM Orders WHERE Code = @code",
    new { code });
  • Both can share the same connection and transaction when needed.
  • Always pass parameters in Dapper. Never build SQL with string concat (SQL injection).

Follow-up question: How do you prove the hot search query is really faster with Dapper?

Follow-up answer:

  • Measure both versions with BenchmarkDotNet for the mapping cost, and a load test (k6, NBomber) for the real endpoint.
  • Look at the SQL EF generates (ToQueryString() or logs). Often the SQL is the same, and the difference is tiny.
  • Check the database side: execution plan and index use. A missing index costs much more than any ORM.
  • Only switch if the gain is real and matters for the users.

Red flag: Picks one tool for everything because of an opinion (“ORMs are bad” or “raw SQL is old”). Or uses string concat to build Dapper SQL.

Report a problem or send a better text

Edit on GitHub

49A query is fast in staging but slow in productionHard–SQL and indexesEntity Framework Core

Question: This EF Core query takes 15 ms in staging. In production it sometimes takes 8 seconds. It is not slow all the time: after a restart of the database it is fast for a while, then slow again. The code is the same in both places.

var orders = await db.Orders
    .Where(o => o.TenantId == tenantId
             && o.Status == status)
    .OrderByDescending(o => o.CreatedAt)
    .Take(50)
    .ToListAsync(ct);

How do you investigate this, step by step? What are the likely causes?

Short answer: Same code does not mean same query plan. Production has different data size, data shape (one huge tenant), statistics, indexes, load and settings. The “fast after restart, then slow” pattern is a strong sign of parameter sniffing: SQL Server caches a plan made for the first parameter values. If the first call was for a tiny tenant, the plan is bad for a huge tenant (or the opposite).

Hint (if the candidate says there is no problem): The slow calls are all for tenant 7, which has 60% of all orders. For other tenants the query is fast. What does the database do differently?

How to investigate (step by step)

  1. Get facts: which calls are slow? Use logs and traces with the parameter values. Is it always slow, or only for some parameters or times?
  2. Get the real SQL: EF Core logs or ToQueryString(). Check it is the same in both places.
  3. Get the real plan from production: Query Store (SQL Server) or EXPLAIN ANALYZE (PostgreSQL). Compare the estimated rows with the actual rows. A big gap means bad statistics or sniffing.
  4. Check indexes: is there an index like (TenantId, Status, CreatedAt DESC)? Staging often has the index from a script, production does not (or the opposite).
  5. Check statistics: old statistics after a big data load make wrong estimates.
  6. Check waits and blocking: maybe the query is not slow, but waits for a lock held by another job.
  7. Check resources: CPU, IO, memory grants, and other heavy queries at the same time (a nightly report).

Common causes and fixes

Cause Fix
Parameter sniffing on skewed data Better index; OPTION (RECOMPILE) for this query; plan forcing in Query Store
Missing or different index Add a covering index that matches filter and sort
Old statistics Update statistics; check the auto-update setting
Blocking Shorter transactions; snapshot isolation for reads
Data type mismatch Match parameter type to column (e.g. varchar vs nvarchar), so the index can be used

Make staging closer to production: similar data volume and skew (anonymized), the same indexes from migrations, and a load test before release.

Follow-up question: What is the “implicit conversion” problem, and how can EF Core cause it?

Follow-up answer:

  • If a column is varchar but the parameter is sent as nvarchar, SQL Server must convert the column value on every row. Then it cannot seek on the index; it scans.
  • EF Core sends string as nvarchar by default. If the column is varchar, configure it: .IsUnicode(false) or HasColumnType("varchar(50)").
  • In the plan you see a CONVERT_IMPLICIT warning and an index scan.

Red flag: Says “just add more CPU” or “restart the database”. Or blames EF Core without looking at the execution plan.

Report a problem or send a better text

Edit on GitHub

50App Service vs Container Apps vs AKS: how do you choose?Design–KubernetesDocker

Question: Our company is moving to Azure. We have one ASP.NET Core API, two background workers that read from a queue, and a small team of 5 developers with no Kubernetes experience. One developer wants to start with AKS “because everyone uses Kubernetes”. Where would you host this, and why? When would your answer change?

Short answer: Pick the simplest platform that meets the needs. Each step up gives more control but more work:

  • App Service: a managed web host. Push code or a container, get scaling, TLS, slots. Best for simple web apps and APIs.
  • Container Apps: managed containers on top of Kubernetes, without managing Kubernetes. Scale to zero, scale by queue length (KEDA), Dapr, revisions. Good for APIs plus workers and small microservices.
  • AKS: full Kubernetes. Full control, but you run upgrades, node pools, networking, ingress, monitoring.

For this team, Container Apps (or App Service for the API) is a better start. AKS would cost them a lot of time with little gain now.

Hint (if the candidate says there is no problem): Six months after choosing AKS, two developers spend half their time on cluster upgrades, ingress certificates and node issues, and feature work is late. What did the choice really cost?

Comparison

App Service Container Apps AKS
You manage Your app Your containers The cluster too
Unit App / container Container Pod, any K8s object
Scale to zero No (mostly) Yes With extra tools
Scale on queue length Limited Yes (KEDA built in) Yes (install KEDA)
Custom K8s tools (operators, Helm, service mesh) No No Yes
Team skill needed Low Medium High

How to decide (step by step)

  1. What do you run? Only web apps → App Service. Web + workers + event-driven scaling → Container Apps.
  2. Team skill and size. Who is on call for the platform? Without K8s experts, AKS is a risk.
  3. Special needs. You need custom operators, special networking, GPU nodes, a service mesh, or a strict multi-tenant cluster → AKS.
  4. Cost model. Scale to zero helps workers with uneven load. AKS has a fixed node cost even when idle.
  5. Exit path. Containers keep you portable. Starting on Container Apps and moving to AKS later is not hard, because both run the same images.

What would change my answer? Many services (20+), a platform team, or needs that only full Kubernetes has.

Follow-up question: How do you scale the background workers based on the queue, not on CPU?

Follow-up answer:

  • CPU is a bad signal for workers: they may be idle on CPU while 50,000 messages wait.
  • In Container Apps, add a KEDA scale rule for the queue (Service Bus, Storage Queue, Kafka) with a target like “1 replica per 100 messages”, and min replicas 0 or 1.
  • In AKS, install KEDA and use a ScaledObject with the same idea.
  • Make the workers safe for this: handle shutdown well (finish or return the message), and be idempotent, because scale-in can stop a worker in the middle.

Red flag: Picks AKS (or any platform) “because it is popular”, without asking about team skill, workload type or cost.

Report a problem or send a better text

Edit on GitHub

51Managing secrets from local dev to productionMedium–SecurityCI/CD

Question: In a new project you find this in appsettings.json, which is in Git:

{
  "ConnectionStrings": {
    "Db": "Server=prod-sql;User Id=sa;Password=P@ss123"
  },
  "Payment": { "ApiKey": "sk_live_8f2a..." }
}

The same file is copied to every developer’s machine and to the build server. What are the risks? How should secrets be handled from local development to production? What are managed identities, and how do they help?

Short answer: A secret in Git is a leaked secret: everyone with repo access (now and in the future, and every clone and backup) has the production password and the live payment key. Secrets must live outside the code: User Secrets on a developer machine, a secret store (Azure Key Vault, AWS Secrets Manager, HashiCorp Vault) in the cloud, and environment variables or mounted files only as the delivery way. Best of all, avoid secrets: a managed identity lets the app prove who it is to Azure services with no password at all.

Hint (if the candidate says there is no problem): An intern pushed a copy of the repo to a public GitHub repo for 10 minutes. Two hours later the payment provider reports strange charges. If we delete the file now, are we safe?

Step by step: the right setup

  1. Rotate now. The leaked password and key are burned. Change them first, then clean up. Deleting the file does not remove it from Git history.
  2. Local dev: dotnet user-secrets set "Payment:ApiKey" "...". Stored in the user profile, outside the repo, and loaded only in Development. Better: use local containers (DB in Docker) with dev-only passwords.
  3. CI/CD: keep secrets in the pipeline’s secret store (GitHub Actions secrets, Azure DevOps variable groups linked to Key Vault). They are masked in logs.
  4. Production: the app reads from Key Vault at startup:
builder.Configuration.AddAzureKeyVault(
    new Uri("https://my-vault.vault.azure.net/"),
    new DefaultAzureCredential());
  1. No secret for the vault itself: DefaultAzureCredential uses the managed identity in Azure, and your own az login on your machine. So the same code works everywhere.
  2. Prevent it again: secret scanning in the repo (GitHub secret scanning, gitleaks in CI), and code review.

What is a managed identity?

  • An identity in Microsoft Entra ID that Azure creates for your resource (App Service, Container App, VM, AKS workload).
  • Azure gives the app short-lived tokens automatically. There is no password to store, leak or rotate.
  • You give this identity a role, for example “Key Vault Secrets User” or “Storage Blob Data Reader”, with least privilege.
  • Many services accept it directly, so even the DB password can go away (Entra authentication for Azure SQL).
  • System-assigned: tied to one resource, deleted with it. User-assigned: a separate resource you can share between apps.

Follow-up question: How do you rotate a secret (for example an API key) without downtime?

Follow-up answer:

  • Support two valid keys at the same time (most providers allow a primary and a secondary key).
  • Steps: create the new key → put it in Key Vault → the app picks it up (reload config on a timer, or restart pods one by one) → check traffic uses the new key → revoke the old key.
  • Do not cache the secret forever in memory; use a reload interval.
  • Automate it and set alerts before keys expire.

Red flag: Thinks deleting the file from the repo is enough. Or “encrypts” the secret in appsettings with a key that is also in the repo.

Report a problem or send a better text

Edit on GitHub

52Your ideal CI/CD pipeline and zero-downtime deploymentsMedium to hard–CI/CDKubernetes

Question: Today the team deploys like this: a developer builds on their own laptop, copies the files to the server on Friday evening, stops the site, replaces the files, and starts it again. Users see an error page for about 5 minutes, and twice last month a wrong build reached production. Describe your ideal CI/CD pipeline for a .NET API. How do you deploy with zero downtime?

Short answer: Every change goes through the same automated pipeline: build once, test, scan, make one immutable artifact (a container image), and promote that same artifact through environments (dev → staging → production) with checks in between. For zero downtime, never stop all instances at once: use rolling, blue-green or canary deployments, with health checks, graceful shutdown, and backward-compatible database changes.

Hint (if the candidate says there is no problem): “It works on my machine.” The production build had a different SDK version and an old config file. Why is “build on a laptop” dangerous even with good developers?

The pipeline (step by step)

  1. On every pull request (CI): restore, build, run unit tests and integration tests (Testcontainers), code analysis, dependency and secret scanning. The PR cannot merge if any step fails.
  2. On merge to main: build the container image once, tag it with the commit SHA (not latest), and push it to a registry.
  3. Deploy to staging automatically. Run smoke tests and maybe a short load test.
  4. Production: the same image, with an approval gate if needed. Config and secrets come from the environment, not from the image.
  5. After deploy: watch metrics and errors. Roll back automatically if health checks or SLOs fail.
  6. Database migrations run as a separate, controlled step (migration bundle or script), and must work with both the old and new code.

Zero-downtime strategies

Strategy How Good for
Rolling Replace instances one by one Default in Kubernetes, simple
Blue-green Start the full new version next to the old, then switch traffic Fast rollback (switch back)
Canary Send 5% of traffic to the new version, then grow Risky changes, catching bugs early

What makes it really zero downtime

  • Readiness probe: traffic only goes to an instance after it is ready (warm-up done, DB reachable).
  • Graceful shutdown: the old instance stops getting new requests, finishes current ones, then exits (handle SIGTERM; set a long enough shutdown timeout).
  • Compatible changes: old and new code run together for a while, so the DB schema and API contracts must work for both (expand and contract).
  • Feature flags: deploy code switched off, then turn it on separately. Deploy and release become two different steps.

Follow-up question: The new version has a bug. How fast can you go back, and what can make rollback impossible?

Follow-up answer:

  • With immutable images, rollback is “deploy the previous SHA”, or switch traffic back in blue-green. It should take minutes, and be practiced.
  • What breaks rollback: a DB migration that dropped or renamed a column the old code needs; a message format the old code cannot read; data written in a new format.
  • So: destructive DB changes come only in a later release, after the new code is stable. And messages and APIs stay backward compatible.

Red flag: Builds a different artifact for each environment, or deploys by copying files by hand. Thinks zero downtime is only about the load balancer, and forgets the database.

Report a problem or send a better text

Edit on GitHub

53How do you containerize a .NET application?Medium–Docker

Question: A teammate wrote this Dockerfile for our ASP.NET Core API. The image is 1.9 GB, every build takes 6 minutes even for a one-line change, and the security scanner reports many issues.

FROM mcr.microsoft.com/dotnet/sdk:9.0
WORKDIR /app
COPY . .
RUN dotnet publish -c Release -o out
ENV ConnectionStrings__Db="Server=prod;Password=..."
ENTRYPOINT ["dotnet", "out/MyApi.dll"]

What is wrong with it? How would you containerize this app?

Short answer: It ships the whole SDK (compilers, tools) and the source code to production, it breaks the layer cache (COPY . . before restore), it puts a secret inside the image, and it runs as root. Use a multi-stage build: build with the SDK image, then copy only the published output into a small runtime image (aspnet), restore packages in their own cached layer, run as a non-root user, and pass config and secrets at run time.

Hint (if the candidate says there is no problem): Anyone who can pull the image can run docker history and read the password. And a change in one .cs file makes NuGet download all packages again. Why?

A better Dockerfile

FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /src
COPY MyApi/MyApi.csproj MyApi/
RUN dotnet restore MyApi/MyApi.csproj
COPY . .
RUN dotnet publish MyApi -c Release -o /out \
    --no-restore

FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS final
WORKDIR /app
COPY --from=build /out .
USER $APP_UID
EXPOSE 8080
ENTRYPOINT ["dotnet", "MyApi.dll"]

Why each part (step by step)

  1. Multi-stage: the final image has only the runtime and our DLLs. Hundreds of MB smaller, and fewer things to attack.
  2. Copy csproj, restore, then copy the rest: Docker caches each layer. Packages are downloaded again only when the csproj changes.
  3. .dockerignore: skip bin/, obj/, .git, local secrets. Smaller context, faster build, no leaks.
  4. Non-root user: .NET 8+ images include an app user ($APP_UID). If the app is hacked, the attacker is not root.
  5. Port 8080: since .NET 8, the default port in the images is 8080 (non-root cannot bind port 80).
  6. No secrets in the image: pass config with environment variables or mounted files at run time, from the orchestrator’s secret store.
  7. Pin versions and rebuild often, to get security patches. Scan the image in CI.

More options

  • dotnet publish /t:PublishContainer can build an image without a Dockerfile.
  • Smaller bases: aspnet:9.0-alpine or chiseled Ubuntu images (no shell, no package manager).
  • Native AOT for very small and fast-starting images, if all libraries support it.
  • In containers, .NET respects the CPU and memory limits, so set them on purpose.

Follow-up question: The container starts, but Kubernetes kills it after 30 seconds. How do you make the app “container-friendly”?

Follow-up answer:

  • Add health checks: builder.Services.AddHealthChecks() and app.MapHealthChecks("/healthz"), and point liveness and readiness probes to them. Readiness should check what the app needs (DB), liveness should be light.
  • Log to stdout as structured JSON, not to files in the container.
  • Handle SIGTERM: let the host stop gracefully; set HostOptions.ShutdownTimeout to fit.
  • Keep the container stateless: no uploads or sessions on the local disk.

Red flag: Ships the SDK image to production, bakes secrets into the image, or uses the latest tag for deploys.

Report a problem or send a better text

Edit on GitHub

54How authentication works in your API, and 401 vs 403Medium–Authentication and authorization

Question: Our SPA signs users in with an identity provider (like Entra ID, Auth0 or Keycloak) and calls our ASP.NET Core API with a token. The mobile team reports: “When a user is not an admin and calls DELETE /api/users/5, the API returns 401, so the app logs the user out.” This is the setup:

builder.Services
    .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(o =>
    {
        o.Authority = "https://login.example.com/";
        o.Audience = "orders-api";
    });

Explain how authentication works here, step by step. What is the difference between 401 and 403? Why might the API return the wrong one?

Short answer: Authentication answers “who are you?”. Authorization answers “what can you do?”. The SPA gets an access token (a JWT) from the identity provider with OpenID Connect. The API does not call anyone for each request: it checks the token’s signature with the provider’s public keys, and checks issuer, audience and expiry. 401 Unauthorized means “I don’t know who you are” (no token, bad or expired token): sign in again. 403 Forbidden means “I know you, but you are not allowed”: do not sign out, show “no access”. A wrong 401 often comes from custom code that returns Unauthorized() for permission checks, or from a missing authentication setup for that endpoint.

Hint (if the candidate says there is no problem): In the controller someone wrote if (!User.IsInRole("Admin")) return Unauthorized();. What does the client think this means?

How it works (step by step)

  1. The user signs in at the identity provider (Authorization Code flow with PKCE for SPAs and mobile apps). The app never sees the password.
  2. The provider returns an ID token (for the app: who signed in) and an access token (for the API).
  3. The SPA sends Authorization: Bearer <access token> on each call.
  4. UseAuthentication() runs the JwtBearer handler. At the first request, it downloads the provider’s metadata and signing keys (JWKS) from the Authority and caches them.
  5. It validates the signature, iss, aud, exp / nbf. If valid, it builds HttpContext.User from the claims.
  6. UseAuthorization() checks the endpoint’s rules. Not signed in → challenge → 401 (with a WWW-Authenticate header). Signed in but rule fails → forbid → 403.

Fix for the bug

[Authorize(Roles = "Admin")]
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id) { ... }

// or, for checks inside the code:
if (!allowed) return Forbid();

Common mistakes

  • Using the ID token to call the API (wrong audience).
  • Not checking the audience, so a token for another API is accepted.
  • Long-lived access tokens with no refresh strategy.
  • Putting tokens in localStorage in a browser app without thinking about XSS. Often the BFF pattern (cookies, token kept on the server) is safer.

Follow-up question: For a resource that the user is not allowed to see, should the API return 403 or 404?

Follow-up answer:

  • 403 tells the caller “this thing exists”. For sensitive data (another customer’s order), this leaks information.
  • Many APIs return 404 when the user has no right to even know the resource exists (like GitHub for private repos).
  • Rule: if the existence is not secret, 403 is clearer. If it is secret, 404. Be consistent across the API.

Red flag: Mixes up authentication and authorization, or says “401 and 403 are the same”. Or thinks the API must call the identity provider for every request to check a JWT.

Report a problem or send a better text

Edit on GitHub

55Role-based and policy-based authorizationMedium–Authentication and authorization

Question: Our authorization rules are spread in controllers like this:

[Authorize(Roles = "Admin,Manager,RegionalManager,Finance")]
public async Task<IActionResult> Approve(int id)
{
    var invoice = await _db.Invoices.FindAsync(id);
    if (!User.IsInRole("Admin")
        && invoice.Amount > 10_000)
        return Forbid();
    ...
}

Now the business adds a new rule: “a manager can approve only invoices of their own department”. Every new rule means changing many controllers. How do role-based and policy-based authorization work in ASP.NET Core? How would you design this?

Short answer: Role-based checks if the user is in a role: simple, but roles grow and mix with business rules. Policy-based gives each rule a name (like CanApproveInvoice) and puts the logic in one place: requirements and handlers. Endpoints only say which policy they need. For rules that depend on the data (this invoice’s department or amount), use resource-based authorization with IAuthorizationService.

Hint (if the candidate says there is no problem): An auditor asks: “Show me everyone who can approve a 50,000 invoice, and where this rule is written.” How long does it take to answer today?

Role vs policy

Role-based Policy-based
Asks “Is the user in role X?” “Does the user meet rule X?”
Logic lives in Attributes spread in the code One registered policy
Can use claims, data, time, other services No Yes
Change a rule Edit many endpoints Edit one handler

Design (step by step)

  1. Name the rules by permission, not by job title: invoices.approve.
  2. Register policies once:
builder.Services.AddAuthorizationBuilder()
    .AddPolicy("CanApproveInvoice", p => p
        .RequireAuthenticatedUser()
        .AddRequirements(new ApproveInvoiceRequirement()));
builder.Services.AddScoped<IAuthorizationHandler,
    ApproveInvoiceHandler>();
  1. Write the handler for the resource:
public class ApproveInvoiceHandler : AuthorizationHandler<
    ApproveInvoiceRequirement, Invoice>
{
    protected override Task HandleRequirementAsync(
        AuthorizationHandlerContext ctx,
        ApproveInvoiceRequirement req, Invoice inv)
    {
        var dept = ctx.User.FindFirst("department")?.Value;
        if (ctx.User.IsInRole("Admin")
            || (dept == inv.Department && inv.Amount <= 10_000))
            ctx.Succeed(req);
        return Task.CompletedTask;
    }
}
  1. Call it where you have the resource:
var result = await _authz.AuthorizeAsync(
    User, invoice, "CanApproveInvoice");
if (!result.Succeeded) return Forbid();
  1. Test the handlers with unit tests: many users × many invoices.

Notes

  • [Authorize(Policy = "...")] works for rules without data. Resource-based is for rules that need the loaded object.
  • Roles still have a place: they can be one input inside a policy.
  • Set a fallback policy so every endpoint needs an authenticated user unless marked [AllowAnonymous].

Follow-up question: Where do the user’s permissions come from? Should we put all permissions inside the JWT?

Follow-up answer:

  • Putting every permission in the token makes it big, and changes only apply when a new token is issued.
  • Common way: the token carries identity and a few stable claims (user id, tenant, roles). The API loads detailed permissions from its own store and caches them for a short time.
  • In .NET 8+, IAuthorizationRequirementData and custom policy providers can build policies from names like perm:invoices.approve dynamically.

Red flag: Only knows [Authorize(Roles = ...)] and puts all business permission logic inside controllers with if statements.

Report a problem or send a better text

Edit on GitHub

56Over-posting and mass assignmentMedium–SecurityMinimal APIs and controllers

Question: This endpoint lets users update their own profile. A security tester changed their account to admin and gave themselves a big credit, using only this endpoint.

public class User
{
    public int Id { get; set; }
    public string Name { get; set; } = "";
    public bool IsAdmin { get; set; }
    public decimal Credit { get; set; }
}

[HttpPut("me")]
public async Task<IActionResult> Update(User input)
{
    input.Id = CurrentUserId;
    _db.Users.Update(input);
    await _db.SaveChangesAsync();
    return NoContent();
}

How did they do it? What is over-posting (mass assignment)? How do you prevent it?

Short answer: Model binding fills every property of User that is in the JSON. The tester just sent {"name":"x","isAdmin":true,"credit":99999}. Because the code binds the request directly to the entity and saves all fields, they changed fields they should never touch. This is over-posting (also called mass assignment). The fix: bind to a DTO that has only the fields the user may change, then copy those fields to the entity on purpose.

Hint (if the candidate says there is no problem): The UI form only has a “Name” field. Does that protect the other fields? What does the API actually accept?

Why it happens (step by step)

  1. The JSON serializer does not know which fields the UI shows. It sets any matching property.
  2. Update(input) marks all columns as modified, so IsAdmin and Credit are saved.
  3. Even if no one uses them today, a new property added to the entity later becomes writable too, without anyone noticing.
  4. The opposite bug: fields the client did not send become null or zero, and overwrite real data.

Solution: input DTOs

public record UpdateProfileRequest(
    [property: Required, MaxLength(100)] string Name);

[HttpPut("me")]
public async Task<IActionResult> Update(
    UpdateProfileRequest req, CancellationToken ct)
{
    var user = await _db.Users.FindAsync([CurrentUserId], ct);
    if (user is null) return NotFound();
    user.Name = req.Name;
    await _db.SaveChangesAsync(ct);
    return NoContent();
}
  • Load the entity from the database, change only allowed fields, save. EF Core updates only the changed columns.
  • Different actions get different DTOs (UpdateProfileRequest, AdminUpdateUserRequest).
  • The same idea for output: return a response DTO, so you do not leak fields like PasswordHash.
  • Weaker options: [Bind("Name")] (MVC forms only, easy to forget), [JsonIgnore] on the entity (mixes API concerns into the domain).

Related: IDOR

  • The fix above also takes the user id from the token (CurrentUserId), not from the request. If the id came from the URL or body, a user could change someone else’s profile (Insecure Direct Object Reference). Always check ownership.

Follow-up question: How do you support partial updates (PATCH) safely?

Follow-up answer:

  • Option 1: a DTO with nullable fields, and only apply non-null values. Simple, but you cannot “set to null” on purpose.
  • Option 2: JSON Patch (JsonPatchDocument<UpdateProfileRequest>) applied to the DTO, never to the entity. Then validate the DTO and copy fields.
  • Option 3: separate task-based endpoints (PUT /me/name, POST /me/email-change), which are often clearer and easier to secure.

Red flag: Binds requests directly to EF entities and relies on the UI to hide fields. Takes the owner id from the request body.

Report a problem or send a better text

Edit on GitHub

57Synchronous HTTP or async messaging? When to use a queueDesign–MicroservicesRabbitMQ

Question: When a customer places an order, the Order service calls four other services over HTTP, one after another, before it answers the customer:

POST /orders
  -> Inventory: reserve items      (120 ms)
  -> Payment:   charge card        (900 ms)
  -> Email:     send confirmation  (1.5 s)
  -> Loyalty:   add points         (300 ms)
<- 201 Created

When the Email service is down, nobody can place an order. On sale days, the order endpoint times out. Which calls should stay synchronous, and which should become async messages? When would you use a message queue?

Short answer: Use synchronous HTTP (or gRPC) when the caller needs the answer now to continue (is the item in stock? did the payment pass?). Use async messaging when the work can happen later and the caller does not need the result to answer the user (email, loyalty points, analytics). Here, Email and Loyalty should react to an OrderPlaced event. A queue gives you decoupling (Email down does not stop orders), load leveling (the queue absorbs peaks), and retries. The cost: eventual consistency, more moving parts, and harder debugging.

Hint (if the candidate says there is no problem): The availability of this endpoint is the product of all four services. If each one is up 99.5% of the time, how often does placing an order work?

Why the chain is fragile (step by step)

  1. Availability multiplies: 0.995⁴ ≈ 98%. More services in the chain → more failures.
  2. Latency adds up: the user waits for the slowest path, including the email server.
  3. Temporal coupling: all services must be up at the same time.
  4. Retries are unclear: if Loyalty fails after the card was charged, do we fail the order? Retry? Who remembers?

A better design

POST /orders
  -> Inventory: reserve (sync, needed now)
  -> Payment:   charge  (sync, or async + status page)
  save Order + OrderPlaced in one transaction (outbox)
<- 201 Created
OrderPlaced -> Email service   (async)
OrderPlaced -> Loyalty service (async)
  • The outbox makes sure the event is sent if, and only if, the order was saved.
  • Consumers must be idempotent, because messages can come twice.
  • Payment can also be async (return 202 Accepted + a status URL) if the provider is slow.

When to use a queue (checklist)

  • The work is slow or not needed for the response.
  • One event, many interested services (pub/sub).
  • Load comes in bursts, and the worker can process at its own speed.
  • You need reliable retries and a place for failed messages (dead-letter queue).

When not to: simple CRUD, a query that needs fresh data, or a small team where one more broker is more risk than help.

Follow-up question: What is the difference between a queue (like RabbitMQ) and an event log (like Kafka)? When do you pick each?

Follow-up answer:

  • Queue (RabbitMQ, Azure Service Bus): a message is delivered to one consumer of that queue and then removed. Good for commands and work distribution, routing rules, per-message ack, delay and dead-letter features.
  • Log (Kafka, Event Hubs): messages stay for a retention time. Many consumer groups read the same stream at their own position, and can replay history. Order is kept per partition. Good for event streams, high throughput, and analytics.
  • In .NET, libraries like MassTransit hide some differences, but the delivery model still shapes the design.

Red flag: Puts everything on a queue (“async is always better”), or makes every call synchronous. Cannot name the costs of eventual consistency.

Report a problem or send a better text

Edit on GitHub

58A message that keeps failing (poison message)Medium to hard–Resilience with PollyMassTransitRabbitMQ

Question: A worker reads InvoiceCreated messages from a queue. One message has a bad date format. The consumer throws, the message goes back to the queue, and the worker picks it up again, forever. CPU is at 100%, logs grow by 2 GB per hour, and thousands of good messages wait behind it.

public async Task Consume(ConsumeContext<InvoiceCreated> ctx)
{
    var date = DateTime.Parse(ctx.Message.IssuedAt);
    await _invoices.SaveAsync(ctx.Message, date);
}

How do you handle a message that keeps failing?

Short answer: Separate transient failures (DB timeout, network: retry may help) from permanent failures (bad data, a bug: retry never helps). Retry transient ones a limited number of times with backoff. After that, or right away for permanent errors, move the message to a dead-letter queue (DLQ) (in MassTransit, the _error queue), with the error details. Then alert, investigate, fix, and replay from the DLQ. Never retry forever.

Hint (if the candidate says there is no problem): If we retry 5 times immediately, the DB timeout during a deploy still fails all 5 times in one second. And a parse error fails 5 times too. What is wrong with both?

Step by step

  1. Classify errors. FormatException, validation errors → permanent. TimeoutException, DbUpdateException for deadlocks, HTTP 503 → transient.
  2. Immediate retries: a few quick tries for short glitches.
  3. Delayed retries (redelivery): wait longer between tries (for example 1 min, 5 min, 30 min), so a down dependency has time to come back.
  4. Dead-letter: after the limit, move the message with headers: exception type, message, stack, retry count, original queue.
  5. Alert on DLQ size (any message in DLQ should page or create a ticket, depending on the business).
  6. Fix and replay: fix the code or data, then move messages from DLQ back to the main queue. Consumers must be idempotent so replay is safe.

MassTransit example

x.AddConsumer<InvoiceCreatedConsumer>(c =>
{
    c.UseMessageRetry(r =>
    {
        r.Ignore<FormatException>();
        r.Exponential(5, TimeSpan.FromSeconds(1),
            TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(5));
    });
});
// After retries, the message moves to the
// "<queue>_error" queue with fault headers.
  • Redelivery with longer delays (UseDelayedRedelivery) needs broker support for delayed messages.
  • In Kafka there is no built-in DLQ: you write the failed record to a separate “dead letter” topic yourself, and commit the offset, so the partition is not blocked.

Also important

  • Validate messages at the producer too, and use a schema (contract tests, schema registry).
  • Keep the ordering impact in mind: skipping a message to the DLQ may break order for that key.
  • Log once per failure with the message id, not the whole payload every retry (privacy and log volume).

Follow-up question: Retrying is safe only if the consumer is idempotent. How do you make this consumer idempotent?

Follow-up answer:

  • Use a business key: the invoice id. Save with a unique constraint, or “insert if not exists”, so a second save does nothing.
  • Or keep a table of processed message ids (inbox pattern), written in the same transaction as the business change.
  • Side effects outside the DB (sending an email) need their own key or an outbox, so they are not repeated.

Red flag: Retries forever with no delay, or catches all exceptions and ignores them (“so the queue does not get stuck”), which silently loses data.

Report a problem or send a better text

Edit on GitHub

59Testing strategy for a .NET API: what makes a test valuableMedium–Unit tests with xUnitIntegration tests

Question: Our API has 1,800 unit tests and 92% code coverage. Still, last month three bugs reached production: a wrong SQL filter, a broken JSON field name, and a missing DI registration. Many tests look like this:

[Fact]
public async Task Create_calls_repository()
{
    var repo = new Mock<IOrderRepository>();
    var sut = new OrderService(repo.Object);
    await sut.CreateAsync(new CreateOrder(...));
    repo.Verify(r => r.AddAsync(It.IsAny<Order>()),
        Times.Once);
}

Every refactor breaks hundreds of tests. What is your testing strategy for a .NET API? What makes a test valuable, and not just one more in the count?

Short answer: A valuable test catches real bugs, does not break on safe refactors, runs fast enough, and tells you clearly what broke. Tests that only check “method X was called” test the implementation, not the behavior: they break on refactors and miss bugs in SQL, JSON, DI and configuration. A good strategy is a mix: unit tests for pure business logic, integration tests with a real database and the real HTTP pipeline for most API behavior (WebApplicationFactory + Testcontainers), a few end-to-end tests for key journeys, and contract tests between services. Coverage is a tool to find untested code, not a goal.

Hint (if the candidate says there is no problem): None of the three bugs could be caught by a test that mocks the repository and never runs SQL, never serializes JSON, and never builds the DI container. Which kind of test would have caught each one?

What makes a test valuable (step by step)

  1. Tests behavior through a public surface: “POST /orders with an empty cart returns 400”, not “calls AddAsync once”.
  2. Few mocks: mock only what you do not own or what is slow and outside (payment provider, email). Use the real DB in a container.
  3. Fails for one clear reason and has a clear name.
  4. Deterministic: no real clock, random or shared state; inject TimeProvider.
  5. Survives refactors: changing internal classes should not break it.

A practical mix

Layer What Tools
Unit Domain rules, calculations, validators xUnit, no mocks or few
Integration (most value) Endpoint → handler → DB, JSON, auth, DI WebApplicationFactory, Testcontainers
Contract API/message shape between teams Pact, schema checks
E2E A few critical user journeys Playwright

This is sometimes called the testing trophy or honeycomb: more weight on integration tests for typical API services, instead of a pyramid that is mostly unit tests.

How each bug is caught

  • Wrong SQL filter → integration test against a real database.
  • Broken JSON name → API test that checks the response body, or a contract test.
  • Missing DI registration → any test that starts the app with WebApplicationFactory.

Follow-up question: How do you use code coverage and mutation testing well?

Follow-up answer:

  • Coverage shows lines that ran, not that they were checked. 92% coverage with weak asserts means little. Use it to find untested risky areas, not as a target (Goodhart’s law).
  • Mutation testing (Stryker.NET) changes the code a little (> to >=) and checks if a test fails. Surviving mutants show tests that run code but do not really test it.
  • Run mutation tests on core domain code, not the whole solution: they are slow.

Red flag: Measures quality only by test count or coverage. Mocks everything, including EF Core, and thinks the database layer is tested.

Report a problem or send a better text

Edit on GitHub

60Integration tests that depend on a databaseMedium–Integration testsEntity Framework Core

Question: Our integration tests use EF Core’s InMemory provider. They all pass. In production, a query with a unique index violation, a transaction, and a ToLower() comparison behave differently. Another team uses one shared SQL Server test database for everyone, and tests fail randomly when two builds run at the same time. How do you write integration tests that depend on a database?

Short answer: Test against the same database engine as production, in a disposable, isolated instance. In .NET the common way is Testcontainers (start a real SQL Server or PostgreSQL in Docker for the test run) plus WebApplicationFactory (start the real API in memory and call it over HTTP). Keep tests independent: reset data between tests (Respawn, or a transaction per test), and never share one database between parallel builds. The InMemory provider is not a relational database: no constraints, no transactions, no SQL translation, so it hides real bugs.

Hint (if the candidate says there is no problem): With InMemory, inserting two users with the same email works. In production it throws. And EF.Functions.Like cannot be tested at all. Why?

Setup (step by step)

  1. Start a container once for the test class or the whole run:
public class ApiFactory : WebApplicationFactory<Program>,
    IAsyncLifetime
{
    private readonly MsSqlContainer _db =
        new MsSqlBuilder().Build();

    protected override void ConfigureWebHost(
        IWebHostBuilder b) =>
        b.UseSetting("ConnectionStrings:Db",
            _db.GetConnectionString());

    public async Task InitializeAsync()
    {
        await _db.StartAsync();
        using var scope = Services.CreateScope();
        await scope.ServiceProvider
            .GetRequiredService<AppDbContext>()
            .Database.MigrateAsync();
    }

    async Task IAsyncLifetime.DisposeAsync() =>
        await _db.DisposeAsync();
}
  1. Apply the real migrations (not EnsureCreated), so migrations are tested too.
  2. Each test arranges its own data, calls the API with factory.CreateClient(), and checks the HTTP response and the database state.
  3. Reset between tests: Respawn deletes data from all tables fast, keeping the schema. Or give each test class its own database name.
  4. Replace only outside systems (payment, email) with fakes in ConfigureTestServices.

Speed tips

  • Start one container per run (xUnit collection fixture), not per test.
  • Testcontainers can reuse containers locally.
  • Keep a clear split: fast unit tests on every save, integration tests on every PR in CI (CI agents need Docker).

Alternatives

  • SQLite in-memory is closer than InMemory (it is relational), but SQL dialect and behavior still differ from SQL Server or PostgreSQL.
  • A shared test database: only if each test runs inside a transaction that is rolled back, and builds never run in parallel. Fragile.

Follow-up question: A test is “flaky”: it fails 1 time in 20 on CI. How do you handle it?

Follow-up answer:

  • Do not just re-run it. A flaky test trains the team to ignore red builds.
  • Common causes: shared data between tests, test order, real time (DateTime.Now), async code without awaiting, timeouts on slow CI agents, parallel tests on one DB.
  • Fix the cause: isolate data, inject TimeProvider, wait for conditions instead of Task.Delay.
  • If you cannot fix it now, quarantine it with a ticket and an owner, and fix it soon. Never just delete it.

Red flag: Uses EF Core InMemory and calls it an integration test. Or shares one long-lived test database across all builds.

Report a problem or send a better text

Edit on GitHub

61What is structured logging and why does it matter?Easy–Structured logging

Question: A customer says their order 98123 failed yesterday. Our logs are in a central log system, and the code logs like this:

_logger.LogInformation(
    "Order " + order.Id + " failed for user "
    + user.Email + " after " + ms + " ms");

It takes 40 minutes of text search to find the right lines, and we cannot answer “how many orders failed per hour last week?” What is structured logging, and how would you change this code?

Short answer: Structured logging means each log entry is data with named fields, not just a sentence. With a message template, LogInformation("Order {OrderId} failed after {ElapsedMs} ms", id, ms), the log system stores OrderId=98123 and ElapsedMs=532 as separate fields. Then you can filter and aggregate: OrderId = 98123, or count failures per hour. String concatenation loses the fields, makes a new string even when the level is off, and here also leaks personal data (email).

Hint (if the candidate says there is no problem): Try to write a query for “all failed orders that took more than 2 seconds” on these text logs. What do you need to parse?

How to do it (step by step)

  1. Use message templates, never string interpolation or +:
_logger.LogWarning(
    "Order {OrderId} failed after {ElapsedMs} ms",
    order.Id, ms);
  1. Write JSON to the console (or use Serilog / OpenTelemetry) so the log system gets the fields: builder.Logging.AddJsonConsole();
  2. Add context once with scopes, not in every message:
using (_logger.BeginScope(new Dictionary<string, object>
    { ["OrderId"] = order.Id, ["TenantId"] = tenant }))
{
    // every log in here has OrderId and TenantId
}
  1. Correlate: include the trace id (Activity.Current.TraceId) in every log, so logs from many services for one request can be found together. OpenTelemetry does this automatically.
  2. Pick the right level: Information for business events, Warning for handled problems, Error for failures that need action, Debug off in production.
  3. No secrets or personal data (passwords, tokens, full emails, card numbers). Log the user id instead.

Performance

  • With templates, arguments are not formatted if the level is disabled.
  • For hot paths, use the [LoggerMessage] source generator: no boxing, no parsing of the template at run time.
[LoggerMessage(Level = LogLevel.Warning,
    Message = "Order {OrderId} failed after {ElapsedMs} ms")]
static partial void OrderFailed(
    ILogger logger, int orderId, long elapsedMs);

Follow-up question: Logs, metrics and traces: what is the difference, and when do you use each?

Follow-up answer:

  • Logs: detailed events (“order 98123 failed: card declined”). Good for the “why” of one case. Expensive at high volume.
  • Metrics: numbers over time (requests per second, error rate, p99 latency). Cheap, good for dashboards and alerts.
  • Traces: the path of one request across services, with timing per step. Good for “where is the time spent?”
  • They work best linked together: an alert from a metric → a slow trace → the logs of that trace id.

Red flag: Uses string interpolation in log calls and thinks text search is enough. Logs tokens or personal data.

Report a problem or send a better text

Edit on GitHub

62How do you structure a .NET project?Design–Clean ArchitectureVertical Slice

Question: We are starting a new API for a clinic booking system. A teammate proposes this solution, “like the template we saw online”:

Clinic.Domain
Clinic.Domain.Shared
Clinic.Application
Clinic.Application.Contracts
Clinic.Infrastructure
Clinic.Infrastructure.Persistence
Clinic.Api
Clinic.Common

Adding one field to a booking needs changes in 7 files across 5 projects. The team has 3 developers. How would you structure this .NET project? What are the options, and how do you choose?

Short answer: Structure should follow how the code changes and how big the team and domain are, not a template. Two popular styles: Clean (layered) architecture groups code by technical layer (Domain, Application, Infrastructure, Api) with dependencies pointing inward. Vertical slice architecture groups code by feature (Bookings/Create, Bookings/Cancel), each slice holding its endpoint, handler, validation and data access. For a small team, start with few projects (often one API project plus a test project), organize folders by feature, and keep the domain logic free of infrastructure. Split into more projects only when there is a real reason (enforced boundaries, reuse, separate deployment).

Hint (if the candidate says there is no problem): Most changes are “a new feature” or “change one feature”. In this structure, how many places does a new developer open to understand how “cancel booking” works?

The options

Layered / Clean Vertical slice
Group by Technical layer Feature / use case
One change touches Many layers and projects Mostly one folder
Strong at Clear dependency rules, rich shared domain Fast feature work, low coupling between features
Risk Ceremony, pass-through layers Duplicate code between slices if not careful

A practical structure for this team (step by step)

src/Clinic.Api/
  Features/
    Bookings/
      CreateBooking.cs   // endpoint + request + handler
      CancelBooking.cs
    Doctors/
  Domain/                // Booking, Doctor, rules
  Infrastructure/        // DbContext, email, clock
  Program.cs
tests/Clinic.Api.Tests/
  1. Feature folders: everything for one use case is together, so it is easy to find, change and delete.
  2. Domain stays pure: business rules in Domain, with no reference to EF Core or HTTP.
  3. Boundaries with tests, not only projects: architecture tests (NetArchTest, ArchUnitNET) can check “Domain does not use Infrastructure”.
  4. Grow when needed: split a separate Domain project when the rules get rich, or a separate module when a part becomes its own bounded context (a modular monolith).
  5. Shared code carefully: a Common project often becomes a dumping ground. Share only what is truly common.

Follow-up question: What is a modular monolith, and how is it different from microservices?

Follow-up answer:

  • One deployable app, split inside into modules by business area (Bookings, Billing, Patients). Each module has its own data (own schema or tables) and a small public API; other modules cannot touch its internals.
  • Compared to microservices: no network calls, one deployment, simple transactions and debugging. But module boundaries still let you split a module into a service later, if scale or team size needs it.
  • It is a common, safe default for a new product with a small or medium team.

Red flag: Copies a big template “because it is best practice” without asking about team size or the domain. Or puts everything in one folder with no boundaries at all.

Report a problem or send a better text

Edit on GitHub

63Each SOLID principle with a real .NET exampleMedium–SOLID

Question: You see this class in code review. It is 1,400 lines long, and every sprint someone changes it.

public class InvoiceService
{
    public void Create(Invoice inv)
    {
        // validate, calculate tax by country (switch)
        // save with new SqlConnection(...)
        // build PDF, send email with new SmtpClient(...)
    }
}

Explain each SOLID principle with a real .NET example. Which principles does this class break, and how would you improve it?

Short answer: SOLID is five guidelines that keep code easy to change. This class breaks most of them: it has many reasons to change (SRP), every new country edits the switch (OCP), and it creates its dependencies with new, so it cannot be tested or swapped (DIP). Fix it by splitting responsibilities, depending on small interfaces, and injecting them with DI. But SOLID is a tool, not a rule to apply everywhere: use it where change actually happens.

Hint (if the candidate says there is no problem): The finance team changes the tax rules, the design team changes the PDF, and IT moves email to a new provider. All three edit the same file in the same week. What happens?

The five principles, with .NET examples

  1. S: Single Responsibility. A class should have one reason to change (one “actor” who asks for changes). Here: tax rules, persistence, PDF and email are four reasons. Split into TaxCalculator, InvoiceRepository, InvoicePdfBuilder, IEmailSender.

  2. O: Open/Closed. Open for extension, closed for modification. Add a new country without editing old code:

public interface ITaxRule
{
    bool AppliesTo(string country);
    decimal Calculate(Invoice inv);
}
// register many: services.AddScoped<ITaxRule, DeTaxRule>();
// use IEnumerable<ITaxRule> to pick one

ASP.NET Core’s middleware pipeline is a real example: you add behavior without changing the framework.

  1. L: Liskov Substitution. A subtype must work wherever the base type is used, without surprises. Classic .NET example: ReadOnlyCollection<T> implements ICollection<T> but throws NotSupportedException on Add. Code that expects ICollection<T> breaks. Better: accept IReadOnlyCollection<T> when you only read.

  2. I: Interface Segregation. Clients should not depend on methods they do not use. .NET itself split read and write: IReadOnlyList<T> vs IList<T>. In our code: a report job needs IInvoiceReader, not a 25-method IInvoiceRepository.

  3. D: Dependency Inversion. High-level policy depends on abstractions, not on details. InvoiceService depends on IEmailSender; the SMTP or SendGrid class is a detail registered in DI. Now we can test with a fake and change provider in one place.

The improved shape

public class CreateInvoiceHandler(
    TaxCalculator tax, IInvoiceRepository repo,
    IInvoicePdfBuilder pdf, IEmailSender email)
{
    public async Task Handle(Invoice inv, CancellationToken ct)
    {
        inv.ApplyTax(tax.For(inv));
        await repo.AddAsync(inv, ct);
        var file = pdf.Build(inv);
        await email.SendAsync(inv.CustomerEmail, file, ct);
    }
}

Follow-up question: Can SOLID be overdone? Give an example.

Follow-up answer:

  • Yes. An interface for every class even with one implementation and no test need, ten tiny classes for a 20-line feature, or a “strategy” for a rule that never changes.
  • Cost: more files to read, harder navigation, and abstraction that guesses future needs wrong (YAGNI).
  • Rule of thumb: introduce the abstraction when the second real variation appears, or when you need the seam for testing.

Red flag: Recites the five names and textbook definitions (Shape/Rectangle) but cannot point to one problem in real code. Or applies them everywhere, making a simple feature 15 classes.

Report a problem or send a better text

Edit on GitHub

64Design patterns: system design impact and team anti-patternsMedium to hard–Design patternsSOLID

Question: A new developer wants to show good design. For a feature “send a welcome SMS after sign-up”, their pull request adds:

ISmsSenderFactory, SmsSenderFactory
ISmsStrategy, KavenegarSmsStrategy
SmsSenderBuilder
SmsServiceSingleton
ISmsRepository, GenericRepository<T>
SmsCommand, SmsCommandHandler, SmsMediatorPipeline

There is only one SMS provider and no plan for another. How do SOLID and design patterns influence system design decisions? Which anti-patterns do you watch for when teams use patterns?

Short answer: Patterns are named solutions to recurring problems. They help when the problem is really there, and they hurt when they are added “just in case”. At system level, the same ideas show up as boundaries: DIP becomes ports and adapters, Strategy becomes pluggable providers, Observer becomes events and pub/sub, Decorator becomes middleware and proxies. Watch for: pattern-first design (choosing patterns before understanding the problem), speculative generality, the Singleton as a global variable, generic repository on top of EF Core, anemic layers that only pass calls, and mediator everywhere hiding the flow. This PR needs one interface ISmsSender and one class.

Hint (if the candidate says there is no problem): A bug in the welcome SMS takes a new teammate 2 hours to trace, through 9 files, to find the one line that calls the provider. What did all these patterns buy us?

How SOLID and patterns shape system design (step by step)

  1. DIP → ports and adapters: the domain defines IPaymentGateway; Stripe or a local bank are adapters. Changing provider becomes a deployment choice, not a rewrite.
  2. SRP → service and module boundaries: “one reason to change” at system level is “one team / one business capability” per service or module.
  3. OCP → plugin points: new payment methods, new export formats, new tax rules as new classes registered in DI, or new consumers of an event.
  4. Observer → events: instead of the Order service calling Email directly, it publishes OrderPlaced, and others subscribe.
  5. Decorator → cross-cutting concerns: retries (Polly), caching, logging around a client, or middleware in the pipeline.

Anti-patterns to watch for

Anti-pattern Sign Better
Pattern-first “Let’s use Factory here” before a second case exists Start simple, refactor to a pattern when needed
Singleton as global state Static instance with mutable data DI with a clear lifetime
Generic repository on EF Core IRepository<T> with GetAll() Use DbContext, or specific query methods
Mediator everywhere Every call goes through Send(), “find usages” is useless Direct calls inside a feature; mediator only where pipeline behavior helps
Lasagna layers Each layer only forwards to the next Remove layers that add no rule
Golden hammer One pattern for all problems Choose by the problem

How to review it: ask “what change does this pattern make easier, and is that change likely?” If the answer is “maybe one day”, remove it.

Follow-up question: When is it right to add an abstraction early, before a second implementation exists?

Follow-up answer:

  • When it is a boundary to the outside world that you need to fake in tests (clock, payment provider, email, file storage).
  • When the cost of changing later is very high, like a public API contract or a message format other teams depend on.
  • When the platform expects it (registering an IHostedService, an IHealthCheck).
  • Otherwise, keep the concrete class; extracting an interface later with an IDE takes minutes.

Red flag: Measures design quality by the number of patterns used. Cannot explain what problem each pattern in their code solves.

Report a problem or send a better text

Edit on GitHub

65Exceptions or result objects for error handling?Medium–Clean CodeMinimal APIs and controllers

Question: In our transfer service, every business problem is an exception:

public void Transfer(int from, int to, decimal amount)
{
    var a = _repo.Get(from)
        ?? throw new NotFoundException("Account");
    if (a.Balance < amount)
        throw new InsufficientFundsException();
    ...
}

A middleware maps exception types to HTTP codes. Under a promo campaign, 30% of transfers fail with “insufficient funds”, CPU goes up, and the error dashboard is full of “errors” that are normal user mistakes. A teammate wants to replace everything with a Result<T> type. When do you use exceptions, and when result objects?

Short answer: Use exceptions for exceptional, unexpected situations that the caller usually cannot handle right there: the database is down, a bug, broken config. Use result objects (or similar return values) for expected business outcomes that the caller must handle: insufficient funds, validation errors, “not found” in a lookup. Exceptions for normal flow are slow (stack trace capture), hidden (the signature does not show them), and pollute monitoring. But result objects everywhere make code noisy. A mix is common: results at the domain/application level, exceptions for infrastructure failures, and one place that maps both to ProblemDetails.

Hint (if the candidate says there is no problem): The on-call engineer got paged at 3 AM for an “error spike”. It was customers with empty wallets. What is wrong with treating this as an error?

Why exceptions for expected outcomes hurt (step by step)

  1. Cost: throwing captures a stack trace and unwinds frames. Cheap once, expensive at thousands per second (it is faster in .NET 9, but still not free).
  2. Invisible contract: Transfer returns void; nothing tells the caller which failures to handle. With a result, the compiler and the reader see it.
  3. Monitoring noise: real failures hide among expected ones.
  4. Control flow by catch is hard to read: a throw deep inside can jump far away.

A simple result type

public record Error(string Code, string Message);

public readonly record struct Result<T>(T? Value, Error? Error)
{
    public bool IsSuccess => Error is null;
    public static Result<T> Ok(T v) => new(v, null);
    public static Result<T> Fail(Error e) => new(default, e);
}
public async Task<Result<Receipt>> TransferAsync(...)
{
    var a = await _repo.GetAsync(from, ct);
    if (a is null) return Result<Receipt>.Fail(Errors.NotFound);
    if (a.Balance < amount)
        return Result<Receipt>.Fail(Errors.Insufficient);
    ...
}

// endpoint
return result.IsSuccess
    ? Results.Ok(result.Value)
    : Results.Problem(statusCode: 422,
        title: result.Error!.Code);

Keep exceptions for

  • Infrastructure failures (DB timeout, network). Let them bubble up to a global handler (IExceptionHandler in .NET 8+) that logs once and returns a 500 ProblemDetails.
  • Bugs and broken invariants (ArgumentNullException, InvalidOperationException): fail fast.
  • Constructors and places where a return value is not possible.

Follow-up question: What is the risk with result objects, and how do you avoid it?

Follow-up answer:

  • Ignored results: the caller forgets to check IsSuccess and uses Value. Make Value throw if failed, or use Match methods, or an analyzer.
  • Wrapping everything: returning Result from every tiny method spreads if (!r.IsSuccess) return r.Error everywhere. Use it at meaningful boundaries (use cases), not in every helper.
  • Swallowing real exceptions into results (catch (Exception) { return Fail(...) }) loses stack traces. Only convert expected exceptions.

Red flag: Uses exceptions for all validation and business rules and sees no cost. Or catches Exception everywhere and returns false.

Report a problem or send a better text

Edit on GitHub

66Async everywhere, but no throughput gainHard–async/await and the thread poolPerformance and Span

Question: The team spent two months converting the whole API to async/await, from controllers down to the database calls. There is no .Result or .Wait() left. Load tests show the same throughput as before: about 400 requests per second, and then latency climbs. The CPU is at 35%. How do you investigate why async gave no gain?

Short answer: Async does not make work faster; it frees threads while waiting for I/O. It helps throughput only when threads were the bottleneck. If the real limit is somewhere else, async cannot help: the database (connection pool size, locks, slow queries), a downstream service or its rate limit, a lock or SemaphoreSlim in our code, CPU-bound work on a few hot requests, an outgoing HTTP connection limit, or the load test client itself. Find the bottleneck with measurements before changing more code.

Hint (if the candidate says there is no problem): At 400 requests per second, the counters show 100 active DB connections and many requests waiting in “Timeout expired… obtaining a connection from the pool”. What is the limit now?

How to investigate (step by step)

  1. Check the load test itself: is the client machine maxed out? Does it use enough connections? A weak client looks like a slow server.
  2. Look at the runtime counters: dotnet-counters monitor for thread pool queue length, thread count, GC % time, lock contention, and active requests. A small queue means threads are not the problem.
  3. Look at traces (OpenTelemetry): where does a typical request spend time? DB? An HTTP call? Waiting for a semaphore?
  4. Database: connection pool (default Max Pool Size is 100 in SqlClient), wait stats, blocking, slow query plans. If every request holds a connection for 250 ms, 100 connections ≈ 400 requests per second. That is our number.
  5. Shared locks in code: a lock or a SemaphoreSlim(1) around a cache refresh, a static Dictionary with a lock, logging to a slow sink synchronously.
  6. Downstream limits: another service with its own limit, or an HttpClient with MaxConnectionsPerServer set low.
  7. CPU-bound parts: big JSON serialization, regex, encryption. Async does not help CPU work; it needs less work or more cores.

Common fixes, by bottleneck

Bottleneck Fix
DB connection pool Shorter connection hold time (open late, close early), faster queries, caching, read replicas. Raising the pool size helps only if the DB can handle it
Lock in code Remove shared state, use ConcurrentDictionary, or make the critical section tiny
Downstream service Caching, batching, async messaging, ask for a higher limit
CPU Profile with dotnet-trace; reduce allocations; scale out

Also check async mistakes that remove the gain

  • Task.Run wrapping sync code in ASP.NET (“fake async”): it just moves the blocking to another pool thread.
  • Sequential awaits that could be parallel (await a; await b; when they are independent → Task.WhenAll).
  • Sync I/O hidden in libraries (sync file or stream reads, Stream.Read in a custom formatter).

Follow-up question: We have 10 independent calls to other services per request. Should we just use Task.WhenAll on all of them?

Follow-up answer:

  • It lowers latency for one request: the time becomes the slowest call, not the sum.
  • But it multiplies load on downstream services at peak: 400 rps × 10 parallel calls. It can hit their limits or the connection pool faster.
  • Limit concurrency (Parallel.ForEachAsync with MaxDegreeOfParallelism, or SemaphoreSlim), add timeouts and cancellation, and think about partial failure: what if one of the ten fails?

Red flag: Expects async to make each request faster, or answers “add more async” or “increase the thread pool” without measuring where the time goes.

Report a problem or send a better text

Edit on GitHub