CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Karl Seguin

developer @ Fuel Industries ottawa, ontario

Random: Help with ASP.NET + PerfCounters / MySQL Thoughts / RedGate Rules / Amazon Web Service / CDN / PHP and OO

Here's just an accumulation of random thoughts I've had over the last couple weeks.

1.
First and most importantly, I'm running into the problem where I'm trying to build a monitoring system that hooks into the System.Diagnostics.PerformanceCounter class and logs data (to be used for reporting later on) ever X seconds. The problem I'm running into with ASP.NET is that I don't know, nor can I seem to figure out, the instance name for the specific application. When I'm hooking into the ASP.NET Apps vXXXX, this isn't a problem because it uses the IIS name (_LM_W3SVC_.....Root). However, when I need counters from the Process or .NET XXX objects, the instances show up as the actual process (all I get is w3wp, w3wp#1, w3wp#2). At any given time, I have no clue which process is responsible for which application. IISAPP.vbs doesn't help because that just gives me the PID, which I still can't use.


2.
SQL INSERT should support a WHERE clause which actually does an UPDATE for rows matching the WHERE expression. So instead of checking if something exists and doing an IF .. update ... ELSE  insert ..

You can just do:
INSERT INTO Users (....) VALUES (...)
   WHERE UserId = 3


3.
MySQL Stored Procedure language is based on the SQL:2003 ISO/ANSI standard. Compared to TSQL, it's still very young (as is the entire stored procedure support). My biggest complaint, which might just be a case of complete ignorance on my part, is that variables aren't prefixed. In a normal language, variable prefixing isn't important...but in SQL it's critical because you easily get into conflicts with column names. Take:

CREATE PROCEDURE GetUserById
(
   Id INT
)
BEGIN

   SELECT Username, Email
      FROM Users
      WHERE Id = Id
END

How will MySQL handle that? I don't know. We've simply settled on prefixing all our parameters/variables with an underscore.


4.
While we're talking about MySQL..If you are using MySQL, set the sql-mode variable to STRICT_ALL_TABLES. It's a joke that it isn't on by default - and something that's almost impossible to go back and fix. You can learn more here, but implicit behaviour sucks


5.
SQL Server doesn't escape my wrath today either. I don't think Microsoft should buy-out RedGate (because that might put an end to a good thing), but RedGates products certainly put a lot of the MS SQL Server client products to shame. The SQL Bundle is stupidly useful...matched only by Resharper.


6.
The single biggest problem with the Amazon Web Services is...that someone at Amazon decided to call them Web Services. If you've never heard of AWS,
what would you think? Another Web 2.0 web service to tie into some Amazon property, like their store, or their search or something. WRONG! AWS S3 is a cheap storage/bandwidth service...and EC2 (which we just love here at Fuel) is an "Elastic Cloud" network which lets us quickly scale computing power at the push of a button. Per hour billing of CPU cycles is very interesting to us, and as it gets easier and easier to do, it'll be a lot more interesting to everyone else too. It'd be great if EC2 ran Windows and they had worked out some great licensing arrangement with Microsoft, but it's such a great service
we're just not using MS products on there (we might try a mono-solution in the near future though). Microsoft and Google and the thousands of hosting companies are gonna have to adjust.

7.
Speaking of Content Delivery, if you are paying too much for bandwidth (like more than $1.00/gb) consider using one of the many newer CDN's out there. We prefer PantherExpress, but there's also CacheFly and Amazon S3. In my experience, you pay A LOT less, get MUCH better customer service and almost as good (in some cases much better) speeds as you do with the 2 big boys of the CDN industry.

8.
Can't finish this off without laughing at PHP. I've said it before, and I'll say it again, PHP developers and the PHP team itself just don't get object
oriented programming. The MySQLi extension is the OO way of connecting to a MySQL database (versus the normal MySQL extension). Not only does MySQLi support a completely procedural way of being used, but check out their documentation on how to use it:

Example 1417. Object oriented style
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

printf("Host information: %s\n", $mysqli->host_info);

/* close connection */
$mysqli->close();
?>

Notice those crapy mysqli_connect_errno() and mysqli_connect_error() calls? I'm sure the lack of exceptions will make Joel happy, but come on...this just plain out sucks!



Comments

WaterBreath said:

2) SQL:2003 supports a DML statement called MERGE which will let you do exactly that.  But I can't find documentation about this on the MySQL webpage, so maybe they don't support it.  They do have a REPLACE statement which is similar but only works with a primary key or unique index.  See dev.mysql.com/.../replace.html

