Software Best Practices Learned by Experience


[Updated]: It is considered good practice to cache your data in memory, either on a distributed machines by using a robust Caching System like
Meme Cache or Redis. If you can't use those Highly Scalable Distributed Cache System, look into using Concurrent Datastractrure like

ConcurrentDictionary and store only needed or HOT data, HOT data means that is frequently used. Keep in mind to expire records that are not
used for period of time.

Read this Wiki to understand best practices. https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AsyncGuidance.md

public class PersonController : Controller
{
   private DbContext _db;
   
   // The cache needs expiration
   private static ConcurrentDictionary<int, Task<Article>> _cache = new ConcurrentDictionary<int, Task<Article>>();
   
   public PersonController(DbContext db)
   {
      _db = db;
   }
   
   public async Task<IActionResult> GetArticle(int id)
   {
       var article = await _cache.GetOrAdd(id, (key) => _db.Articles.FindAsync(key));
       return Ok(person);
   }
}

[Note]: The best way to create a Model/POCO/Entity Repository is by structuring the Modal based on its own properties. Let the Reporistory contain its own Internal Methods utilizing Arrays. See the code below

//The idea here is to not spread a bunch of Customers in Memory but
//to have an array of Customer properties that would allow a fast Memory access

public sealed class MyCustomerRepository{

int NumberOfCustomer;
double[] Scoring;
DateTime[] DateOfBirth;

//Then create functions that do one thing in this same Class
public void UpdateScoring(){

//Do something

}
}

[8/16/22]: The best way to Call a Web API from C-Sharp



[6/25/22]:
When developing a highly scalable and performant application, think about what data is going to travel on the wire from your Server to the Client (person accessing the web page). Have empathy, care for the user, and put yourself in the user's shoes.

[Rule of Thumb]: Return only necessary data, look into the HTTP Response Payload and remove some of the data not needed for that specific request.
- remove unnecessary cookies, javascript, any processing compute needed to return the response, and unnecessary images, and make sure you are caching appropriately.
- Always verify, DO NOT ASSUME things are working right.
- Pay extra attention to Memory Allocation, every time you want to create a new object think again, and again. Think about different ways to solve the problem without creating an Object to keep track of what you are doing. If you are developing in C-Sharp looking for how you could use Span and Array Sharing to avoid allocation.

- Be aware of 1st, 2nd, and 3rd generation Memory Allocation, Heap vs Stack. This will help you understand Allocation and when the Garbage Collector kicks in
   to collect Stale Objects.

- Know the Life Cycle of the Object you just created, when does it get collected by a Garbage Collector? when you create a new object, does it get allocated
   on the Heap or Stack? does it linger a while before it is disposed of?

- Know where your resources like images and data from the database are, and what your infrastructure looks like. Be frugal in terms of how you call the database, and use resources (do you retrieve or call the database every time the request hits your Server?).

Ask questions:
1. Are the images on #SASS or #SSD Drives? is the #CDN involved in this architecture? what is the Cache Period for this resource?
2. Where is the #Database located? is there a Load #Balancer to help hit the nearest Database Server?
3. Is there any Caching Middleware in place, how do we think performance in this case?
4. When the request hits the server, do you think about returning the response as soon as possible without halting that request?

     - Identify the Hot Path, the code execution route many requests take and eliminate unnecessary computing or processing
            - I mean, if you have an operation that massages the request or retrieves a big blob of data from someone on every request.
              Think about how you could optimize that Hot Path.

5. What is the Internet Speed of the Datacenter this application is going to be hosted?


Ask questions:
1. If this user was me, how would I like the web page to perform when browsing
2. How would I rate my experience when interacting with the page?
3. Would I like to see popups, if yes what information would I like to see in that popup?


Asking the questions above will help evaluate some of the architecture decisions and Software Good Practices to implement.

[6/22/22]: Writing to the Database and Saving Changes
1. when writing to the database using Entity Framework Core 6.0, you need to Save Changes while you are still in the current function, otherwise, when you try to Save Changes when you get out of the function and in another class the EF Core Change Tracker does not detect changes and item won't be saved in the database.

- Rule of thumb though, keep in mind that when you use Async Await do not combine any Async with Async function calls.



[9/20/21]: If you are developing business logic, do not scatter code that does merely the same all over the application. Find one file that is responsible for one task and break up big functions into smaller manageable functions.

- Case in thought: I spent  4hours trying to find what is causing a redirect to another page in a legacy code base. Not cool man!


References:

- Another related Article talking about performance can be found on the link below:
https://ernestech.com/articles/details/3865/







For peering opportunity Autonomouse System Number: AS401345 Custom Software Development at ErnesTech Email AddressContact: [email protected]