# Improper Instantiation: Building Shared Objects Again and Again Hurts Systems

> Learn why rebuilding an expensive shared object on every request exhausts resources, how to share it safely, and how to spot the problem under load.

**In short:** Improper instantiation is rebuilding an expensive shared object on every request, which exhausts resources under load, and the fix is to share one safely.

From "System design", a free course by Venkat R. Murty: https://designervenkat.online/learn/system-design/improper-instantiation

## What You Will Learn

Some parts of a system are costly to build. A web client, a database link, or a service client may open network connections and read settings before it can send even one request. Designers often mean these parts to be built once and then shared. Improper Instantiation is the mistake of building them again and again instead. This lesson explains what the mistake looks like, why it hurts, how to fix it, and how to spot it when a live system starts to slow down or fail.

- **Object:** A bundle of code and data that works as one thing, such as a client that sends web requests.
- **Instance:** One real copy of an object, built in memory from its class (the blueprint). Creating an instance means building that copy.
- **Antipattern:** A common way of solving a problem that usually works badly and can make things worse.
- **Socket:** A network endpoint that a program uses to send and receive data. Each open connection uses one.
- **Connection pool:** A set of open connections kept ready, so new work can borrow one instead of opening a new one.
- **Singleton:** A design in which only one instance of a class exists, and all the code shares it.
- **Thread-safe:** Safe to use from several threads (lanes of work running at the same time) without mixing up data.
- **Latency:** The time one request takes, from the moment it starts until the answer arrives.

## What Does Improper Instantiation Mean?

Improper Instantiation happens when code keeps creating new instances of an object that was meant to be created once and then shared. Microsoft's Azure Architecture Center, a well-known set of cloud design guidance, calls this an improper instantiation antipattern and says it can hurt performance. The guide's examples include the HttpClient class in .NET, which sends web requests, and clients for Redis and Azure Cosmos DB, which are database services.

The key word is shareable. The mistake only matters when the object is safe to share. If an object cannot be used by several parts of a program at once, building a fresh copy may be the right choice, and this antipattern does not apply.

### A Simple Way to Picture It

Imagine a team that has one printer in the hall. Every time someone needs to print, they buy a new printer, set it up, print one page, and throw the printer away. The page prints, but the setup cost is wasted every time, and the store runs out of printers. A shared printer that everyone uses would do the same job for far less effort. Software often makes the same mistake with clients and connections.

## What Does It Look Like?

The usual form of the mistake is a request handler. A request handler is the function that runs each time a user's request arrives. When the handler builds a client inside itself, every user request pays the full setup cost. In C#, HttpClient is one example, where a line such as creating a new client inside the function sends a fresh connection out on every call.

### The Pattern in Python

Python is used below to show the idea, because it runs with no network. The class ExpensiveClient stands in for a costly client. Each time it is built, a counter goes up, so we can see how many builds happen. The first loop builds a new client for every call. The second part builds one client and reuses it.

```python
class ExpensiveClient:
    created = 0

    def __init__(self):
        ExpensiveClient.created += 1

    def fetch(self, name):
        return "order " + name


def per_request(name):
    client = ExpensiveClient()
    return client.fetch(name)


for name in ["a", "b", "c"]:
    per_request(name)
print("instances after per-request calls:", ExpensiveClient.created)

shared = ExpensiveClient()
for name in ["a", "b", "c"]:
    shared.fetch(name)
print("instances after shared use:", ExpensiveClient.created)

```

Output:

```text
instances after per-request calls: 3
instances after shared use: 4

```

The three per-request calls built three objects. The shared version built only one object in total, and the three later calls built none. The code did the same fetch work in both cases, but the shared version skipped the setup three times.

## Why Does It Hurt?

The damage grows with traffic. A little extra setup per call is not noticeable on a laptop, but a busy server may handle thousands of requests in a short time. Each of those requests repeats the same work, and the work piles up.