3) Have you tried using a table alias?  In most DBMSes you can do something like below.

CREATE PROCEDURE GetUserById

(

    Id INT

)

BEGIN

    SELECT Username, Email

         FROM Users u

     WHERE u.Id = Id

END

I think this will work in MySQL as well.  Or, maybe you could just tweak the parameter name.

# April 24, 2007 3:23 PM

Ayende Rahien said:

2/ INSERT that Updates

This is usually called Upsert, and can be implemented as:

INSERT INTO Foo (...) values(...)

WHERE NOT EXISTS

(

select 1 from Foo WHERE User = 3

)

UPDATE Foo

Set ...

Where User = 3

# April 24, 2007 6:22 PM

Fregas said:

If you're referring to Joel on Software, i bet you're right. I've almost given up on getting useful information out of him.  Every now and then he has a nugget of wisdom.

Yeah, the lack of OOness is one of the things that have driven me screaming away from doing any PHP.  I'm hoping Ruby or Python takes its place as the open source web language of choice.

# April 24, 2007 7:42 PM

karl said:

Ya...Spolksy....haven't read his blog in forever. I'm continuously amazed that he's running a successful business doing what he does.

# April 25, 2007 11:35 AM

Eric said:

Karl-

I'd love to know how you guys are using EC2. I'm a big fan of S3 and find the concept of EC2 fascinating, I just haven't found a good application for it yet.

-Eric

# April 25, 2007 12:36 PM

karl said:

Eric:

We are currently only using it as a cheap unmanaged hosting which provides tremendous ability for us to quickly scale to a spike in demand.

Our goal moving forward is to automate the scaling. For example, I used to work on a website that got 4million+ hits a day on Mondays, with traffic steadily declining until Friday. This meant we had spare CPU cycles 6 days out of 7 days (since we had to build our infrastructure to support the highest demand).

Using EC2, we could start up 25 servers on Monday at 5:00AM, and shut them down at ~10:00PM, which, over the week, would represent a significant savings.

I'm sure most busy sites have similar predictable traffic. Without wanting to sound too-North American centric, I'm sure digg.com sees a huge spike in traffic between 8:00AM-EST  and 10:00PM-PST. EC2 could probably save them money.

Speaking of digg, it would be a cool blog-engine addon to integrate with EC2. The blog engine would detect huge spikes in traffic an automatically start up a new server, which would then register itself with the load balancer or something. I was actually thinking you could probably build a HttpModule that monitored traffic or CPU load and automatically did just that.

Although i can't speak in any detail about it, we are using it in a very similar way. We expect to have very predictable spikes of traffic and want to only pay for the CPU cycles as they are needed. Even if it's manual for now, it still works quite well.

# April 25, 2007 6:31 PM

Eric said:

Karl-

Thanks for your comments. I'm in the queue for the EC2 service, so hopefully I'll be able to get into it soon.

I love the idea of a flexible pool of servers that expands and contracts based on load and the idea of doing that in an automated fashion makes a lot of sense. I read an article in the EC2 documentation about someone that did something similar by having each server register itself on startup in a file on S3 that the load balancer would use to determine where to send requests. Granted, that example is less about automatically increasing capacily and more about automatically registering a server in a pool, but cool nonetheless and half the battle.

Just as profound as being able to expand and contract resources is the change in design strategy that a service like this requires. Thinking about a site as a cloud of federated servers that can expand and contract at any time and that have only transient disks forces us as developers to address issues that we wouldn't otherwise, like how to store a database if there is no "durable" local disk storage. It's a constraint that could lead to greater scalability and resilience. Granted, we should probably always be assuming this constraint, regardless of hosting arangement, but it's all to easy not to.

-Eric

# April 26, 2007 12:08 PM

Alan said:

Karl: About the your problem with PHP I just left you a comment on you previous post about this.

Here is a link for those reading the comments.

codebetter.com/.../Is-PHP-the-new-VB6_3F00_.aspx

# May 5, 2007 1:39 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

Free Tech Publications