Observation - Watch Out Boundaries
Tags: observation
Published: 2019-06-23 05:07:01

---
title: "Observation - Watch Out Boundaries"
published: true
description: "My first observation on the common production-error-prone patterns. The first is boundaries."
tags: Observation
canonical_url: https://www.thaianhduc.com/2019/06/observation-watch-out-boundaries/
---

*Original posted on [my blog](https://www.thaianhduc.com/2019/06/observation-watch-out-boundaries/) June 23, 2019*

When I first started my software development career I think writing software was hard. And it is true. However, the definition of writing software at that time was different. What I really meant was my code met the functional requirement (and it was not always true) and ran. That was when I did not see in production. So everything was working fine in my machine.

Over the time, I have had chances to bring my code into production and seen them running. No surprise that they did not work well in production. Every developer knows the famous "It works well in my machine". And it might work well in test. But it always has problems in production.

Why? There is no single answer for that problem. Software are developed by developers with different level of skills, experience, intelligent, ... Even the team has the most talented developers in the world, their products still have bugs. So I am not trying to find a solution for that problem. Instead, I embrace and observe the fact. There is no silver solution but there are tips and tricks to prevent as much as possible.

From my own experience, from Pluralsight courses, from youtube, ... from any source I touched, I want to document what I have observed. I do not intend to go in the detail of each item. Rather I want to have a list and some explanations, references. The detail varies project by project.

## SQL

If you build applications with SQL Server as data storage, watch out 2 common unbounded patterns

1. Select N+1
1. Select * without top n

Asking Google for "Select N+1" you will know what it is immediately. There are detail explanation with code example. Usually developers that have worked in ORM know it very well.

The second watch out is a bit tricky. That is when your applications issue a query to SQL in this pattern

``` SQL
SELECT * FROM dbo.Employee WHERE [Predicate]
```

In the test environment, there is no problem. However, in the production, the data is huge and that query might return millions of records.

These days many applications do not talk to the database directly. Instead there are ORM/LinqToSQL in between. And this piece of code is not uncommon

```CSharp
var employeesByName = ctx.Employees.Where(x => x.Name.StartWith("Smith")).ToList();
```

Some ORMs might put a limit on the generated queries. What developers need to do is to review all the generated queries.

The rule of thumb is that always control the number of returned records. Put the max on everything.

## Connections

There are many kinds of connections that applications make - connect to the database, connect to external services. When making such those calls, there are some watch out

1. Timeout: Make sure a timeout value is set on everything. Usually the modern frameworks have default values. Just make sure there is and you are aware of them.
1. Close connections properly. Just imagine what happens if you have your door opened? Bad things happen.

## JSON - Serialization and Deserialization

JSON is cool. Developers work with JSON everyday in one form or another. Many take it for granted. We rarely pay attention to the size of data. I once experienced such a problem [here - hidden cost of an architecture](https://www.thaianhduc.com/2018/10/hidden-cost-of-an-architecture/).
So if you have to explicitly use JSON directly in your code, ask these questions

1. Do I have to use it? Is there any other options?
1. What size? Is the size under controlled?

Some might argue that RAM is cheap. So why should we care too much about the size? Yes. RAM is cheap but it has limit. Once it reaches the limit, your application will freeze or crash. And if your applications are running on the cloud, everything your applications consume, there is cost involved.

## Enumerable, List, ForEach

Does developer write code without the usage of loop? Have we ever wondered how many items are there in a list? When talking about a list, we should be aware that all items are stored in memory. So the size really matters here even it is trivial.
Another trap is at the Enumerable. Enumerable represents a sequence that mostly expected to iterate only once. By nature, we do not know the size of a sequence (There is no Count property on the IEnumerable interface). Therefore, when calling .ToList(), be aware of all the nasty things can happen.

I brought them here for reference. It might not a thing that takes down production. But it is nice to be aware of as well.


There might be more about boundaries to watch out. Those are what I have come up so far. What's yours?