- **Wasted setup work:** Each new object repeats its startup steps, such as reading settings, preparing its internal parts, and opening links to the remote service. The Azure guide notes that when setup is slow, the cost of creating each instance can hurt scaling, even when no socket is involved.
- **Socket exhaustion:** A socket is a limited resource. When a client is thrown away, its connections can stay behind for a while. One description says each socket lingers in a state called TIME_WAIT after disposal, which is a short holding period that keeps the port reserved. Many clients created in a short time can use up the available sockets.
- **Latency that grows with traffic:** Each request waits for its own setup and its own new connection before any real work starts. As load rises, response times slow. The result is higher latency, which means slower answers.
- **Crashes and failed calls:** When the system runs out of socket resources, it cannot open new network connections. Calls then fail with socket errors, and in the worst case the app slows so much that users give up or the service stops.

Memory can suffer too. Creating and dropping many objects means more memory use and more work for the garbage collector, the part of the runtime that cleans up unused memory. This makes the slowdown even worse.

## How Do You Fix It?

The fix has one rule: create the object once, then reuse it for many requests. Microsoft's guidance for HttpClient says that to avoid port exhaustion, you should reuse the client for as many requests as possible. Other shared objects follow the same idea, even though the details differ.

### What Are Ways to Share One Object?

There are three common ways to share an object safely. Pick the one that fits your framework and how long your program runs.

- **Singleton or static shared instance:** You build the object once when the program starts and keep it in a static field, which is a value that belongs to the class rather than to one call. Microsoft's guidance for modern .NET suggests a static or singleton HttpClient, with a setting that recycles its connections on a schedule.
- **Let the framework manage it:** Dependency injection means a framework builds objects and hands them to the code that needs them. With IHttpClientFactory, clients are meant to be short-lived, and the factory pools the handlers underneath. The framework's factory for HttpClient manages the handlers underneath, so reuse happens without manual work.
- **Connection pool:** A connection pool keeps a set of open connections ready. Clients borrow one when they need it and return it when they finish. Many database and HTTP libraries do this for you, so your code only needs to keep the client object itself alive.

### Sharing Safely and Keeping Lifetimes Right

A shared object is used by many requests at once, so it must be thread-safe. Some libraries document that their objects support concurrent use, and others do not. Check the documentation before you share an object. If the object does not support it, a different design is needed.

Shared objects also need care with settings. If each request changes a property on a shared client, such as a header, requests can race, which means they interfere with each other. Set shared properties once at startup. If different parts of the app need different settings, create separate shared objects for each group.

Some resources must stay short-lived. A database connection is scarce, so holding one open when no work needs it can block other users. Share the client that manages connections, but borrow and release individual connections as you go.

> **Tip:** A shared client can keep old network addresses. Microsoft notes that HttpClient resolves a name only when it creates a connection. Set a lifetime for pooled connections, such as a few minutes, so connections are recycled and new addresses are picked up.

**Quick check:** A photo-sharing backend builds a new database connection inside the function that handles each 'like' tap. During a viral post, the API starts throwing 'too many connections' errors. What is the underlying problem, and what is the simplest fix?

A. The database disk is full, so the team should add storage to the database server.
B. Each tap sends many tiny requests, so the team should batch the taps into fewer calls.
C. Each tap builds a new connection, so connections pile up. Create one shared connection pool when the app starts and reuse it.
D. The API needs a cache for like counts, so the team should store the totals in memory.

**Answer:** C. This is Improper Instantiation: code rebuilds a shareable connection for every call. A single shared pool, built once, stops the pile-up. Batching and caching solve other problems, and extra disk space does not stop open connections from piling up.

## Worked Example: A Food Delivery Order Screen

This example is made up to show the steps. Imagine a food delivery app with an order-status API. When a customer opens the order screen, the API calls a payment provider's web service to check the payment. The API creates a new HTTP client for every call. Follow what happens at dinner time, when many customers open the screen in the same few minutes.

1. **Start with the code as written:** Each request handler creates a new HTTP client, sends one call to the payment provider, and then disposes of the client. Disposing means releasing the object's resources.
2. **Add peak traffic:** At dinner time, many order-status requests arrive at once. Each one builds a new client, which opens new connections to the payment provider.
3. **Watch the sockets run out:** Connections from the discarded clients linger after they close. The server's socket supply drops, and new calls find no free sockets.
4. **See the screen hang:** Calls that cannot open a connection fail or wait a long time. Customers see the order screen spin and never finish loading.
5. **Create one shared client:** When the app starts, it builds one HTTP client with a connection lifetime setting. Every handler uses that same client to call the payment provider, so connections are reused.
6. **Check the result:** Run the same peak-time load test before and after the change. In this example, the socket errors stop, and the order screen loads at normal speed.

> **Tip:** Keep the load test results from before and after the change. Without a before picture, it is hard to prove the fix worked.

## How to Spot It

Improper Instantiation can hide well. On a quiet day, the system may look fine. The clues appear under load, when many requests arrive together. Look for a set of symptoms that appear as traffic grows.

- **Latency rises with traffic:** Response times climb as the number of users grows, and they fall again when traffic drops.
- **Socket or connection errors:** Logs show messages about exhausted sockets, too many connections, or file handles running out. Microsoft's guide lists an increase in these exceptions as a key sign.
- **CPU looks normal:** Memory may also rise because of repeated object creation.

1. **Watch production for slowdowns:** Note the times when response times climb or the system fails for lack of resources.
2. **Read the telemetry at those times:** Telemetry is the data a system records about its work. Find the operations that create and destroy resource-using objects during those moments.
3. **Load test each suspect:** Run each suspected operation under heavy traffic in a test environment, not on live users.
4. **Review the code:** Check how each client or connection object is created and kept, and whether it is built inside a request handler.

### Reading Code for New-Instance Calls

Search the code for places where a client or connection is created. Pay special attention to lines inside request handlers, loops, and functions that run for every user action. A new client inside a loop is a strong warning sign, because it repeats the setup on each pass.

### Load Testing and Profiling

A load test sends traffic to the system in a safe place and records how it behaves. A profiler shows where time and resources go inside the program. Together they show the operation that creates the cost. Microsoft's guidance says that instrumentation and logging are key to finding the root cause, but you also need to know what to look for.

**Quick check:** A service's CPU use stays around one quarter, and memory is flat. As traffic doubles, response times climb, and the logs show socket errors. What should the engineer check first?

A. Whether a faster processor would lower the response times.
B. Whether a request handler creates a new network client on each call, and then load test that path.
C. Whether turning off logging would reduce the errors.
D. Whether doubling the size of the cache would stop the slowdown.

**Answer:** B. The symptoms point to repeated creation of network objects under load, not to a slow processor or a full cache. The next step is to read the request path and test it under load.

## Key Takeaways

Improper Instantiation means rebuilding an object that is meant to be built once and shared. It wastes setup time, uses up sockets and connections, and makes latency grow with traffic. The fix is to create the shared object once, using a singleton, a static shared instance, or a connection pool. Check that the object is thread-safe, keep settings fixed after startup, and keep scarce resources short-lived. To spot the problem, watch for rising latency and socket errors under load, read the code for new-instance calls in request handlers, and confirm with load tests and profiling.

## Sources

- [Improper Instantiation antipattern - Azure Architecture Center](https://learn.microsoft.com/en-us/azure/architecture/antipatterns/improper-instantiation/)
- [Performance testing and antipatterns - Azure Architecture Center](https://learn.microsoft.com/en-us/azure/architecture/antipatterns/)
- [HttpClient guidelines for .NET - Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines)
- [A New HttpClient on Every Call Exhausts TCP Ports. Reuse One Client, or Use IHttpClientFactory - DEV Community](https://dev.to/aimpexresources/a-new-httpclient-on-every-call-exhausts-tcp-ports-reuse-one-client-or-use-ihttpclientfactory-596g)
- [HTTP Resilience in .NET (Part 3): Connection Pooling & HttpClient Lifetime](https://devskillsunlock.com/blog/http-resilience-in-net-part-3-connection-pooling-httpclient-lifetime)
- [Correctly using HttpClient in .NET - Nelson Nobre](https://nelsonbn.com/blog/dotnet-httpclient-use-correctly/)
- [Top 10 Azure Functions Anti-Patterns - Tsuyoshi Ushio](https://tsuyoshiushio.medium.com/top-10-azure-functions-anti-patterns-690ccff04a58)
